如何解决如果rc == 0,则运行处理程序播放并退出播放
嗨,有一种方法,我可以运行处理程序播放,如果rc == 0,则退出播放。它只能通过使用fail_when退出播放,如果rc!= 0,则继续播放。我无法进行通知:服务守卫执行。曾经玩过其他方法,例如创建2次播放通知,并且没有运气。
- 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”未定义
错误似乎出在'/home/ansible/linuxpatchingv2/roles/applyPatch/tasks/main.yml'中:第9行,第3列,但可能 根据确切的语法问题放在文件的其他位置。
违规行似乎是:
ignore_errors:是
- 元:end_play ^这里
解决方法
Q:“运行处理程序播放,如果rc == 0,则退出播放”
A:这不可能在一项任务中完成,因为一项任务不能同时是changed
和failed
。这两个动作必须分开。例如,在下面的剧本中,该命令将成功执行,通知处理程序并结束播放
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
如果命令失败,同一本剧本将继续。例如
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。