From 1da5e21289d7407925190139165a9cd7a3f69960 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 13 Feb 2019 03:05:49 +0530 Subject: [PATCH] examples: fix Ansible API example (#51863) Signed-off-by: Abhijeet Kasurde --- examples/scripts/uptime.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/examples/scripts/uptime.py b/examples/scripts/uptime.py index deef4481b28..507e384ff83 100755 --- a/examples/scripts/uptime.py +++ b/examples/scripts/uptime.py @@ -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():