Ansible Conditionals 小栗子

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章