Skip to content

Instantly share code, notes, and snippets.

@nicholaskuechler
Created October 4, 2013 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicholaskuechler/6827654 to your computer and use it in GitHub Desktop.
Save nicholaskuechler/6827654 to your computer and use it in GitHub Desktop.
Ansible - run command on a single host (10.20.30.40)
#!/usr/bin/python
import ansible.runner
import sys
# construct the ansible runner and execute on all hosts
results = ansible.runner.Runner(
host_list=['10.20.30.40'],
pattern='*',
forks=10,
module_name='command',
module_args='/usr/bin/uptime',
).run()
if results is None:
print "No hosts found"
sys.exit(1)
print "UP ***********"
for (hostname, result) in results['contacted'].items():
if not 'failed' in result:
print "%s >>> %s" % (hostname, result['stdout'])
print "FAILED *******"
for (hostname, result) in results['contacted'].items():
if 'failed' in result:
print "%s >>> %s" % (hostname, result['msg'])
print "DOWN *********"
for (hostname, result) in results['dark'].items():
print "%s >>> %s" % (hostname, result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment