gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:19:51 24 4
gpt4 key购买 nike

在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com