如果 rc == 0 在 ansible 中运行处理程序播放并退出播放 [英] Run a handler play and exit play if rc == 0 in ansible
问题描述
有没有一种方法可以运行处理程序播放,然后在 rc == 0 时退出播放.它只能使用 failed_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.
违规行似乎是:
忽略错误:正确
- 元:end_play^ 这里
推荐答案
问:如果 rc == 0,则运行处理程序播放然后退出播放"
A:这是不可能在一个任务中完成的,因为一个任务不能同时更改
和失败
.这两个动作必须分开.比如下面的playbook中命令会成功,通知handler,结束播放
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 在 ansible 中运行处理程序播放并退出播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!