Skip to content

Commit

Permalink
examples: fix Ansible API example (ansible#51863)
Browse files Browse the repository at this point in the history
Signed-off-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
Akasurde authored and s-hertel committed Feb 12, 2019
1 parent bf20c41 commit 1da5e21
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions examples/scripts/uptime.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -31,10 +31,10 @@ def v2_runner_on_failed(self, result, *args, **kwargs):

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)
Expand All @@ -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
Expand All @@ -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():
Expand Down

0 comments on commit 1da5e21

Please sign in to comment.