ansible-playbook之条件判断

ansible条件判断

在工作中,我们在执行playbook时,需要对某些条件进行判断,只有当满足条件才执行相应的tasks;

1.when条件判断:只条满足when的条件时才执行对应的tasks

注:when关键字后面跟着的是python的表达式,在表达式中你能够使用任何的变量或者facts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#注:当需要用远程主机的一些信息时,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指定的条件时说明是执行成功的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#注:默认情况下执行了命令的主机状态都为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

ansible-playbook之条件判断

3.failed_when:当执行失败后,会将信息存在register的stderr中,通过判断指定的字符是否在stderr中来确定是否真的失败;

暂时没有合适的例子,等有了再补~

本文转自激情燃烧的岁月博客51CTO博客,原文链接http://blog.51cto.com/liuzhengwei521/1962382如需转载请自行联系原作者

weilovepan520