我想通过执行以下操作来通知我角色中的处理程序:
- name: Notify handler
notify: my_handler
when: this_thing_is_true|bool
但安斯布尔只是抱怨:
错误!在任务中未检测到模块/操作。
我尝试过各种楔子,例如:
- name: Notify handler
meta: noop
notify: my_handler
when: this_thing_is_true|bool
但这同样令人发牢骚:
[警告]:有条件时不支持 noop 任务
有什么建议吗?
@β.εηοιτ.βε 显示了更喜欢debug
的解决方案,但我认为assert
的输出更干净。
debug
代码:
- name: with debug
debug: msg=''
changed_when: true
when: something is changed
notify: 'do stuff'
assert
代码:
- name: with assert
assert: { that: true, quiet: true }
changed_when: true
when: something is changed
notify: 'do stuff'
debug
输出:
任务 [mytask : with debug] ****** 更改: [服务器] => { "味精": " } 运行处理程序 [我的任务:做事] ****** 更改: [服务器]
assert
输出(似乎更干净(:
任务 [mytask : with assert] ****** 更改: [服务器] 运行处理程序 [myhandlers : do stuff] ****** 更改: [服务器]
请注意,运行任务不足以通知处理程序,您还需要一个创建更改结果的任务。
借助 Ansible.
中的changed_when
选项,您可以对任何任务进行更改结果,然后,可以选择执行简单的debug
。
我有其他想法,但最终并没有真正意义:
pause
:但您不能暂停少于一秒钟assert
:但是断言您还需要放入changed_that
以通知处理程序的相同条件感觉很愚蠢。你仍然可以assert: that=true
但感觉同样愚蠢。- 也许我能想到的最愚蠢的想法是
fail
的任务,failed_when: false
。 - 与上述内容相比,
command: 'true'
可能不那么愚蠢,但我并不完全相信,仍然
给定剧本:
- hosts: localhost
gather_facts: no
vars:
this_thing_is_true: true
tasks:
- debug:
msg: 'Notifying handlers'
# var: this_thing_is_true
# ^-- might be an alternative option to msg:
changed_when: this_thing_is_true
notify:
- me
handlers:
- name: me
debug:
msg: 'I have been notified'
回顾一下:
PLAY [localhost] **************************************************
TASK [debug] ******************************************************
changed: [localhost] =>
msg: Notifying handlers
RUNNING HANDLER [me] **********************************************
ok: [localhost] =>
msg: I have been notified
我从这里的两个给定答案中学到了东西。最重要的是,我很好奇为什么你不能总是为了通知而触发更改,然后使用它的语句。经过一些测试,事实证明你可以!\o/
- debug:
msg: Deciding what to do
changed_when: true
notify: "{{ ( res.json == [] ) | ternary( 'on empty object', 'on object exist' ) }}"
如果为 true,则调用处理程序"在空对象上",否则调用"在对象上存在">