- Salt Cookbook
- Anirban Saha
- 756字
- 2021-07-16 13:21:57
Using Python functions in conditionals
Being based on Python, one of the biggest advantages of Salt is to be able to use a lot of Salt-specific features such as functions. In this recipe, you will learn how to use Python functions in Salt to manipulate data and make efficient use of them to configure our infrastructure.
How to do it...
- Configure two minions, one named
stgdc1app01
and another namedstgdc2app01
. - Run the following commands to get the grains with which we will be working in this recipe:
[root@salt-master ~]# salt 'stgdc1app01' grains.item \ ip_interfaces hwaddr_interfaces stgdc1app01: hwaddr_interfaces: {'lo': '00:00:00:00:00:00', 'eth0': '00:0c:29:ef:d5:56', 'eth1': '00:0c:29:ef:d5:4c'} ip_interfaces: {'lo': ['127.0.0.1'], 'eth0': ['192.168.0.2'], 'eth1': ['192.168.29.128']} [root@salt-master ~]# salt 'stgdc2app01' grains.item \ ip_interfaces hwaddr_interfaces stgdc2app01: hwaddr_interfaces: {'lo': '00:00:00:00:00:00', 'eth0': '00:0c:29:61:ea:08', 'eth1': '00:0c:29:61:ea:fe'} ip_interfaces: {'lo': ['127.0.0.1'], 'eth0': ['172.16.0.3'], 'eth1': ['172.16.29.129']}
- Create a new state in the staging environment called
netconfig
and create a directory in thenetconfig
directory calledfiles
. - Create the
/opt/salt-cookbook/staging/netconfig/files/ifcfg-eth0
file and edit it to have the following contents:DEVICE=eth0 HWADDR={{ hwaddr }} TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=no BOOTPROTO=none IPADDR={{ ipaddr }} NETMASK={{ netmask }}
- Next, create the
/opt/salt-cookbook/staging/netconfig/init.sls
file and edit it to have the following contents:{% if grains['fqdn'].startswith('stgdc1') %} {% set netmask = '255.255.255.0' %} {% elif grains['fqdn'].startswith('stgdc2') %} {% set netmask = '255.255.0.0' %} {% endif %} network_file: file.managed: - name: /etc/sysconfig/network-scripts/ifcfg-eth0 - source: salt://hostconfig/files/ifcfg-eth0 - mode: 644 - template: jinja - context: ipaddr: {{ grains['ip_interfaces']['eth0'][0] }} netmask: {{ netmask }} hwaddr: {{ grains['hwaddr_interfaces']['eth0'].upper() }}
- Run the following command to apply the state to the minions:
[root@salt-master ~]# salt -L 'stgdc1app01,stgdc2app01' state.sls netconfig saltenv=staging stgdc1app01: ---------- ID: network_file Function: file.managed Name: /etc/sysconfig/network-scripts/ifcfg-eth0 Result: True Comment: File /etc/sysconfig/network-scripts/ifcfg-eth0 updated Changes: ---------- diff: --- +++ @@ -1,6 +1,8 @@ DEVICE=eth0 HWADDR=00:0c:29:ef:d5:56 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=no -BOOTPROTO=dhcp +BOOTPROTO=none +IPADDR=192.168.0.3 +NETMASK=255.255.255.0 Summary ------------ Succeeded: 1 Failed: 0 ------------ Total: 1 stgdc2app01: ---------- ID: network_file Function: file.managed Name: /etc/sysconfig/network-scripts/ifcfg-eth0 Result: True Comment: File /etc/sysconfig/network-scripts/ifcfg-eth0 updated Changes: ---------- diff: --- +++ @@ -1,6 +1,8 @@ DEVICE=eth0 HWADDR=00:0C:29:61:EA:08 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=no -BOOTPROTO=dhcp +BOOTPROTO=none +IPADDR=172.16.0.3 +NETMASK=255.255.0.0 Summary ------------ Succeeded: 1 Failed: 0 ------------ Total: 1
How it works...
In this recipe, we demonstrated the usage of Python functions in Salt. The objective of the recipe is to make a decision based on the fqdn
grain of the minion and set a variable. The variable along with some more Python function-based manipulation is then used to create a network configuration file on the minion based on a template that we created. We will learn more about templates in Chapter 3, Modules, Orchestration, and Scaling Salt.
First, we created two minions with different names. As subnet masks are not yet available in Salt via grains, we set the subnet masks for the minions through variables using a conditional based on the fqdn
grain:
{% if grains['fqdn'].startswith('stgdc1') %}
Here, we can see the use of a Python function startswith()
, which finds out if the minion name starts with devdc1
or devdc2
. Based on this knowledge, it sets the netmask
variable.
Next, we used a template to create a file on the minions and passed few values to the template. The IP address is passed by a direct use of the grain value. The subnet mask is passed by use of the variable netmask
that we had set before.
We see a second use of a Python function in the third value that is passed to the template:
hwaddr: {{ grains['hwaddr_interfaces']['eth1'].upper() }}
Here, we get the value of the MAC address of the eth0
interface by using the grain access method. We then apply the upper()
Python function of this value to convert all of the characters to uppercase.
Next, we applied the state to the minions by use of the list matcher:
[root@salt-master ~]# salt -L 'stgdc1app01,stgdc2app01'
When we build new hosts, most of the time the network configuration is in dhcp
mode. In the output of the command, we see the dhcp
mode being changed to static mode for the network configuration and the values of the IP address and subnet mask being added. The MAC address does not change because it was already present in the configuration file at the time of the build.
This recipe mentions just a couple of the Python functions that can be used to manipulate Salt data, whereas in production configurations, a lot more can be done using functions.
See also
- The Setting host entries and grains recipe in Chapter 4, General Administration Tasks, to find a demonstration of Python function in Salt conditional
- The Targeting minions recipe, to learn how to use matchers in Salt
- The Using iterations in states recipe, to learn how to implement iterations
- SPSS數(shù)據(jù)挖掘與案例分析應(yīng)用實踐
- 從零開始:數(shù)字圖像處理的編程基礎(chǔ)與應(yīng)用
- Vue.js前端開發(fā)基礎(chǔ)與項目實戰(zhàn)
- Visual FoxPro 程序設(shè)計
- Network Automation Cookbook
- Learning ASP.NET Core 2.0
- 自制編程語言
- C/C++程序員面試指南
- Mobile Device Exploitation Cookbook
- 小程序從0到1:微信全棧工程師一本通
- Learning Android Application Testing
- Mastering ASP.NET Core 2.0
- 零基礎(chǔ)學(xué)SQL(升級版)
- Elasticsearch搜索引擎構(gòu)建入門與實戰(zhàn)
- Java Web動態(tài)網(wǎng)站開發(fā)(第2版·微課版)