1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

examples: fix Ansible API example (#51863)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2019-02-13 03:05:49 +05:30 committed by Sloane Hertel
parent bf20c4126c
commit 1da5e21289

View file

@ -1,13 +1,13 @@
#!/usr/bin/env python
from collections import namedtuple
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader
from ansible.playbook.play import Play
from ansible.plugins.callback import CallbackBase
from ansible.vars.manager import VariableManager
from ansible import context
from ansible.module_utils.common.collections import ImmutableDict
# Create a callback object so we can capture the output
@ -31,10 +31,10 @@ class ResultsCollector(CallbackBase):
def main():
host_list = ['localhost', 'www.example.com', 'www.google.com']
Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'remote_user',
'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check',
'diff'])
# since the API is constructed for CLI it expects certain options to always be set in the context object
context.CLIARGS = ImmutableDict(connection='smart', module_path=['/usr/share/ansible'], forks=10, become=None,
become_method=None, become_user=None, check=False, diff=False)
# required for
# https://github.com/ansible/ansible/blob/devel/lib/ansible/inventory/manager.py#L204
sources = ','.join(host_list)
@ -43,11 +43,6 @@ def main():
# initialize needed objects
loader = DataLoader()
options = Options(connection='smart', module_path=['/usr/share/ansible'], forks=100,
remote_user=None, private_key_file=None, ssh_common_args=None, ssh_extra_args=None,
sftp_extra_args=None, scp_extra_args=None, become=None, become_method=None,
become_user=None, verbosity=None, check=False, diff=False)
passwords = dict()
# create inventory and pass to var manager
@ -71,14 +66,15 @@ def main():
inventory=inventory,
variable_manager=variable_manager,
loader=loader,
options=options,
passwords=passwords,
stdout_callback=callback,
)
tqm._stdout_callback = callback
result = tqm.run(play)
finally:
if tqm is not None:
tqm.cleanup()
if loader:
loader.cleanup_all_tmp_files()
print("UP ***********")
for host, result in callback.host_ok.items():