ansible条件判断when

时间:2023-02-19 13:06:18

一、概述

在日常运维工作中,在有的时候ansble-playbook的结果依赖于变量、fact或者是前一个任务的执行结果,从而需要使用到条件语句。
使用ansible-playbook时,可能需要对某些条件进行判断,只有当满足条件才执行相应的tasks。

二、判断

1、when条件判断

1、when条件判断:只条满足when的条件时才执行对应的tasks
需要注意:when关键字后面跟着的是python的表达式,在表达式中我们能够使用任何的变量或者facts。
另外注意:当需要用远程主机的一些信息时,gather_facts必须要开启,默认是开启状态!!!!!
- hosts: kevin_server
remote_user: root
gather_facts: True
tasks:
- name: Host 172.16.60.242 run this task
debug: 'msg=" {{ ansible_default_ipv4.address }}"'
when: ansible_default_ipv4.address == "172.16.60.242"

- 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 webserver01 Machie run this task
debug: 'msg="{{ ansible_fqdn }}"'
when: info['stdout'] == "webserver01"

- name: Hostname is startswith l run this task
debug: 'msg="{{ ansible_fqdn }}"'
when: info['stdout'].startswith('l')

2、when条件判断之引用变量
when变量引用错误提示:[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}.
正确的引用方式:将{{}} or {% %} 改为()
错误写法示例:when: ansible_default_ipv4.address == {{ webserver01 }}
正确写法示例:when: ansible_default_ipv4.address == (webserver01)
- hosts: kevin_server
remote_user: root
gather_facts: True
tasks:
- name: Host 192.168.1.101 run this task
#debug: 'msg=" {{ ansible_default_ipv4.address }}"'
shell: hostname
when: ansible_default_ipv4.address == (webserver02)

2、changed_when

先执行task,并对task返回的值进行判断,当满足changed_when指定的条件时说明是执行成功的
需要注意:默认情况下执行了命令的主机状态都为changed,本例对输出进行判断,包含是某个指定字符才能为changed;
- hosts: kevin_server
remote_user: root
gather_facts: True
tasks:
- name: all host run this task
shell: hostname
register: info
changed_when: '"webserver01" in info.stdout'

3、failed_when

failed_when:当执行失败后,会将信息存在register的stderr中,通过判断指定的字符是否在stderr中来确定是否真的失败;
- hosts: kevin_server
remote_user: root
gather_facts: True
tasks:
- name: this command prints FAILED when it fails
command: echo "FAILED"
register: command_result
failed_when: "'FAILED' in command_result.stdout"

- name: this is a test
shell: echo "haha"
可以看出,第一个task任务的failed_when已经满足了,所以就此停止playbook的运行了,下面的task任务也不会执行了!

failed_when其实是ansible的一种错误处理机制,是由fail模块使用了when条件语句的组合效果。
所以,上面的配置也可以调整成下面写法(上面第一个task可以调整为下面第1和第2个task的写法,是一样的效果):
- hosts: kevin_server
remote_user: root
gather_facts: True

tasks:
- name: this command prints FAILED when it fails
command: echo "FAILED"
register: command_result

- name: fail the play if the previous command did not succeed
fail: msg="the command failed"
when: "'FAILED' in command_result.stdout"

- name: this is a test
shell: echo "haha"
这里就可以看出"failed_when"的作用,它的作用就是当failed_when关键字对应的条件成立时,failed_when会将对应的任务的执行状态设置为失败,以停止playbook的运行!
但是需要注意的时:failed_when虽然会将任务的执行状态设置为失败,但是它并不代表任务真的失败了!就以上面例子来说,上面的command模块的确时完全正常的执行了,
只不过在执行之后,failed_when对应的条件成立了,failed_when将command模块的执行状态设置为失败而已!所以,failed_when并不会影响command模块的执行过程,
只会在条件成立时影响command模块最终的执行状态,以便于停止playbook的运行。

因此需要注意:
failed_when:关键字的作用是在条件成立时,将对应任务的执行状态设置为失败!
changed_when:关键字的作用是在条件成立时,将对应任务的执行状态设置为changed!