判断的关键字是: when
ansible判断用的是jinja2的模板
1.判断数字的大小(大于等于小于)
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{
{item}}"
with_items: [1,2,3]
when: item > 1
2.判断字符串
逻辑与
---
- hosts: testB
remote_user: root
tasks:
- debug:
msg: "System release is Redhat7"
when: ansible_distribution == "RedHat" and ansible_distribution_version == "7.5"
另外一种写法:
---
- hosts: testB
remote_user: root
tasks:
- debug:
msg: "System release is Redhat7"
when:
- ansible_distribution == "RedHat"
- ansible_distribution_version == "7.5"
逻辑或
---
- hosts: testB
remote_user: root
tasks:
- debug:
msg: "System release is Redhat7"
when: ansible_distribution == "RedHat" and (ansible_distribution_version == "7.5" or ansible_distribution_version == "7.3")
逻辑非
---
- hosts: testB
remote_user: root
tasks:
- debug:
msg: "System release is not windows"
when: not ansible_distribution == "Windows"
3.ignore_errors 忽略错误继续执行
---
- hosts: testB
remote_user: root
tasks:
- name: task1
shell: "ls /testaaa"
register: returnmsg
ignore_errors: true 忽略错误依然执行下面的task
- name: task2
debug:
msg: "Command execution successful"
when: returnmsg.rc == 0
- name: task3
debug:
msg: "Command execution failed"
when: returnmsg.rc != 0
---
- hosts: testB
remote_user: root
tasks:
- name: task1
shell: "dateeee"
register: returnmsg
ignore_errors: true
- name: tasks
debug:
var: returnmsg
4.判断文件是否存在(is exists or is not exists or not is exists)
-