在有的時候play的結果依賴於變量、fact或者是前一個任務的執行結果,從而需要使用到條件語句。
一、when
有的時候在特定的主機需要跳過特定的步驟,例如在安裝包的時候,需要指定主機的操作系統類型,或者是當操作系統的硬盤滿了之後,需要清空文件等,可以使用when語句來做判斷 。when關鍵字後面跟着的是python的表達式,在表達式中你能夠使用任何的變量或者fact,當表達式的結果返回的是false,便會跳過本次的任務
1、基本用法,示例:
---
- name: Install VIM
hosts: all tasks:
- name:Install VIM via yum
yum: name=vim-enhanced state=installed
when: ansible_os_family =="RedHat"
- name:Install VIM via apt
apt: name=vim state=installed
when: ansible_os_family =="Debian"
- name: Unexpected OS family
debug: msg="OS Family ` ansible_os_family ` is not supported" fail=yes
when: not ansible_os_family =="RedHat" or ansible_os_family =="Debian"
條件語句還有一種用法,它還可以讓你當達到一定的條件的時候暫停下來,等待你的輸入確認。一般情況下,當ansible遭遇到error時,它會直接結束運行。那其實你可以當遭遇到不是預期的情況的時候給使用pause模塊,這樣可以讓用戶自己決定是否繼續運行任務:
- name: pause for unexpected conditions
pause: prompt="Unexpected OS"
when: ansible_os_family !="RedHat"
2、在when中使用jinja2的語法,示例:
tasks: - command: /bin/false register: result #將命令執行的結果傳遞給result變量 ignore_errors: True #忽略錯誤 - command: /bin/something when: result|failed #如果註冊變量的值 是任務failed則返回true - command: /bin/something_else when: result|success #如果註冊變量的值是任務success則返回true - command: /bin/still/something_else when: result|skipped #如果註冊變量的值是任務skipped則返回true - command: /bin/foo when: result|changed #如果註冊變量的值是任務changed則返回true
- hosts: all user: root vars: epic: true tasks: - shell: echo "This certainly is epic!" when: epic - shell: echo "This certainly is not epic!" when: not epic
4、如果變量不存在,則可以通過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
5、when在循環語句中的使用方法,示例:
tasks: - command: echo {{ item }} with_items: [ 0, 2, 4, 6, 8, 10 ] when: item > 56、在include和roles中使用when:
在include中使用的示例:- include: tasks/sometasks.yml when: "'reticulating splines' in output"
在roles中使用的示例:- hosts: webservers roles: - { role: debian_stock_config, when: ansible_os_family == 'Debian' }
二、條件導入
有些時候,你也許想在一個Playbook中以不同的方式做事,比如說在debian和centos上安裝apache,apache的包名不同,除了when語句,還可以使用下面的示例來解決:
-- - hosts: all remote_user: root vars_files: - "vars/common.yml" - [ "vars/{{ ansible_os_family }}.yml", "vars/os_defaults.yml" ] tasks: - name: make sure apache is running service: name={{ apache }} state=running很多不同的yml文件只是包含鍵和值,如下:
--- # for vars/CentOS.yml apache: httpd somethingelse: 42
如果操作系統是’CentOS’, Ansible導入的第一個文件將是’vars/CentOS.yml’,緊接着 是’/var/os_defaults.yml’,如果這個文件不存在。而且在列表中沒有找到,就會報錯。 在Debian系統中,最先查看的將是’vars/Debian.yml’而不是’vars/CentOS.yml’, 如果沒找到,則尋找默認文件’vars/os_defaults.yml’。
三、with_first_found
有些時候,我們想基於不同的操作系統,選擇不同的配置文件,及配置文件的存放路徑,可以藉助with_first_found來解決:
- name: template a file template: src={{ item }} dest=/etc/myapp/foo.conf with_first_found: - files: - {{ ansible_distribution }}.conf - default.conf paths: - search_location_one/somedir/ - /opt/other_location/somedir/
四、failed_when
failed_when其實是ansible的一種錯誤處理機制,是由fail模塊使用了when條件語句的組合效果。示例如下:
- name: this command prints FAILED when it fails command: /usr/bin/example-command -x -y -z register: command_result failed_when: "'FAILED' in command_result.stderr"我們也可以直接通過fail模塊和when條件語句,寫成如下:
- name: this command prints FAILED when it fails command: /usr/bin/example-command -x -y -z register: command_result ignore_errors: True - name: fail the play if the previous command did not succeed fail: msg="the command failed" when: "'FAILED' in command_result.stderr"五、changed_when
當我們控制一些遠程主機執行某些任務時,當任務在遠程主機上成功執行,狀態發生更改時,會返回changed狀態響應,狀態未發生更改時,會返回OK狀態響應,當任務被跳過時,會返回skipped狀態響應。我們可以通過changed_when來手動更改changed響應狀態。示例如下:
- shell: /usr/bin/billybass --mode="take me to the river" register: bass_result changed_when: "bass_result.rc != 2" #只有該條task執行以後,bass_result.rc的值不爲2時,纔會返回changed狀態 # this will never report 'changed' status - shell: wall 'beep' changed_when: False #當changed_when爲false時,該條task在執行以後,永遠不會返回changed狀態
本文出自 “無名小卒” 博客,請務必保留此出處http://breezey.blog.51cto.com/2400275/1757593