block将多个任务组成一个块:
我们发现如果使用上述的判断方法加上task来指定动作,判断成功后我们只能执行一个动作,那么如果想要执行多个动作怎么办?
也许我们可以让它进行多次判断:
比如判断条件成立后我们要执行3个动作,就写3次判断,让它每次判断成功后执行一个动作
vim pd14.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "task1"
when: 2 > 1
- debug:
msg: "task3"
when: 2 > 1
- debug:
msg: "task3"
when: 2 > 1
但这样明显很麻烦,而且也不专业,所以此时我们就需要block来帮助我们实现这个需求:
vim pd14.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "task1 not in block"
- block:
- debug:
msg: "task2 in block"
- debug:
msg: "task3 in block"
when: 2 > 1
我们可以看到task1不在block中,也输出了,而block中的两个动作也执行了
block结合rescue实现错误处理功能:
vim pd15.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- shell: 'ls /aaa'
register: return_value
ignore_errors: true
- debug:
msg: "I cought an error"
when: return_value is failed
上述写法是我们之前使用的普通写法,使用when判断错误,而block可以结合rescue来判断错误,无须使用when:
vim pd15.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- block:
- shell: 'ls /aaa'
rescue:
- debug:
msg: "I cought an error"
rescue即如果block中的动作执行失败,则执行rescue中的内容
写法2:当block中有多个动作,而第一个动作就执行失败时,后面的动作不会执行,而是执行rescue中的动作
vim pd15.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- block:
- shell: 'ls /aaa'
- shell: 'ls /opt'
- shell: 'ls /testdir'
rescue:
- debug:
msg: "I caught an error"
写法3:当block中有多个动作,而最后一个动作才执行失败,前面的动作会正常执行,最后执行rescue中的动作
vim pd15.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- block:
- shell: 'ls /opt'
- shell: 'ls /testdir'
- shell: 'ls /aaa'
rescue:
- debug:
msg: "I caught an error"
写法4:当rescue中有多个动作时,如果block中有动作执行失败,则执行rescue中的所有动作
vim pd15.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- block:
- shell: 'ls /opt'
- shell: 'ls /testdir'
- shell: 'ls /aaa'
rescue:
- debug:
msg: "I caught an error1"
- debug:
msg: "I caught an error2"
block+rescue+always:
vim pd16.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- block:
- debug:
msg: 'I execute normally'
- command: /bin/false
- debug:
msg: 'I never ecexute, due to the above task failing'
rescue:
- debug:
msg: 'I cought an error'
- command: /bin/false
- debug:
msg: 'I also never ecexute'
always:
- debug:
msg: 'This always executes'
我们可以看到当令block手动执行失败时,会执行rescue中的动作,而不会执行command后面指定的动作;而rescue失败后也不会执行command后面指定的动作,而不管如何always都会执行!
fail模块:手动报错
vim blk1.yml
---
- hosts: testB
remote_user: root
tasks:
- debug:
msg: "1"
- debug:
msg: "2"
- fail:
- debug:
msg: "3"
- debug:
msg: "4"
我们可以看到输出了1、2后,就报错了;后面的3、4都没有被输出;错误信息为"Failed as requested from task",这是fail模块默认的错误信息,我们也可以指定
写法2:指定错误信息:
vim blk1.yml
---
- hosts: testB
remote_user: root
tasks:
- debug:
msg: "1"
- debug:
msg: "2"
- fail:
msg: "Interrupt running playbook"
- debug:
msg: "3"
- debug:
msg: "4"
fail模块:自定义错误
vim blk2.yml
---
- hosts: testB
remote_user: root
tasks:
- shell: "echo '------error-----'"
register: return_value
- fail:
msg: "running fail"
when: "'error' in return_value.stdout"
- debug:
msg: "I never execute,because the playbook has stopped"
当返回的信息中包含’error’时,则报错,报错信息为running fail
注意:这里的’error’必须带引号,即在使用判断时,条件中包含的字符串要带引号
我们可以看到debug中的内容并没有被输出:
写法2:不符合报错信息时,不会报错,会执行下面的动作
vim blk2.yml
---
- hosts: testB
remote_user: root
tasks:
- shell: "echo '------westos----'"
register: return_value
- fail:
msg: "running fail"
when: "'error' in return_value.stdout"
- debug:
msg: "I never execute,because the playbook has stopped"
failed_when关键字:自定义错误
vim blk2.yml
---
- hosts: testB
remote_user: root
tasks:
- debug:
msg: "I execute normally"
- shell: "echo '----error----'"
register: return_value
failed_when: "'error' in return_value.stdout"
- debug:
msg: "I never execute,because the playbook has stopped"
changed_when关键字:改变状态为changed
vim blk3.yml
---
- hosts: testB
remote_user: root
tasks:
- debug:
msg: "test message"
#changed_when: 2 > 1
我们先看看不加changed_when关键字时的效果:debug模块的状态为ok
加上changed_when关键字时的效果:debug模块的状态为changed
vim blk3.yml
---
- hosts: testB
remote_user: root
tasks:
- debug:
msg: "test message"
changed_when: 2 > 1
由此实验可以看出,changed_when关键字的作用是改变状态为changed。还记得handler模块吗?只有状态为changed的动作,才会执行对于handler模块中的内容。所以此关键字可以和handler模块在必要时结合使用
changed_when关键字:设置为false时,表示改变状态不为changed
vim blk4.yml
---
- hosts: testB
remote_user: root
tasks:
- shell: "ls /opt"
我们先看看不加changed_when关键字时的效果:shell模块的状态为changed
加上changed_when关键字时的效果:debug模块的状态为ok
vim blk4.yml
---
- hosts: testB
remote_user: root
tasks:
- shell: "ls /opt"
changed_when: false