Last active
May 11, 2016 07:09
-
-
Save oreh/9dee21c4053207aab8d3ebfe2d1c7059 to your computer and use it in GitHub Desktop.
Dummy example to write an ansible dynamic inventory in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import re | |
from optparse import OptionParser | |
import json | |
hosts = { | |
'all': { | |
'hosts': ['node0', 'node1', 'node2'], | |
'vars' : { | |
'owner': 'me', | |
'project': 'dummy' | |
} | |
} | |
} | |
def get_hosts(): | |
return hosts | |
def pick_host(hosts, name): | |
for g in hosts: | |
if name in hosts[g]['hosts']: | |
return hosts[g]['vars'] | |
if __name__ == "__main__": | |
parser = OptionParser() | |
parser.add_option( | |
"--list", | |
action="store_true", | |
dest="list_host", | |
default=None, | |
help="list hosts" | |
) | |
parser.add_option( | |
"--host", | |
dest="host_name", | |
help="get info about a HOST", | |
metavar="HOST" | |
) | |
(options, args) = parser.parse_args() | |
hosts = get_hosts() | |
if options.host_name: | |
print(pick_host(hosts, options.host_name)) | |
sys.exit(0) | |
if options.list_host: | |
print(json.dumps(hosts)) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment