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

相关文章
相关标签/搜索