Created
November 8, 2017 16:16
-
-
Save hypergig/37dd0a0d8d8a5f063756352e7fd729e5 to your computer and use it in GitHub Desktop.
ansible dynamic inventory from environment vars
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 | |
''' | |
This file is an ansible dynamic inventory script used to help discover | |
inventory from environment variables | |
''' | |
import json | |
from os import environ | |
def gen_inv(): | |
# look for these words in the environment variable name | |
triggers = ['_SERVER', '_HOST'] | |
# default inventory json | |
inv = { | |
'_meta': { | |
'hostvars': {} | |
} | |
} | |
# discover the inventory | |
[inv.update({k: v.split()}) for k, v in environ.items() if | |
any([t in k for t in triggers])] | |
# return | |
print json.dumps(inv, sort_keys=True, indent=2) | |
if __name__ == '__main__': | |
gen_inv() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment