一、基本用法,示例:python
---
- 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模塊,這樣可讓用戶本身決定是否繼續運行任務:web
- name: pause for unexpected conditions
pause: prompt="Unexpected OS"
when: ansible_os_family !="RedHat"
二、在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
四、若是變量不存在,則能夠經過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
五、when在循環語句中的使用方法,示例:
tasks: - command: echo {{ item }} with_items: [ 0, 2, 4, 6, 8, 10 ] when: item > 5六、在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' }
2、條件導入
有些時候,你也許想在一個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’。 shell
3、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/
4、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"5、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/1757593apache