2016-11-02 21:47:42 +00:00
|
|
|
#!/usr/bin/env python
|
2017-05-18 07:38:40 +00:00
|
|
|
|
2016-02-17 18:21:12 +00:00
|
|
|
from collections import namedtuple
|
2017-05-18 07:38:40 +00:00
|
|
|
|
|
|
|
from ansible.executor.task_queue_manager import TaskQueueManager
|
2017-10-03 14:59:37 +00:00
|
|
|
from ansible.inventory.manager import InventoryManager
|
2017-05-18 07:38:40 +00:00
|
|
|
from ansible.parsing.dataloader import DataLoader
|
2016-02-17 18:21:12 +00:00
|
|
|
from ansible.playbook.play import Play
|
|
|
|
from ansible.plugins.callback import CallbackBase
|
2017-07-27 08:54:26 +00:00
|
|
|
from ansible.vars.manager import VariableManager
|
2017-05-18 07:38:40 +00:00
|
|
|
|
2012-04-04 14:27:24 +00:00
|
|
|
|
2016-06-29 18:31:25 +00:00
|
|
|
# Create a callback object so we can capture the output
|
2016-02-17 18:21:12 +00:00
|
|
|
class ResultsCollector(CallbackBase):
|
2012-04-04 14:27:24 +00:00
|
|
|
|
2016-02-17 18:21:12 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ResultsCollector, self).__init__(*args, **kwargs)
|
2017-05-18 07:38:40 +00:00
|
|
|
self.host_ok = {}
|
2016-02-17 18:21:12 +00:00
|
|
|
self.host_unreachable = {}
|
|
|
|
self.host_failed = {}
|
2012-04-04 14:27:24 +00:00
|
|
|
|
2016-02-17 18:21:12 +00:00
|
|
|
def v2_runner_on_unreachable(self, result):
|
|
|
|
self.host_unreachable[result._host.get_name()] = result
|
2012-04-04 14:27:24 +00:00
|
|
|
|
2017-05-18 07:38:40 +00:00
|
|
|
def v2_runner_on_ok(self, result, *args, **kwargs):
|
2016-02-17 18:21:12 +00:00
|
|
|
self.host_ok[result._host.get_name()] = result
|
2012-04-04 14:27:24 +00:00
|
|
|
|
2017-05-18 07:38:40 +00:00
|
|
|
def v2_runner_on_failed(self, result, *args, **kwargs):
|
2016-02-17 18:21:12 +00:00
|
|
|
self.host_failed[result._host.get_name()] = result
|
2012-04-04 14:27:24 +00:00
|
|
|
|
|
|
|
|
2016-02-17 18:21:12 +00:00
|
|
|
def main():
|
2017-05-18 07:38:40 +00:00
|
|
|
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',
|
2017-10-03 14:59:37 +00:00
|
|
|
'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check',
|
|
|
|
'diff'])
|
2018-01-16 17:39:15 +00:00
|
|
|
# required for
|
|
|
|
# https://github.com/ansible/ansible/blob/devel/lib/ansible/inventory/manager.py#L204
|
|
|
|
sources = ','.join(host_list)
|
|
|
|
if len(host_list) == 1:
|
|
|
|
sources += ','
|
2016-02-17 18:21:12 +00:00
|
|
|
|
|
|
|
# initialize needed objects
|
|
|
|
loader = DataLoader()
|
2018-01-16 17:39:15 +00:00
|
|
|
options = Options(connection='smart', module_path=['/usr/share/ansible'], forks=100,
|
2017-05-18 07:38:40 +00:00
|
|
|
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,
|
2017-10-03 14:59:37 +00:00
|
|
|
become_user=None, verbosity=None, check=False, diff=False)
|
2016-02-17 18:21:12 +00:00
|
|
|
|
|
|
|
passwords = dict()
|
|
|
|
|
|
|
|
# create inventory and pass to var manager
|
2018-01-16 17:39:15 +00:00
|
|
|
inventory = InventoryManager(loader=loader, sources=sources)
|
2017-10-03 14:59:37 +00:00
|
|
|
variable_manager = VariableManager(loader=loader, inventory=inventory)
|
2016-02-17 18:21:12 +00:00
|
|
|
|
|
|
|
# create play with tasks
|
2017-01-11 02:47:03 +00:00
|
|
|
play_source = dict(
|
2017-05-18 07:38:40 +00:00
|
|
|
name="Ansible Play",
|
|
|
|
hosts=host_list,
|
|
|
|
gather_facts='no',
|
|
|
|
tasks=[dict(action=dict(module='command', args=dict(cmd='/usr/bin/uptime')))]
|
2017-01-29 07:28:53 +00:00
|
|
|
)
|
2016-02-17 18:21:12 +00:00
|
|
|
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
|
|
|
|
|
|
|
|
# actually run it
|
|
|
|
tqm = None
|
|
|
|
callback = ResultsCollector()
|
|
|
|
try:
|
|
|
|
tqm = TaskQueueManager(
|
2017-01-29 07:28:53 +00:00
|
|
|
inventory=inventory,
|
|
|
|
variable_manager=variable_manager,
|
|
|
|
loader=loader,
|
|
|
|
options=options,
|
|
|
|
passwords=passwords,
|
|
|
|
)
|
2016-02-17 18:21:12 +00:00
|
|
|
tqm._stdout_callback = callback
|
|
|
|
result = tqm.run(play)
|
|
|
|
finally:
|
|
|
|
if tqm is not None:
|
|
|
|
tqm.cleanup()
|
|
|
|
|
2016-12-06 23:00:35 +00:00
|
|
|
print("UP ***********")
|
2016-02-17 18:21:12 +00:00
|
|
|
for host, result in callback.host_ok.items():
|
2018-01-10 20:03:25 +00:00
|
|
|
print('{0} >>> {1}'.format(host, result._result['stdout']))
|
2016-02-17 18:21:12 +00:00
|
|
|
|
2016-12-06 23:00:35 +00:00
|
|
|
print("FAILED *******")
|
2016-02-17 18:21:12 +00:00
|
|
|
for host, result in callback.host_failed.items():
|
2018-01-10 20:03:25 +00:00
|
|
|
print('{0} >>> {1}'.format(host, result._result['msg']))
|
2016-02-17 18:21:12 +00:00
|
|
|
|
2016-12-06 23:00:35 +00:00
|
|
|
print("DOWN *********")
|
2016-02-17 18:21:12 +00:00
|
|
|
for host, result in callback.host_unreachable.items():
|
2018-01-10 20:03:25 +00:00
|
|
|
print('{0} >>> {1}'.format(host, result._result['msg']))
|
2016-02-17 18:21:12 +00:00
|
|
|
|
2018-07-29 11:46:06 +00:00
|
|
|
|
2016-02-17 18:21:12 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|