Skip to content

Instantly share code, notes, and snippets.

@sivel
Last active December 19, 2023 01:54
Show Gist options
  • Save sivel/3c0745243787b9899486 to your computer and use it in GitHub Desktop.
Save sivel/3c0745243787b9899486 to your computer and use it in GitHub Desktop.

Revisions

  1. sivel revised this gist Jun 19, 2017. No changes.
  2. sivel revised this gist Jun 19, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion output.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    $ python inventory2json.py ansible/test/integration/inventory
    $ python inventory2json.py ansible/test/integration/inventory
    {
    "_meta": {
    "hostvars": {
  3. sivel revised this gist Jun 19, 2017. 1 changed file with 15 additions and 7 deletions.
    22 changes: 15 additions & 7 deletions inventory2json.py
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,24 @@
    #!/usr/bin/env python

    import sys
    import json

    from ansible.parsing.dataloader import DataLoader
    from ansible.inventory import Inventory
    from ansible.vars import VariableManager

    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()
    variable_manager = VariableManager()
    inventory = Inventory(loader, variable_manager, sys.argv[1])
    inventory.parse_inventory(inventory.host_list)
    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():
  4. sivel revised this gist May 8, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion inventory2json.py
    Original file line number Diff line number Diff line change
    @@ -22,4 +22,4 @@
    for host in inventory.get_hosts():
    out['_meta']['hostvars'][host.name] = host.vars

    print json.dumps(out, indent=4, sort_keys=True)
    print(json.dumps(out, indent=4, sort_keys=True))
  5. sivel revised this gist Mar 31, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions inventory2json.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    #!/usr/bin/env python

    import sys
    import json

  6. sivel revised this gist Mar 31, 2017. No changes.
  7. sivel revised this gist Mar 31, 2017. 2 changed files with 1 addition and 1 deletion.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion output.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    $ python static2json.py ansible/test/integration/inventory
    $ python inventory2json.py ansible/test/integration/inventory
    {
    "_meta": {
    "hostvars": {
  8. sivel revised this gist Mar 31, 2017. 2 changed files with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion output.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    $ python ini2json.py ansible/test/integration/inventory
    $ python static2json.py ansible/test/integration/inventory
    {
    "_meta": {
    "hostvars": {
    File renamed without changes.
  9. sivel created this gist Mar 22, 2016.
    23 changes: 23 additions & 0 deletions ini2json.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import sys
    import json

    from ansible.parsing.dataloader import DataLoader
    from ansible.inventory import Inventory
    from ansible.vars import VariableManager

    loader = DataLoader()
    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)
    176 changes: 176 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,176 @@
    $ python ini2json.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": {}
    }
    }