ansible - 使用 import_tasks 时如何触发处理程序(通知)?

标签 ansible

在使用 import_tasks 的任务上使用 notify 时,不会触发处理程序。我想知道为什么。 标签 按预期工作。

如何触发导入任务的处理程序?

例子:

剧本测试.yml:

- hosts: all
  gather_facts: no

  handlers:
  - name: restart service
    debug: 
      msg: restart service

  tasks:
  - import_tasks: test_imports.yml
    notify: restart service
    tags: test

测试导入.yml

- name: test
  debug:
    msg: test
  changed_when: yes

- name: test2
  debug:
    msg: test2
  changed_when: yes

预期:

> ansible-playbook -i localhost, test.yml

PLAY [all] *************************************************************************************************************

TASK [test] ************************************************************************************************************
changed: [localhost] => {
    "msg": "test"
}

TASK [test2] ***********************************************************************************************************
changed: [localhost] => {
    "msg": "test2"
}

RUNNING HANDLER [restart service] **************************************************************************************
ok: [localhost] => {
    "msg": "restart service"
}

...

实际:

> ansible-playbook -i localhost, test.yml        

PLAY [all] *************************************************************************************************************

TASK [test] ************************************************************************************************************
changed: [localhost] => {
    "msg": "test"
}

TASK [test2] ***********************************************************************************************************
changed: [localhost] => {
    "msg": "test2"
}

...

最佳答案

此问题已在 Ansible 的错误跟踪器中得到部分解答:

import_tasks is processed at parse time and is effectively replaced by the tasks it imports. So when using import_tasks in handlers you would need to notify the task names within.

来源:mkrizek 的评论:https://github.com/ansible/ansible/issues/59706#issuecomment-515879321


这里也有进一步的解释:

imports do not work like tasks, and they do not have an association between the import_tasks task and the tasks within the imported file really. import_tasks is a pre-processing trigger, that is handled during playbook parsing time. When the parser encounters it, the tasks within are retrieved, and inserted where the import_tasks task was located. There is a proposal to "taskify" includes at ansible/proposals#136 but I don't see that being implemented any time soon.

来源:sivel 评论:https://github.com/ansible/ansible/issues/64935#issuecomment-573062042


对于它的大部分,它似乎最近已被修复:https://github.com/ansible/ansible/pull/73572


但是,就目前而言,可行的方法是:

- hosts: all
  gather_facts: no

  handlers:
  - name: restart service
    debug: 
      msg: restart service

  tasks:
  - import_tasks: test_imports.yml
    tags: test

还有一个 test_imports.yml 文件,例如:

- name: test
  debug:
    msg: test
  changed_when: yes
  notify: restart service

- name: test2
  debug:
    msg: test2
  changed_when: yes
  notify: restart service

这一切产生:

PLAY [all] *******************************************************************************************************

TASK [test] ******************************************************************************************************
changed: [localhost] => 
  msg: test

TASK [test2] *****************************************************************************************************
changed: [localhost] => 
  msg: test2

RUNNING HANDLER [restart service] ********************************************************************************
ok: [localhost] => 
  msg: restart service

PLAY RECAP *******************************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

然后,如果你想在某个地方导入这些任务,这个处理程序没有定义,你可以使用环境变量 ERROR_ON_MISSING_HANDLER 这可以帮助您将缺少处理程序时抛出的错误转换为“简单”警告。

例如

$ ANSIBLE_ERROR_ON_MISSING_HANDLER=false ansible-playbook play.yml -i inventory.yml

PLAY [all] *******************************************************************************************************

TASK [test] ******************************************************************************************************
[WARNING]: The requested handler 'restart service' was not found in either the main handlers list nor in the
listening handlers list
changed: [localhost] => 
  msg: test

TASK [test2] *****************************************************************************************************
changed: [localhost] => 
  msg: test2

PLAY RECAP *******************************************************************************************************
localhost                  : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

关于ansible - 使用 import_tasks 时如何触发处理程序(通知)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66870931/

相关文章:

linux - ansible 仅在主机重新启动时才执行其他操作

Ansible systemd 挂载依赖

ansible - 如何在 Ansible 中将文件内容复制到另一个文件而不覆盖第二个文件

bash - Ansible 输出格式化选项

ansible - 我怎样才能减慢ansible的速度?

ubuntu-12.04 - 在ansible playbook中使用sudo权限执行任务

grep - 如何检查 Ansible 的命令输出中是否存在字符串列表?

amazon-web-services - 用于将 IAM 角色附加到现有 EC2 实例的 Ansible 模块

Ansible 变量名 `environment` 被保留?

ansible - 是否可以使用内联模板?