如何在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-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()
这可以很好地执行剧本,但不会触发回调.
This executes the playbook fine, but the callback is not triggered.
我猜想在使用python API时,没有考虑我的ansible.cfg
文件吗?
I guess when using the python API, my ansible.cfg
file is not being taken into account?
如何为PlaybookExecutor指定回调插件?
(我发现的大多数文档都适用于版本低于< 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是来自此处.它应该源自ansible.plugins.callback.CallbackBase.
(ResultAccumulator is callback plugin class from here. It should be derived from ansible.plugins.callback.CallbackBase.
这篇关于如何在Ansible 2中将回调插件添加到PlaybookExecutor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!