ansible-playbook之条件判断
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible-playbook之条件判断相关的知识,希望对你有一定的参考价值。
ansible条件判断
在工作中,我们在执行playbook时,需要对某些条件进行判断,只有当满足条件才执行相应的tasks;
1.when条件判断:只条满足when的条件时才执行对应的tasks
注:when关键字后面跟着的是python的表达式,在表达式中你能够使用任何的变量或者facts
#注:当需要用远程主机的一些信息时,gather_facts必须要开启,默认是开启状态 [[email protected] 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‘) [[email protected] playbook]# ansible-playbook when.yml PLAY [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.changed_when:先执行task,并对task返回的值进行判断,当满足changed_when指定的条件时说明是执行成功的
#注:默认情况下执行了命令的主机状态都为changed,本例对输出进行判断,包含是某个指定字符才能为changed; [[email protected] 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‘ [[email protected] playbook]# ansible-playbook when_1.yml PLAY [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
3.failed_when:当执行失败后,会将信息存在register的stderr中,通过判断指定的字符是否在stderr中来确定是否真的失败;
暂时没有合适的例子,等有了再补~
本文出自 “激情燃烧的岁月” 博客,请务必保留此出处http://liuzhengwei521.blog.51cto.com/4855442/1962382
以上是关于ansible-playbook之条件判断的主要内容,如果未能解决你的问题,请参考以下文章