如何在 Ansible 2 中向 PlaybookExecutor 添加回调插件 [英] How to add a callback plugin to a PlaybookExecutor in Ansible 2
问题描述
如何在通过其 API 调用 ansible 时指定回调?
How to specify a callback when calling ansible via its API?
我有一个用于 ansible 2.0.0.2 的回调插件 database_write.py
,它在运行时登录到数据库:
I have a callback plugin database_write.py
for ansible 2.0.0.2 that logs into a database when this is run:
ansible-playbook -i inventory.txt playbook.yml # callback is fired ok
这行得通,因为在我的 $PWD 我有 ansible.cfg
与这一行:
This works ok because in my $PWD i have ansible.cfg
with this line:
callback_plugins = ./src/callback
现在我正在尝试使用 python API 使 ansible 执行我的剧本和我的回调.我基本上复制了 ansible-playbook cli 工具的功能
Now I'm trying to make ansible to execute my playbook and my callback using the python API. I've basically copied what the ansible-playbook cli tool does
# based on https://github.com/ansible/ansible/blob/v2.0.0.2-1/lib/ansible/cli/playbook.py
pbex = PlaybookExecutor(playbooks=['../playbook.yml'],
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords)
results = pbex.run()
这很好地执行了剧本,但没有触发回调.
我猜在使用 python API 时,我的 ansible.cfg
文件没有被考虑在内?
I guess when using the python API, my ansible.cfg
file is not being taken into account?
如何将我的回调插件指定给 PlaybookExecutor?
(我发现的大部分文档都适用于 ansible 版本 < 2.0)
(most of the documentation I've found works for ansible versions < 2.0)
先谢谢你!
推荐答案
2.0 API 非常原始,通常不适合最终用户.将来也可能会改变.请参阅邮件列表上的此讨论,在那里我发布了类似的问题并提出了我自己的答案显然是正确的:您可以将回调分配给执行程序的 TaskQueueManager 实例 (PlaybookExecutor._tqm._stdout_callback).
2.0 API is very raw and generally not suited for end-user. It's likely to be changed in the future also. See this discussion on mailing list where I posted similar question and proposed my own answer that apparently was correct: you can assign your callbacks to TaskQueueManager instance of the executor (PlaybookExecutor._tqm._stdout_callback).
pbex = PlaybookExecutor(playbooks=playbooks, inventory=inventory,
variable_manager=variable_manager,
loader=loader, options=options,
passwords=passwords)
cb = ResultAccumulator()
pbex._tqm._stdout_callback = cb
results = pbex.run()
(ResultAccumulator 是来自这里的回调插件类a>. 应该是从 ansible.plugins.callback.CallbackBase 派生出来的.
(ResultAccumulator is callback plugin class from here. It should be derived from ansible.plugins.callback.CallbackBase.
这篇关于如何在 Ansible 2 中向 PlaybookExecutor 添加回调插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!