老师,您好!
我在ansible中定义了两个任务
执行了脚本后,发现两个任务都执行了,但是callback只返回了最后一个任务的结果,这是为什么呢?该如何解决?
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.playbook.play import Play
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase
#DataLoader()用于加载和解析yaml或json格式的文件,sources传递一个列表(表示从哪读取文件)
#InventoryManager类
loader = DataLoader()
inventory = InventoryManager(loader=loader,sources=['imoocc_hosts'])
#VariableManager类
variable_manager = VariableManager(loader=loader,inventory=inventory)
#Options 执行选项
Options = namedtuple('Options',
['connection',
'remote_user',
'ask_sudo_pass',
'verbosity',
'ack_pass',
'module_path',
'forks',
'become',
'become_method',
'become_user',
'check',
'listhosts',
'listtasks',
'listtags',
'syntax',
'sudo_user',
'sudo',
'diff'])
options = Options(connection='smart',
remote_user=None,
ack_pass=None,
sudo_user=None,
forks=5,
sudo=None,
ask_sudo_pass=False,
verbosity=5,
module_path=None,
become=None,
become_method=None,
become_user=None,
check=False,
diff=False,
listhosts=None,
listtasks=None,
listtags=None,
syntax=None)
#Play 执行对象和模块
play_source = dict(
name = "Ansible Play ad-hoc test",
hosts = '192.168.44.129',
gather_facts = 'no',
tasks = [
dict(action=dict(module='shell', args='touch /tmp/ad_hoc_test1')),
dict(action=dict(module='shell', args='touch /tmp/ad_hoc_test2')),
# dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
]
)
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
class ModelResultsCollector(CallbackBase):
def __init__(self, *args, **kwargs):
#super().__init__(*args, **kwargs)
super(ModelResultsCollector,self).__init__(*args, **kwargs)
self.host_ok = {}
def v2_runner_on_ok(self, result):
#def v2_runner_item_on_ok(self, result):
self.host_ok[result._host.get_name()] = result
callback = ModelResultsCollector()
passwords = dict()
tqm = TaskQueueManager(
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords,
stdout_callback=callback,
)
result = tqm.run(play)
result_raw = {'success':{},'failed':{},'unreachable':{}}
for host,result in callback.host_ok.items():
result_raw['success'][host] = result._result
print(result_raw)
结果输出
{
'success': {
'192.168.44.129': {
'changed': True,
'end': '2019-10-22 03:08:04.834335',
'stdout': '',
'cmd': 'touch /tmp/ad_hoc_test2',
'rc': 0,
'start': '2019-10-22 03:08:04.829248',
'stderr': '',
'delta': '0:00:00.005087',
'invocation': {
'module_args': {
'warn': True,
'executable': None,
'_uses_shell': True,
'_raw_params': 'touch /tmp/ad_hoc_test2',
'removes': None,
'creates': None,
'chdir': None,
'stdin': None
}
},
'warnings': ['Consider using file module with state=touch rather than running touch'],
'_ansible_parsed': True,
'stdout_lines': [],
'stderr_lines': [],
'_ansible_no_log': False,
'failed': False
}
},
'failed': {},
'unreachable': {}
}
【课程升级3.6版本】大牛全面践行DevOps,运维与开发间高效协作
了解课程