mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #14535 from b4ldr/update_uptime_for_api_2
update uptime script to use version 2.0 of the api
This commit is contained in:
commit
9112f5af3a
1 changed files with 80 additions and 23 deletions
|
@ -1,31 +1,88 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
from collections import namedtuple
|
||||||
# example of getting the uptime of all hosts, 10 at a time
|
from ansible.parsing.dataloader import DataLoader
|
||||||
|
from ansible.vars import VariableManager
|
||||||
|
from ansible.inventory import Inventory
|
||||||
|
from ansible.playbook.play import Play
|
||||||
|
from ansible.executor.task_queue_manager import TaskQueueManager
|
||||||
|
from ansible.plugins.callback import CallbackBase
|
||||||
|
|
||||||
import ansible.runner
|
# Creat a callback object so we can capture the output
|
||||||
import sys
|
class ResultsCollector(CallbackBase):
|
||||||
|
|
||||||
# construct the ansible runner and execute on all hosts
|
def __init__(self, *args, **kwargs):
|
||||||
results = ansible.runner.Runner(
|
super(ResultsCollector, self).__init__(*args, **kwargs)
|
||||||
pattern='*', forks=10,
|
self.host_ok = {}
|
||||||
module_name='command', module_args='/usr/bin/uptime',
|
self.host_unreachable = {}
|
||||||
).run()
|
self.host_failed = {}
|
||||||
|
|
||||||
if results is None:
|
def v2_runner_on_unreachable(self, result):
|
||||||
print "No hosts found"
|
self.host_unreachable[result._host.get_name()] = result
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
print "UP ***********"
|
def v2_runner_on_ok(self, result, *args, **kwargs):
|
||||||
for (hostname, result) in results['contacted'].items():
|
self.host_ok[result._host.get_name()] = result
|
||||||
if not 'failed' in result:
|
|
||||||
print "%s >>> %s" % (hostname, result['stdout'])
|
|
||||||
|
|
||||||
print "FAILED *******"
|
def v2_runner_on_failed(self, result, *args, **kwargs):
|
||||||
for (hostname, result) in results['contacted'].items():
|
self.host_failed[result._host.get_name()] = result
|
||||||
if 'failed' in result:
|
|
||||||
print "%s >>> %s" % (hostname, result['msg'])
|
|
||||||
|
|
||||||
print "DOWN *********"
|
|
||||||
for (hostname, result) in results['dark'].items():
|
|
||||||
print "%s >>> %s" % (hostname, result)
|
|
||||||
|
|
||||||
|
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'])
|
||||||
|
|
||||||
|
# initialize needed objects
|
||||||
|
variable_manager = VariableManager()
|
||||||
|
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)
|
||||||
|
|
||||||
|
passwords = dict()
|
||||||
|
|
||||||
|
# create inventory and pass to var manager
|
||||||
|
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list)
|
||||||
|
variable_manager.set_inventory(inventory)
|
||||||
|
|
||||||
|
# create play with tasks
|
||||||
|
play_source = dict(
|
||||||
|
name = "Ansible Play",
|
||||||
|
hosts = host_list,
|
||||||
|
gather_facts = 'no',
|
||||||
|
tasks = [ dict(action=dict(module='command', args=dict(cmd='/usr/bin/uptime'))) ]
|
||||||
|
)
|
||||||
|
play = Play().load(play_source, variable_manager=variable_manager, loader=loader)
|
||||||
|
|
||||||
|
# actually run it
|
||||||
|
tqm = None
|
||||||
|
callback = ResultsCollector()
|
||||||
|
try:
|
||||||
|
tqm = TaskQueueManager(
|
||||||
|
inventory=inventory,
|
||||||
|
variable_manager=variable_manager,
|
||||||
|
loader=loader,
|
||||||
|
options=options,
|
||||||
|
passwords=passwords,
|
||||||
|
)
|
||||||
|
tqm._stdout_callback = callback
|
||||||
|
result = tqm.run(play)
|
||||||
|
finally:
|
||||||
|
if tqm is not None:
|
||||||
|
tqm.cleanup()
|
||||||
|
|
||||||
|
print "UP ***********"
|
||||||
|
for host, result in callback.host_ok.items():
|
||||||
|
print '{} >>> {}'.format(host, result._result['stdout'])
|
||||||
|
|
||||||
|
print "FAILED *******"
|
||||||
|
for host, result in callback.host_failed.items():
|
||||||
|
print '{} >>> {}'.format(host, result._result['msg'])
|
||||||
|
|
||||||
|
print "DOWN *********"
|
||||||
|
for host, result in callback.host_unreachable.items():
|
||||||
|
print '{} >>> {}'.format(host, result._result['msg'])
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue