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

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
主站蜘蛛池模板: 阿巴嘎旗| 祥云县| 威宁| 汉源县| 嘉祥县| 贵港市| 余姚市| 高青县| 和田市| 正镶白旗| 阳曲县| 长沙市| 韶山市| 桐城市| 唐河县| 武义县| 诏安县| 新余市| 大田县| 葫芦岛市| 塘沽区| 阆中市| 黔江区| 庐江县| 阿克陶县| 永春县| 湘西| 军事| 丹巴县| 桐庐县| 蒲城县| 浙江省| 海淀区| 长沙市| 新竹市| 昌宁县| 牡丹江市| 岗巴县| 连平县| 德令哈市| 新蔡县|