目前ansible的所有conditionals方式都是使用when进行判断,when的值是一个条件表达式,如果判断成立这个task就执行某个操作,不成立则不执行或跳过

  • hosts: test
    tasks:

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

    • 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 == 1

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

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

    • 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"
}

转载于:https://blog.51cto.com/yangxiongchun/2072102

相关文章:

  • 2021-07-05
  • 2019-11-22
  • 2019-11-22
  • 2021-08-13
  • 2021-12-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
  • 2021-09-14
  • 2021-08-15
  • 2022-01-04
相关资源
相似解决方案