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