条件语句when
类似于编程语言的if
When语句
有时候用户有可能需满足特定条件才执行某一个特定的步骤。例如,在某个特定版本的系统上装包,或者只在磁盘空间满了的文件系统上执行清理操作。这些操作在Playbook中用when语句实现。
主机为Debian Linux立刻关机
tasks:
- name: "shutdown Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_os_family == "Debian"根据action的执行结果,来决定接下来执行的action。
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远程中的系统变量facts变量作为when的条件,用“|int”还可以转换返回值的类型:
条件表达式
基本款
否定款:
变量定义款
数值表达款
与Include一起用
与Role一起用
Last updated