如果rc == 0,则运行处理程序播放并退出播放 [英] Run a handler play and exit play if rc == 0 in ansible
问题描述
有一种方法,我可以运行处理程序播放,如果rc == 0,则退出播放.只能通过使用fail_when退出播放,如果rc!= 0,则继续播放.我无法进行通知:服务守卫执行.曾经玩过其他方法,例如创建2次播放通知,并且没有运气.
Hi is there a way I can run a handler play then exit the play if rc == 0. It can only do is exit the play using failed_when and proceed if rc != 0. I can't make notify: Service Guard execute. Been play with other approach like creating 2 play notify and exit no luck.
- name: Exit SG server from play
command: /usr/local/cmcluster/bin/cmversion
register: sg_check
notify: Service Guard
failed_when: sg_check.rc == 0
这是我尝试过的新代码
- name: Check if Service Gurad then exit
command: /usr/local/cmcluster/bin/cmversion
register: sg_check
notify: Service Guard
changed_when: sg_check.rc == 0
ignore_errors: true
- meta: end_play
when: sg_check.rc == 0
但是我明白了:
错误!条件检查"sg_check.rc == 0"失败.错误是:评估条件(sg_check.rc == 0)时出错:"sg_check"未定义
ERROR! The conditional check 'sg_check.rc == 0' failed. The error was: error while evaluating conditional (sg_check.rc == 0): 'sg_check' is undefined
错误似乎出在'/home/ansible/linuxpatchingv2/roles/applyPatch/tasks/main.yml'中:第9行,第3列,但可能根据确切的语法问题放在文件的其他位置.
The error appears to have been in '/home/ansible/linuxpatchingv2/roles/applyPatch/tasks/main.yml': line 9, column 3, but may be elsewhere in the file depending on the exact syntax problem.
违规行似乎是:
ignore_errors:是
ignore_errors: true
- 元数据:end_play^这里
推荐答案
Q:"运行处理程序播放,如果rc == 0,则退出播放"
A:这不可能在一项任务中完成,因为不能同时更改更改
和 failed
一项任务.这两个动作必须分开.例如,在下面的剧本中,该命令将成功执行,通知处理程序并结束播放
A: This is not possible to accomplish in one task because a task can't be both changed
and failed
at the same time. These two actions must be split. For example, in the playbook below the command will succeed, notify the handler, and end the play
shell> cat pb.yml
- hosts: localhost
gather_facts: false
tasks:
- command: "{{ cmd|default('/bin/true') }}"
register: sg_check
notify: Service Guard
changed_when: sg_check.rc == 0
ignore_errors: true
- meta: end_play
when: sg_check.rc == 0
- debug:
msg: Continue
handlers:
- name: Service Guard
debug:
msg: Service Guard notified
给予(删节)
shell> ansible-playbook pb.yml
...
RUNNING HANDLER [Service Guard] ****
ok: [localhost] =>
msg: Service Guard notified
PLAY RECAP ****
localhost: ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
如果命令失败,则同一本剧本将继续.例如
The same playbook will continue if the command fails. For example
shell> ansible-playbook pb.yml -e "cmd=/bin/false"
PLAY [localhost] ****
TASK [command] ****
fatal: [localhost]: FAILED! => changed=false
cmd:
- /bin/false
delta: '0:00:00.003035'
end: '2020-08-24 09:33:22.039762'
msg: non-zero return code
rc: 1
start: '2020-08-24 09:33:22.036727'
stderr: ''
stderr_lines: <omitted>
stdout: ''
stdout_lines: <omitted>
...ignoring
TASK [debug] ****
ok: [localhost] =>
msg: Continue
PLAY RECAP ****
localhost: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
这篇关于如果rc == 0,则运行处理程序播放并退出播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!