ansible-playbook之条件判断
#注:当需要用远程主机的一些信息时,gather_facts必须要开启,默认是开启状态# cat when.yml
---
- hosts: webservers
remote_user: root
#gather_facts: False
tasks:
- name: Host 192.168.1.101 run this task
debug: 'msg=" {{ ansible_default_ipv4.address }}"'
when: ansible_default_ipv4.address == "192.168.2.101"
- name: memtotal < 500M and processor_cores == 2 run this task
debug: 'msg="{{ ansible_fqdn }}"'
when: ansible_memtotal_mb < 500 and ansible_processor_cores == 2
- name: all host run this task
shell: hostname
register: info
- name: Hostname is lamp1 Machie run this task
debug: 'msg="{{ ansible_fqdn }}"'
when: info['stdout'] == "lamp1"
- name: Hostname is startswith l run this task
debug: 'msg="{{ ansible_fqdn }}"'
when: info['stdout'].startswith('l')
# ansible-playbook when.yml
PLAY **************************************************************************************************************************************
TASK *********************************************************************************************************************************
ok:
ok:
TASK ****************************************************************************************************************
ok: => {
"msg": " 192.168.2.101"
}
skipping:
TASK ******************************************************************************************
skipping:
skipping:
TASK **************************************************************************************************************************
changed:
changed:
TASK **********************************************************************************************************
ok: => {
"msg": "lamp1"
}
skipping:
TASK **********************************************************************************************************
ok: => {
"msg": "lamp1"
}
ok: => {
"msg": "lamp2"
}
PLAY RECAP *********************************************************************************************************************************************
192.168.2.101 : ok=5 changed=1 unreachable=0 failed=0
192.168.2.111 : ok=3 changed=1 unreachable=0 failed=0
页:
[1]