使用 Ansible Python API,我如何才能访问我的代码中的任务级输出?
Using the Ansible Python API, how can I get access to task level outputs in my code?
我像这样使用 playbook runner:
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=1)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=1)
pb = ansible.playbook.PlayBook(
... # basic info
)
results = pb.run()
结果变量包含如下输出:
{"status": 1, "result": {"127.0.0.1": {"unreachable": 0, "skipped": 0, "ok": 3, "changed": 2, "failures": 0}}}
很好。但我还需要任务级别的输出,如下所示:
changed: [127.0.0.1] => {"changed": true, "name": "apache2", "state": "started"}
我尝试更改详细程度,但这不是我想要的。
在四处挖掘之后,我设法将输出输出到日志文件中,如下所示:
from ansible import constants as C
C.DEFAULT_LOG_PATH = 'project.log'
reload(callbacks)
但我需要在我的代码中访问它。
您可能无法完全获得所需的 json 格式,但您可以通过扩展回调 classes 并为您感兴趣的事件编写自己的处理程序来获得相同的信息.
还要确保像这样将 classes 提供给 Playbook 实例:
pb = ansible.playbook.PlayBook(
... # basic info
callbacks= playbook_cb,
runner_callbacks=runner_cb
)
链接:
编辑:
在这种情况下,解决方案是改为扩展 AggregateStat class。 @Tom_Sawyyer
提供的以下代码示例
from ansible.callbacks import AggregateStats
class CustomAggregateStats(AggregateStats):
"""
Holds stats about per-host activity during playbook runs.
"""
def __init__(self):
super(CustomAggregateStats, self).__init__()
self.results = {}
def compute(self, runner_results, setup=False, poll=False,
ignore_errors=False):
"""
Walk through all results and increment stats.
"""
super(CustomAggregateStats, self).compute(runner_results, setup, poll,
ignore_errors)
for (host, value) in runner_results.get('contacted', {}).iteritems():
if 'invocation' in value:
if value['invocation']['module_name'] == 'service':
self.results['service_name'] = value['name']
def summarize(self, host):
"""
Return information about a particular host
"""
summarized_info = super(CustomAggregateStats, self).summarize(host)
# Adding the info I need
summarized_info['result'] = self.results
return summarized_info
最后:
my_stats = CustomAggregateStats()
pb = ansible.playbook.PlayBook(
... # basic info
stats=my_stats,
)
给出输出:
{"127.0.0.1": {"skipped": 0, "ok": 3, "changed": 2, "result": {"service_name": "apache2"}, "failures": 0, "unreachable": 0}}
我像这样使用 playbook runner:
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=1)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=1)
pb = ansible.playbook.PlayBook(
... # basic info
)
results = pb.run()
结果变量包含如下输出:
{"status": 1, "result": {"127.0.0.1": {"unreachable": 0, "skipped": 0, "ok": 3, "changed": 2, "failures": 0}}}
很好。但我还需要任务级别的输出,如下所示:
changed: [127.0.0.1] => {"changed": true, "name": "apache2", "state": "started"}
我尝试更改详细程度,但这不是我想要的。
在四处挖掘之后,我设法将输出输出到日志文件中,如下所示:
from ansible import constants as C
C.DEFAULT_LOG_PATH = 'project.log'
reload(callbacks)
但我需要在我的代码中访问它。
您可能无法完全获得所需的 json 格式,但您可以通过扩展回调 classes 并为您感兴趣的事件编写自己的处理程序来获得相同的信息.
还要确保像这样将 classes 提供给 Playbook 实例:
pb = ansible.playbook.PlayBook(
... # basic info
callbacks= playbook_cb,
runner_callbacks=runner_cb
)
链接:
编辑: 在这种情况下,解决方案是改为扩展 AggregateStat class。 @Tom_Sawyyer
提供的以下代码示例from ansible.callbacks import AggregateStats
class CustomAggregateStats(AggregateStats):
"""
Holds stats about per-host activity during playbook runs.
"""
def __init__(self):
super(CustomAggregateStats, self).__init__()
self.results = {}
def compute(self, runner_results, setup=False, poll=False,
ignore_errors=False):
"""
Walk through all results and increment stats.
"""
super(CustomAggregateStats, self).compute(runner_results, setup, poll,
ignore_errors)
for (host, value) in runner_results.get('contacted', {}).iteritems():
if 'invocation' in value:
if value['invocation']['module_name'] == 'service':
self.results['service_name'] = value['name']
def summarize(self, host):
"""
Return information about a particular host
"""
summarized_info = super(CustomAggregateStats, self).summarize(host)
# Adding the info I need
summarized_info['result'] = self.results
return summarized_info
最后:
my_stats = CustomAggregateStats()
pb = ansible.playbook.PlayBook(
... # basic info
stats=my_stats,
)
给出输出:
{"127.0.0.1": {"skipped": 0, "ok": 3, "changed": 2, "result": {"service_name": "apache2"}, "failures": 0, "unreachable": 0}}