1. 直接使用變量
---
- hosts: manager
vars:
enable: true
tasks:
- name: a simple command
debug:
msg: "I am enabled"
when: enable
2. 使用 JinJa2 built-ins
判斷主版本爲4
---
- hosts: manager
vars:
project_version: "4.6.1"
tasks:
- name: a simple command
debug:
msg: {{ project_version }}
when: project_version.split('.')[0] == '4'
判斷存在於
---
- hosts: manager
vars:
enable: true
tasks:
- name: a simple command
shell: echo "ready used"
register: is_ready
- name: condition executed
debug:
msg: "ready is in the output "
when: "'ready' in is_ready.stdout"
判斷文件是否存在:
---
- hosts: manager
vars:
enable: true
tasks:
- name: a simple command
stat: path=/etc/hosts
register: is_exist
- name: condition executed
debug:
msg: "ready is in the output "
when: is_exist.stat.exists == false