-
When語句
有時候用戶有可能需要某一個主機越過某一個特定的步驟.這個過程就可以簡單的像在某一個特定版本的系統(tǒng)上少裝了一個包一樣或者像在一個滿了的文件系統(tǒng)上執(zhí)行清理操作一樣.
這些操作在Ansible上,若使用when
語句都異常簡單.When語句也含Jinja2表達式,
第一個例子:tasks: - name: "shutdown Debian flavored systems" command: /sbin/shutdown -t now when: ansible_os_family == "Debian"
如果你在RedHat系列l(wèi)inux系統(tǒng)執(zhí)行挠阁,就不會被執(zhí)行
第二個例子:
#cat copyfile.yml --- - hosts: "`host`" user: "`user`" gather_facts: True tasks: - name: Copy file to client copy: src=/etc/ansible/test.txt dest=/usr/local/src when: ansible_os_family == "Debian" - include: add_user.yml when: ansible_os_family == "Debian"
執(zhí)行:
ansible-playbook copyfile.yml -e "host=web user=root" --可以看到下面都skipping掉了PLAY *************************************************************************** TASK [setup] ******************************************************************* ok: [10.0.90.26] ok: [10.0.90.25] TASK [Copy file to client] ***************************************************** skipping: [10.0.90.25] skipping: [10.0.90.26] TASK [include] ***************************************************************** skipping: [10.0.90.25] skipping: [10.0.90.26] PLAY RECAP ********************************************************************* 10.0.90.25 : ok=1 changed=0 unreachable=0 failed=0 10.0.90.26 : ok=1 changed=0 unreachable=0 failed=0
以上yml中的任務都沒有執(zhí)行陌知,因為我測試機器是CentOS系統(tǒng)超埋!而條件是只有當系統(tǒng)是Debian類型的時候執(zhí)行!
PS:文件中的include模塊后續(xù)會介紹荒适。第三個例子:
tasks: - shell: echo "only on Red Hat 6, derivatives, and later" when: ansible_os_family == "RedHat" and ansible_distribution_major_version|int >= 6
意思是只有當系統(tǒng)是RedHat并且版本大于等于6的時候執(zhí)行
第三個例子:只有在server組或者名為server的這臺服務器才執(zhí)行- name: Copy file to client copy: src=/etc/ansible/test.txt dest=/usr/local/src when: "host=='server'"
帶管道的when語句
tasks: - command: /bin/false register: result ignore_errors: True - command: /bin/something when: result|failed - command: /bin/something_else when: result|success - command: /bin/still/something_else when: result|skipped
判斷變量是否已經(jīng)定義:
如果一個變量不存在,你可以使用Jinja2的defined
命令跳過或略過.例如:tasks: - shell: echo "I've got '{{ foo }}' and am not afraid to use it!" when: foo is defined - fail: msg="Bailing out. this play requires 'bar'" when: bar is not defined
-
debug模塊
Print statements during execution
打印執(zhí)行過程中的語句,通常用來調(diào)試編寫好的playbook語句,Examples # Example that prints the loopback address and gateway for each host - debug: msg="System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}" - debug: msg="System {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }}" when: ansible_default_ipv4.gateway is defined - shell: /usr/bin/uptime register: result - debug: var=result verbosity=2 - name: Display all variables/facts known for a host debug: var=hostvars[inventory_hostname] verbosity=4
生產(chǎn)環(huán)境遇到的一個案例:
有2臺server:
第一臺:10.0.90.25安裝了nginx杰扫,
第二臺:10.0.90.26沒有安裝nginx
現(xiàn)在我只想在沒有安裝nginx的server上做操作,需要通過when條件語句實現(xiàn)膘掰,如下:#cat test1.yml --- - hosts: web remote_user: root tasks: - name: ps shell: ps -ef | grep nginx | grep -v grep|wc -l register: nginx_num - debug: var=nginx_num - name: command copy: src=/etc/ansible/server.xml dest=/root when: nginx_num.stdout == "0"
PS:剛開始的時候章姓,沒有添加- debug: var=nginx_num這一項,結果執(zhí)行的時候识埋,總是skipping跳過凡伊,說明條件錯誤后來才使用debug模塊調(diào)試
執(zhí)行結果:#ansible-playbook test1.yml PLAY *************************************************************************** TASK [setup] ******************************************************************* ok: [10.0.90.25] ok: [10.0.90.26] TASK [ps] ********************************************************************** changed: [10.0.90.25] changed: [10.0.90.26] TASK [debug] ******************************************************************* ok: [10.0.90.25] => { "nginx_num": { "changed": true, "cmd": "ps -ef | grep nginx | grep -v grep|wc -l", "delta": "0:00:00.008476", "end": "2016-05-19 20:40:51.742088", "rc": 0, "start": "2016-05-19 20:40:51.733612", "stderr": "", "stdout": "3", "stdout_lines": [ "3" ], "warnings": [] } } ok: [10.0.90.26] => { "nginx_num": { "changed": true, "cmd": "ps -ef | grep nginx | grep -v grep|wc -l", "delta": "0:00:00.009458", "end": "2016-05-19 20:40:51.754993", "rc": 0, "start": "2016-05-19 20:40:51.745535", "stderr": "", "stdout": "0", "stdout_lines": [ "0" ], "warnings": [] } } TASK [command] ***************************************************************** skipping: [10.0.90.25] changed: [10.0.90.26] PLAY RECAP ********************************************************************* 10.0.90.25 : ok=3 changed=1 unreachable=0 failed=0 10.0.90.26 : ok=4 changed=2 unreachable=0 failed=0
可以看到跳過了10.0.90.25,只在10.0.90.26上執(zhí)行了命令窒舟。
范例1:#cat test2.yml --- - hosts: 10.0.90.25 user: root gather_facts: True tasks: - name: test debug module debug: msg="System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}" #ansible-playbook test2.yml PLAY *************************************************************************** TASK [setup] ******************************************************************* ok: [10.0.90.25] TASK [test debug module] ******************************************************* ok: [10.0.90.25] => { "msg": "System 10.0.90.25 has uuid 564DB430-3121-EEE4-33F1-559FEF576AC9" } PLAY RECAP ********************************************************************* 10.0.90.25 : ok=2 changed=0 unreachable=0 failed=0
范例2:
#cat test3.yml --- - hosts: 10.0.90.25 user: root gather_facts: True tasks: - debug: msg="System {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }}" when: ansible_default_ipv4.gateway is defined 說明:只有當遠端server的gateway配置的情況下才執(zhí)行系忙,執(zhí)行結果如下: #ansible-playbook test3.yml PLAY *************************************************************************** TASK [setup] ******************************************************************* ok: [10.0.90.25] TASK [debug] ******************************************************************* ok: [10.0.90.25] => { "msg": "System 10.0.90.25 has gateway 10.0.90.1" } PLAY RECAP ********************************************************************* 10.0.90.25 : ok=2 changed=0 unreachable=0 failed=0 將10.0.90.25網(wǎng)關配置去掉,再一次執(zhí)行: #ansible-playbook test3.yml PLAY *************************************************************************** TASK [setup] ******************************************************************* ok: [10.0.90.25] TASK [debug] ******************************************************************* skipping: [10.0.90.25] PLAY RECAP ********************************************************************* 10.0.90.25 : ok=1 changed=0 unreachable=0 failed=0
說明:因為將遠端server的網(wǎng)關配置去掉了惠豺,when條件不成立银还,就skipping了。
-
notify和Handlers
handlers 用于在發(fā)生改變時執(zhí)行的操作洁墙。notify這個action可用于在每個play的最后被觸發(fā),這樣可以避免多次有改變發(fā)生時每次都執(zhí)行指定的操作,取而代之的是僅在所有的變化發(fā)生完成后一次性地執(zhí)行指定操作蛹疯。比如多個resources指出因為一個配置文件被改動,所以apache需要重新啟動热监,但是重新啟動的操作只會被執(zhí)行一次捺弦。在notify中列出的操作稱為handler也即notify調(diào)用handler中定義的操作,Handlers也是一些task的列表,通過名字來引用列吼,他們和一般的task并沒有什么區(qū)別幽崩。Handlers是由通知者進行notify,如果沒有被notify寞钥,handlers不會執(zhí)行歉铝,不管有多少個通知者進行了notify,等到play中的所有task執(zhí)行完成之后凑耻,handlers也只會被執(zhí)行一次太示。
注意:Handlers 最佳的應用場景是用來重啟服務,或者觸發(fā)系統(tǒng)重啟操作.除此以外很少用到了,而且它會按照聲明的順序執(zhí)行
如下一個例子:
自定義好httpd.conf配置文件(有變量)香浩,拷貝到/etc/httpd/conf目錄类缤,然后啟動httpd,并設置開機自啟動邻吭!#cat test3.yml --- - hosts: web remote_user: root gather_facts: True vars: http_port: 80 max_clients: 200 tasks: - name: ensure apache is at the latest version yum: pkg=httpd state=latest - name: copy httpd config file to client copy: src=/etc/ansible/httpd_test.config dest=/etc/httpd/conf/ notify: - restart apache - name: ensure apache is running service: name=httpd state=started handlers: - name: restart apache service: name=httpd state=restarted
執(zhí)行:
#ansible-playbook test3.yml PLAY *************************************************************************** TASK [setup] ******************************************************************* ok: [10.0.90.25] ok: [10.0.90.26] TASK [ensure apache is at the latest version] ********************************** changed: [10.0.90.26] changed: [10.0.90.25] TASK [copy httpd config file to client] **************************************** changed: [10.0.90.26] changed: [10.0.90.25] TASK [ensure apache is running] ************************************************ ok: [10.0.90.26] ok: [10.0.90.25] RUNNING HANDLER [restart apache] *********************************************** changed: [10.0.90.26] changed: [10.0.90.25] PLAY RECAP ********************************************************************* 10.0.90.25 : ok=5 changed=3 unreachable=0 failed=0 10.0.90.26 : ok=5 changed=3 unreachable=0 failed=0
注:此處定義的notify是restart餐弱,就是安裝好httpd并拷貝好配置文件之后。
notify也可以是restarted囱晴、stopped膏蚓、reloaded
enabled=yes是表示設置httpd開機自啟動。
如果再次執(zhí)行畸写,就不會有任何改變了驮瞧。因為httpd已經(jīng)啟動了#ansible-playbook test3.yml PLAY *************************************************************************** TASK [setup] ******************************************************************* ok: [10.0.90.25] ok: [10.0.90.26] TASK [ensure apache is at the latest version] ********************************** ok: [10.0.90.25] ok: [10.0.90.26] TASK [copy httpd config file to client] **************************************** ok: [10.0.90.26] ok: [10.0.90.25] TASK [ensure apache is running] ************************************************ ok: [10.0.90.26] ok: [10.0.90.25] PLAY RECAP ********************************************************************* 10.0.90.25 : ok=4 changed=0 unreachable=0 failed=0 10.0.90.26 : ok=4 changed=0 unreachable=0 failed=0
Ansible之when條件語句
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來待错,“玉大人籽孙,你說我怎么就攤上這事±署” “怎么了蚯撩?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長烛占。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么忆家? 我笑而不...
- 正文 為了忘掉前任犹菇,我火速辦了婚禮,結果婚禮上芽卿,老公的妹妹穿的比我還像新娘揭芍。我一直安慰自己,他們只是感情好卸例,可當我...
- 文/花漫 我一把揭開白布称杨。 她就那樣靜靜地躺著,像睡著了一般筷转。 火紅的嫁衣襯著肌膚如雪姑原。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼朵逝,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了乡范?” 一聲冷哼從身側響起廉侧,我...
- 正文 年R本政府宣布器仗,位于F島的核電站融涣,受9級特大地震影響童番,放射性物質發(fā)生泄漏。R本人自食惡果不足惜威鹿,卻給世界環(huán)境...
- 文/蒙蒙 一剃斧、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧忽你,春花似錦幼东、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至糟秘,卻和暖如春简逮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蚌堵。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- ###### Ansible總結 ##### 運維工作: 系統(tǒng)安裝(物理機、虛擬機)-->程序包安裝性雄、配置没卸、服務啟...
- 一、高可用集群 (一)提升系統(tǒng)高可用性的解決方案:冗余(redundant) 工作模式active/passive...