官术网_书友最值得收藏!

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...

  1. Configure two minions, one named stgdc1app01 and another named stgdc2app01.
  2. 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']}
    
  3. Create a new state in the staging environment called netconfig and create a directory in the netconfig directory called files.
  4. 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 }}
  5. 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() }}
  6. 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
主站蜘蛛池模板: 鄄城县| 灵丘县| 峡江县| 奉新县| 隆德县| 金川县| 漯河市| 安泽县| 芦山县| 和顺县| 郁南县| 霍州市| 广平县| 西平县| 阿图什市| 黎川县| 若尔盖县| 卢龙县| 奉贤区| 南昌市| 永修县| 临澧县| 湖南省| 城固县| 景谷| 苏尼特右旗| 邛崃市| 五台县| 晋中市| 临西县| 屏东市| 晋宁县| 黄龙县| 姚安县| 祁阳县| 颍上县| 安平县| 沙湾县| 怀集县| 枣强县| 万州区|