Skip to content

Instantly share code, notes, and snippets.

@hypergig
Created November 8, 2017 16:16
Show Gist options
  • Save hypergig/37dd0a0d8d8a5f063756352e7fd729e5 to your computer and use it in GitHub Desktop.
Save hypergig/37dd0a0d8d8a5f063756352e7fd729e5 to your computer and use it in GitHub Desktop.
ansible dynamic inventory from environment vars
#!/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