如何在Ansible中仅基于条件来通知一个处理程序?

问题描述 投票:0回答:1

bool But Ansible just whines: ERROR! no module

- name: Notify handler
  notify: my_handler
  when: this_thing_is_true|bool

但是Ansible就会抱怨:

"错误!任务中没有检测到模块动作

我尝试了各种楔子,比如:"我的任务中没有检测到模块动作"。

- name: Notify handler
  meta: noop
  notify: my_handler
  when: this_thing_is_true|bool

但同样也会发出警告:"[警告]:

[WARNING]: noop task does not support when conditional 有什么建议吗?

ansible conditional-statements notify
1个回答
1
投票

请注意,仅仅运行一个任务是不够的,你还需要一个任务来创建一个改变的结果。

你可以在任何任务上实现一个改变结果,借助于 changed_when 选项。然后,做一个简单的 debug 可以作为一种选择。

其他的想法我也有,但最后并没有真正的意义。

  • pause但你不能停顿少于一秒钟。
  • assert但是,如果断言同样的条件,你也需要将其列入《公约》,那就感觉很傻。changed_that 来通知处理者。你仍然可以 assert: that: true 但感觉同样很傻。
  • 也许我想到的最愚蠢的想法是一个。fail 担负 failed_when: false.
  • command: 'true' 与上面相比,也许不那么傻,但我并不完全相信,仍然是。

鉴于剧本。

- hosts: local
  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 [local] *******************************************************************

TASK [debug] *******************************************************************
changed: [local] => {
    "msg": "Notifying handlers"
}

RUNNING HANDLER [me] ***********************************************************
ok: [local] => {
    "msg": "I have been notified"
}

PLAY RECAP *********************************************************************
local                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
© www.soinside.com 2019 - 2024. All rights reserved.