您如何仅基于条件通知Ansible中的处理程序? [英] How do you notify a handler in Ansible based solely on a conditional?

查看:118
本文介绍了您如何仅基于条件通知Ansible中的处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过以下操作通知角色中的处理程序:

I would like to notify a handler in my role by doing something like this:

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

但是Ansible只是在抱怨:

But Ansible just whines:

错误!在任务中未检测到模块/动作.

ERROR! no module/action detected in task.

我尝试了各种楔子,例如:

I have tried various wedges, such as:

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

但是类似的抱怨:

[警告]:有条件时不支持noop任务有什么建议吗?

[WARNING]: noop task does not support when conditional Any suggestions?

推荐答案

请注意,运行任务不足以通知处理程序,您还需要一个创建更改结果的任务.

Please mind that running a task is not enough for an handler to be notified, you also need a task that creates a changed result.

您可以借助 Ansible中的 changed_when 选项.
然后,执行一个简单的 调试 是一个选择.

You can achieve a change result on any task with the help of the changed_when option in Ansible.
Then, doing a simple debug could be an option.

我有其他想法,但最终并没有真​​正意义:

The other ideas I had, but that did not really made sense in the end:

  • 暂停:但您不能暂停一秒钟以上
  • assert :但是断言与您还需要放入 changed_that 来通知处理程序的相同条件感觉很愚蠢.您仍然可以 assert:that:true ,但感觉同样愚蠢.
  • 也许我能想到的最愚蠢的想法是 fail 任务,而 failed_when:false .
  • 与上述内容相比,
  • 命令:'true'也许不那么愚蠢,但我仍然不完全相信
  • pause: but you cannot pause for less than a second
  • assert: but it feels silly to assert the same condition that you also need to put in the changed_that to notify the handler. You can still assert: that: true but it feels equally silly.
  • Maybe the most silliest of the ideas I could came with was a fail task with a failed_when: false.
  • command: 'true' is maybe less silly, compared to the above, but I am not totally convinced, still

给出剧本:

- 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   

这篇关于您如何仅基于条件通知Ansible中的处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆