這個when應該不能算是一個[模塊][1]
Ansible提供when語句,可以控制任務的執行流程。
一個很簡單的when語句的例子
~~~
tasks:
- name: "shutdown Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_os_family == "Debian
~~~
在符合語句中也可以使用小括號:
~~~
tasks:
- name: "shutdown CentOS 6 and 7 systems"
command: /sbin/shutdown -t now
when: ansible_distribution == "CentOS" and
(ansible_distribution_major_version == "6" or ansible_distribution_major_version == "7")
~~~
在`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
~~~
有時候需要將一個字符串的變量轉換為整數來進行數字比較:
~~~
tasks:
- shell: echo "only on Red Hat 6, derivatives, and later"
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int >= 6
~~~
在playbooks和inventory中定義的變量都可以使用,如,需要根據一個變量的bool值決定是否執行該任務:
~~~
vars:
epic: true
~~~
條件語句:
~~~
tasks:
- shell: echo "This certainly is epic!"
when: epic
~~~
~~~
tasks:
- shell: echo "This certainly isn't 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
~~~
[1]:http://docs.ansible.com/ansible/latest/playbooks_conditionals.html#the-when-statement
- 目錄
- ansible基礎
- ansible簡介
- ansible安裝和測試
- ansible配置文件
- 常用命令
- yaml在ansible中的用法
- inventory
- 變量與facts
- when語句
- handler模塊
- 大雜燴
- ansible模塊
- assert 模塊
- copy模塊
- cron模塊
- debug模塊
- django_manage模塊
- file模塊
- filesystem模塊
- git模塊
- hostname模塊
- lineinfile模塊
- mount模塊
- mysql_user模塊
- mysql_db模塊
- pip模塊
- selinux
- setup模塊
- shell 和 command 模塊
- stat模塊
- supervisorctl
- systemd
- timezone
- unarchive模塊
- user模塊
- wait_for
- yum和service模塊
- 其他模塊或者方法
- setup模塊
- url模塊
- slack 模塊
- pause 模塊
- 其他
- 報錯處理
- playbooks
- 復雜的playbook
- 循環
- roles
- YAML
- jinja2