playbook conditionals

目前ansible的全部conditionals方式都是使用when進行判斷,when的值是一個條件表達式,若是判斷成立這個task就執行某個操做,不成立則不執行或跳過python

  • hosts: test
    tasks:shell

    • name: Host localhost run this task
      debug: msg="{{ ansible_default_ipv4.address }}"
      when: ansible_default_ipv4.address == "172.19.95.175"ide

    • name: memtotal_mb < 1000M and ansible_processor_cores == 1 run this task
      debug: msg="{{ ansible-fqdn }}"
      when: ansible_memtotal_mb < 1000 and ansible_processor_cores == 1this

    • name: all host run this task
      shell: hostname
      register: infodebug

    • name: Hostname is python machie run this task
      debug: msg="{{ ansible_fqdn }}"
      when: info['stdout'] == "cxiong"blog

    • name: Hostname is startswith M run this task
      debug: msg="{{ ansible_fqdn }}"
      when: info['stdout'].startswith('C')
      第1個when是判斷facts信息,條件符合執行
      第2個when判斷ansible_memtotal_mb and ansible_processor_cores信息,2個信息都是int類型,不 須要引號,2個條件表達式用and結合
      第3個when是判斷第3個task的運行結果stdout的值
      第4個when也是判斷第3個task運行結果stdout的值,這裏使用了python的str內置王法startswith,最終會返回True和False

執行結果:
playbook conditionals
第4個若是不跳過:
ok: [192.168.1.1] => {
"msg": "cxiong"
}ip

相關文章
相關標籤/搜索