Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ansible inventory to dynamic inventory JSON output, accepts all inventory input formats
import sys
import json
from ansible.parsing.dataloader import DataLoader
try:
from ansible.inventory.manager import InventoryManager
A24 = True
except ImportError:
from ansible.vars import VariableManager
from ansible.inventory import Inventory
A24 = False
loader = DataLoader()
if A24:
inventory = InventoryManager(loader, [sys.argv[1]])
inventory.parse_sources()
else:
variable_manager = VariableManager()
inventory = Inventory(loader, variable_manager, sys.argv[1])
inventory.parse_inventory(inventory.host_list)
out = {'_meta': {'hostvars': {}}}
for group in inventory.groups.values():
out[group.name] = {
'hosts': [h.name for h in group.hosts],
'vars': group.vars,
'children': [c.name for c in group.child_groups]
}
for host in inventory.get_hosts():
out['_meta']['hostvars'][host.name] = host.vars
print(json.dumps(out, indent=4, sort_keys=True))
$ python inventory2json.py ansible/test/integration/inventory
{
"_meta": {
"hostvars": {
"facthost0": {
"ansible_connection": "local",
"ansible_host": "1270.0.0.1"
},
"facthost1": {
"ansible_connection": "local",
"ansible_host": "1270.0.0.1"
},
"facthost2": {
"ansible_connection": "local",
"ansible_host": "1270.0.0.1"
},
"facthost3": {
"ansible_connection": "local",
"ansible_host": "1270.0.0.1"
},
"facthost4": {
"ansible_connection": "local",
"ansible_host": "1270.0.0.1"
},
"facthost5": {
"ansible_connection": "local",
"ansible_host": "1270.0.0.1"
},
"facthost6": {
"ansible_connection": "local",
"ansible_host": "1270.0.0.1"
},
"facthost7": {
"ansible_connection": "local",
"ansible_host": "1270.0.0.1"
},
"facthost8": {
"ansible_connection": "local",
"ansible_host": "1270.0.0.1"
},
"invenoverride": {
"ansible_connection": "local",
"ansible_ssh_host": "127.0.0.1"
},
"localhost": {
"ansible_connection": "local",
"ansible_ssh_host": "127.0.0.1"
},
"testhost": {
"a": 1,
"ansible_connection": "local",
"ansible_ssh_host": "127.0.0.1",
"b": 2,
"c": 3,
"d": 4,
"defaults_file_var_role3": "overridden from inventory",
"role_var_beats_inventory": "should_not_see_this",
"test_hash": {
"host_vars_testhost": "this is in host_vars/testhost"
}
},
"testhost2": {
"ansible_connection": "local",
"ansible_ssh_host": "127.0.0.1"
},
"testhost3": {
"ansible_ssh_host": "127.0.0.3"
},
"testhost4": {
"ansible_ssh_host": "127.0.0.4"
}
}
},
"all": {
"children": [
"ungrouped",
"inven_overridehosts",
"arbitrary_grandparent",
"amazon"
],
"hosts": [],
"vars": {
"a": 999,
"b": 998,
"c": 997,
"d": 996,
"dos": 2,
"etest": "from group_vars",
"extra_var_override": "FROM_INVENTORY",
"inven_var": "inventory_var",
"inventory_beats_default": "narf",
"test_bare": true,
"test_bare_nested_bad": "{{test_bare_var}} == 321",
"test_bare_nested_good": "{{test_bare_var}} == 123",
"test_bare_var": 123,
"test_hash": {
"group_vars_all": "this is in group_vars/all"
},
"tres": 3,
"unicode_host_var": "Caf\u00e9E\u00f1yei",
"uno": 1
}
},
"amazon": {
"children": [],
"hosts": [
"localhost"
],
"vars": {
"ec2_region": "us-east-1",
"ec2_url": "ec2.amazonaws.com"
}
},
"arbitrary_grandparent": {
"children": [
"arbitrary_parent"
],
"hosts": [],
"vars": {
"grandparent_var": 2000,
"groups_tree_var": 3000,
"overridden_in_parent": 2000
}
},
"arbitrary_parent": {
"children": [
"local"
],
"hosts": [],
"vars": {
"groups_tree_var": 4000,
"overridden_in_parent": 1000
}
},
"inven_overridehosts": {
"children": [],
"hosts": [
"invenoverride"
],
"vars": {
"foo": "foo",
"var_dir": "vars"
}
},
"local": {
"children": [],
"hosts": [
"testhost",
"testhost2",
"testhost3",
"testhost4",
"facthost0",
"facthost1",
"facthost2",
"facthost3",
"facthost4",
"facthost5",
"facthost6",
"facthost7",
"facthost8"
],
"vars": {
"groups_tree_var": 5000,
"hash_test": {
"group_vars_local": "this is in group_vars/local"
},
"parent_var": 6000,
"tres": "three"
}
},
"ungrouped": {
"children": [],
"hosts": [],
"vars": {}
}
}
@prado2016

This comment has been minimized.

Copy link

commented Mar 6, 2018

good job with the inventory, it saved me alot of time. thanks

@jbs321

This comment has been minimized.

Copy link

commented Dec 5, 2018

Great work, helped me a lot, Thank you!

@carrascodc

This comment has been minimized.

Copy link

commented Aug 6, 2019

Thank you SO much for this. This helped me and saved me a lot of time.

@zhujinhe

This comment has been minimized.

Copy link

commented Oct 10, 2019

Thank you for your share. helped me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.