ansible条件判断
在工作中,我们在执行playbook时,需要对某些条件进行判断,只有当满足条件才执行相应的tasks;
1.when条件判断:只条满足when的条件时才执行对应的tasks
注:when关键字后面跟着的是python的表达式,在表达式中你能够使用任何的变量或者facts
#注:当需要用远程主机的一些信息时,gather_facts必须要开启,默认是开启状态[root@nfs-server playbook]# 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')[root@nfs-server playbook]# ansible-playbook when.ymlPLAY [webservers] **************************************************************************************************************************************TASK [Gathering Facts] *********************************************************************************************************************************ok: [192.168.2.101]ok: [192.168.2.111]TASK [Host 192.168.1.101 run this task] ****************************************************************************************************************ok: [192.168.2.101] => { "msg": " 192.168.2.101"}skipping: [192.168.2.111]TASK [memtotal < 500M and processor_cores == 2 run this task] ******************************************************************************************skipping: [192.168.2.101]skipping: [192.168.2.111]TASK [all host run this task] **************************************************************************************************************************changed: [192.168.2.101]changed: [192.168.2.111]TASK [Hostname is lamp1 Machie run this task] **********************************************************************************************************ok: [192.168.2.101] => { "msg": "lamp1"}skipping: [192.168.2.111]TASK [Hostname is startswith l run this task] **********************************************************************************************************ok: [192.168.2.101] => { "msg": "lamp1"}ok: [192.168.2.111] => { "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
2.when条件判断之引用变量
when变量引用错误提示:[WARNING]: when statements should not include jinja2 templating delimiters such as {
{ }} or {% %}.正确的引用方式:将{ {}} or {% %} 改为()
错误写法示例:when: ansible_default_ipv4.address == {
{ ETCD_NODE03 }}正确写法示例:when: ansible_default_ipv4.address == (ETCD_NODE03)
3.changed_when:先执行task,并对task返回的值进行判断,当满足changed_when指定的条件时说明是执行成功的
#注:默认情况下执行了命令的主机状态都为changed,本例对输出进行判断,包含是某个指定字符才能为changed; [root@nfs-server playbook]# cat when_1.yml ---- hosts: webservers remote_user: root #gather_facts: False tasks: - name: all host run this task shell: hostname register: info changed_when: '"lamp1" in info.stdout'[root@nfs-server playbook]# ansible-playbook when_1.ymlPLAY [webservers] **************************************************************************************************************************************TASK [Gathering Facts] *********************************************************************************************************************************ok: [192.168.2.101]ok: [192.168.2.111]TASK [all host run this task] **************************************************************************************************************************ok: [192.168.2.111]changed: [192.168.2.101]PLAY RECAP *********************************************************************************************************************************************192.168.2.101 : ok=2 changed=1 unreachable=0 failed=0 192.168.2.111 : ok=2 changed=0 unreachable=0 failed=0
4.failed_when:当执行失败后,会将信息存在register的stderr中,通过判断指定的字符是否在stderr中来确定是否真的失败;
暂时没有合适的例子,等有了再补~