Created
April 12, 2016 22:42
-
-
Save DavidWittman/189816c1c2b3a25bd1b18f5f9f681262 to your computer and use it in GitHub Desktop.
Script for converting Ansible dynamic inventory to static files. It's not perfect, but it'll get you 90% of the way there.
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 | |
# Converts Ansible dynamic inventory sources to static files | |
# Input is received via stdin from the dynamic inventory file | |
# ex: | |
# ec2.py --list | ansible-dynamic-inventory-converter.py | |
import json | |
import os | |
import sys | |
import pyaml | |
def add_vars(_type, _id, variables): | |
assert _type == "group" or _type == "host" | |
dir_name = "./%s_vars" % _type | |
if not os.path.isdir(dir_name): | |
os.mkdir(dir_name) | |
with open('%s/%s' % (dir_name, _id), 'a') as fh: | |
fh.write(pyaml.dump(variables)) | |
def add_host_vars(host, variables): | |
add_vars('host', host, variables) | |
def add_group_vars(group, variables): | |
add_vars('group', group, variables) | |
def main(): | |
raw_json = sys.stdin.read() | |
inventory = json.loads(raw_json) | |
inventory_filename = "./hosts" | |
with open(inventory_filename, 'a') as fh: | |
for group in inventory: | |
if group == "_meta": | |
for host, variables in inventory[group]["hostvars"].iteritems(): | |
add_host_vars(host, variables) | |
if "vars" in inventory[group]: | |
add_group_vars(group, inventory[group]["vars"]) | |
if "hosts" in inventory[group]: | |
fh.write("[%s]\n" % group) | |
for host in inventory[group]["hosts"]: | |
fh.write("%s\n" % host) | |
fh.write("\n") | |
if "children" in inventory[group]: | |
fh.write("[%s:children]\n" % group) | |
for child in inventory[group]["children"]: | |
fh.write("%s\n" % child) | |
fh.write("\n") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I ended up just dumping the inventory to a plain text file, and then making a 1-line executable bash script that just does
cat inventory.txt
. Since the script is executable, Ansible expects the inventory in dynamic JSON format, and it "just works". I suppose I'm just turning my plain text file into a "static dynamic inventory". Thanks for sharing this script, which was enough to get the creative juices flowing.