ansible条件判断

when


```

[root@localhost ~]#vim test.yml

---

- hosts: B

  remote_user: root

  tasks:

  - debug: msg="system release is centos" #“”不能少

    when: ansible_distribution == "CentOS" #==两端加空格, "CentOS"引号不能少。在when中变量名不用加{{}}

[root@localhost ~]#vim test.yml

---

- hosts: B

  remote_user: root

  tasks:

  - debug: msg={{item}}

    with_items: [1,2,3]

    when: item > 2

运算比较符:

==

!=

>

<

>=

<=

and

or

not

()

---

- hosts: B

  remote_user: root

  tasks:

  - debug:

      msg: "System release is centos6 or centos7"

    when: ansible_distribution == "CentOS" and (ansible_distribution_major_version == "6" or ansible_distribution_major_version == "7")

```

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容