ptyhon中salt state中使用判斷、迴圈及變數

來源:互聯網
上載者:User

上篇文章在state檔案中使用了jinja模板,提高了書寫state檔案的靈活性。本篇繼續學習state檔案的編寫。在state檔案中可以使用判斷、迴圈及變數,使大大提高了其靈活性,本篇來學習一下這些用法。

在salt state中使用判斷

這個樣本使用了兩個minions,一個名稱為stgdc1app01,另一個為stgdc2app01。在“salt的安裝和配置”中介紹了如何安裝和配置salt的master和minion,這裡直接進入state檔案的編寫。本篇的目標是根據minion的fqdn名稱,建立網路的設定檔,設定檔基於模板檔案,在建立過程中使用了一些基於Python的操作。下面就看一下具體的操作:

首先需要得到這兩個minions的關於網路的grains資料,後面需要用到:

[root@centos7-A ~]# salt \* grains.item ip_interfaces hwaddr_interfaces
stgdc2app01:
    ----------
    hwaddr_interfaces:
        ----------
        eno16777736:
            00:0c:29:62:4d:63
        lo:
            00:00:00:00:00:00
    ip_interfaces:
        ----------
        eno16777736:
            - 192.168.71.169
            - fe80::20c:29ff:fe62:4d63
        lo:
            - 127.0.0.1
            - ::1
stgdc1app01:
    ----------
    hwaddr_interfaces:
        ----------
        eno16777736:
            00:0c:29:94:77:20
        lo:
            00:00:00:00:00:00
    ip_interfaces:
        ----------
        eno16777736:
            - 192.168.71.168
            - fe80::20c:29ff:fe94:7720
        lo:
            - 127.0.0.1
            - ::1
接著在staging的環境下建立netconfig的state,就是在staging的基目錄下建立netconfig檔案夾,在此檔案夾下繼續建立一個名為files的檔案夾,在files檔案夾下建立名為ifcfg-eth0的檔案,在netconfig檔案夾下建立init.sls檔案,最後檔案夾的目錄結構類似於如下這樣:

[root@centos7-A staging]# tree
.
└── netconfig
    ├── files
    │   └── ifcfg-eth0
    └── init.sls

2 directories, 2 files
init.sls檔案和ifcfg-eth0檔案的內容分別如下:

ifcfg-eth0
DEVICE=eth0
HWADDR={{ hwaddr }}
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR={{ ipaddr }}
NETMASK={{ netmask }}
init.sls
{% if grains['id'].startswith('stgdc1') %}
{% set netmask = '255.255.255.0' %}
{% elif grains['id'].startswith('stgdc2') %}
{% set netmask = '255.255.0.0' %}
{% endif %}
network_file:
 file.managed:
- name: /etc/sysconfig/network-scripts/ifcfg-eth0
- source: salt://netconfig/files/ifcfg-eth0
- mode: 644
- template: jinja
- context:
ipaddr: {{ grains['ip_interfaces']['eno16777736'][0] }}
netmask: {{ netmask }}
hwaddr: {{ grains['hwaddr_interfaces']['eno16777736'].upper() }}
檔案產生的大部分過程與上篇文章介紹的一致,這裡我們主要關心init.sls檔案中的第1行和第3行,它使用了判斷語句,並且使用了Python的函數startswith,它根據minion名稱的不同,設定不同的netmask。另外注意此檔案的最後一行使用了Python的函數upper,它將字串全部轉換為大寫。完成上述檔案的編寫後,便可以將state應用到minions上:

[root@centos7-A netconfig]# salt -L 'stgdc1app01,stgdc2app01' state.sls netconfig saltenv=staging
在應用state到minions上時,這裡使用了-L命令列選項來指定應用到的minion。-L選項的意義是使用列表來指定minion,下篇會學習各種指定minions的方式。

在salt state中使用迴圈

仍然使用上面的minions,在staging環境中,編輯pillar檔案/opt/salt/pillar/staging/user/init.sls,使其具有以下內容:

init.sls
dev_user_list:
optimus:
uid: 7001
passwd: '$1$Dw1TxMI7$pmeYTdmz.rlunqPd7JELR.'
bumblebee:
uid: 7002
passwd: '$1$ZHUeIAfq$6sJl9rHVDX2UjBH1KrPZP1'
ironhide:
uid: 7003
passwd: '$1$rcJAiq7y$bJzv3HzVTbeQlA3cIu1Gb1'
編輯user state的設定檔/opt/salt/staging/user/init.sls,使其具有如下內容:

init.sls
{% for user, details in pillar['dev_user_list'].iteritems() %}
{{ user }}:
 user.present:
- home: /home/{{ user }}
- uid: {{ details['uid'] }}
- password: {{ details['passwd'] }}
- shell: /bin/bash
{% endfor %}
上面檔案的第一行和最後一行組成了for迴圈,pillar[‘dev_user_list’]中儲存了一個Python字典,在使用了iteritems函數後,字典中的username儲存在user變數中,其它資料儲存在details變數中。可以通過和前面一樣的方式來應用state到minions上。

在salt state中使用變數

其實在第一小節“在salt state中使用判斷”裡已經應用到了變數。在state的init.sls檔案中,第2行為:

{% set netmask = '255.255.255.0' %},
這裡set關鍵字用來設定變數,netmask作為變數名,變數值為等號後面的部分。

在使用變數時,一定不要在變數名外加引號,就像init.sls檔案中第14行那樣使用:

netmask: {{ netmask }}
這裡前面的netmask是在context下,表示模板ifcfg-eth0中的netmask的值是從這裡得到的。後面的netmask是state檔案中的變數,由set關鍵字指定,值是等號後面的部分。

本篇學習了salt state中判斷、迴圈及變數的使用。下篇看一下salt中指定minion的各種方式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.