标签:src 报错 hosts ble cond 判断 roo 状态 play
想要playbook按照我们的意愿中断任务,可以借助fail模块。
在任务没有设置ignore_errors:true情况下,如果有任务执行失败,playbook会自动停止执行,当fail模块执行后,playbook会认为任务执行失败了,会主动中止任务。
---
- hosts: all
remote_user: root
tasks:
- debug:
msg: "1"
- debug:
msg: "2"
- fail:
- debug:
msg: "3"
- debug:
msg: "4"

我们也可以通过fail模块自带的msg,自定义报错信息,
---
- hosts: test70
remote_user: root
tasks:
- debug:
msg: "1"
- fail:
msg: "Interrupt running playbook"
- debug:
msg: "2"

fail模块通常与when模块结合,如果中断剧本的条件成立,则执行fail模块,中断剧本。
---
- hosts: all
remote_user: root
tasks:
- shell: "echo ‘This is a string for testing--error‘"
register: return_value
- fail:
msg: "Conditions established,Interrupt running playbook"
when: "‘error‘ in return_value.stdout"
- debug:
msg: "I never execute,Because the playbook has stopped"

failed_when也可以完成类似的功能,当条件成立时将对应任务的状态设置为失败,中止剧本。
---
- hosts: all
remote_user: root
tasks:
- debug:
msg: "I execute normally"
- shell: "echo ‘This is a string for testing error‘"
register: return_value
failed_when: ‘ "error" in return_value.stdout‘
- debug:
msg: "I never execute,Because the playbook has stopped"

failed_when关键词的作用是在条件成立时将任务状态改为失败。
changed_when关键词的作用是在条件成立时,将任务状态改为changed。
---
- hosts: all
remote_user: root
tasks:
- debug:
msg: "test message"
changed_when: 2 > 1

当changed_when状态被设置为false时,不管原任务状态为啥,最终都会被设置为ok状态
---
- hosts: all
remote_user: root
tasks:
- shell: "ls /opt"
changed_when: True

标签:src 报错 hosts ble cond 判断 roo 状态 play
原文地址:https://www.cnblogs.com/jinyuanliu/p/10656467.html