Dynamic Inventory Script - Groups in Groups

Hello,

I am running into a problem with trying to create Dynamic Inventories via a Python Script, when the structure has Groups that belong to Groups.

If I just have a list of servers in each group, I do not have any problems.

Here is an INI example of what I need to create:

For example, here is what the INI version of my inventory would look likeL
[cell_test_1]
unix1
unix9

[cell_test_2]
unix2
unix10

[Test:children]
cell_test_1
cell_test_2

My script is able to generate either yaml or jason.

The output or each look like:
YAML
Test:
children:
cell_test_1:
hosts:

  • unix1
  • unix9
    cell_test_2:
    hosts:
  • unix2
  • unix10

JSON{“test”: {“children”: {“cell_test_1”: {“hosts”: [“unix1”, “unix9”]}, “cell_test_2”: {“hosts”: [“unix2”, “unix10”]}}, “vars”: null}}

Feeding in either into Tower produces the output of:
2.060 INFO Updating inventory 94: xx_Test_Inventory

2

2.112 DEBUG Using system install of ansible-inventory CLI: /usr/bin/ansible-inventory

3
2.112 INFO Reading Ansible inventory source: /tmp/awx_45115_aZ12p6/tmpYrrkcr

4
2.123 DEBUG Using provided directory ‘/tmp/awx_45115_aZ12p6’ for isolation.

5
2.123 DEBUG Running from /tmp/awx_45115_aZ12p6 working directory.

6
3.154 INFO Processing JSON output…

7
3.154 DEBUG Loaded group: all

8
3.154 DEBUG Loaded group: Test

9
3.155 DEBUG Adding child group Test to parent all

10
3.155 DEBUG Loaded group: cell_test_1

11
3.155 DEBUG Adding child group cell_test_1 to parent Test

12
3.155 DEBUG Loaded group: cell_test_2

13
3.155 DEBUG Adding child group cell_test_2 to parent Test

14
3.155 DEBUG Adding child group Test to parent all

15
3.155 DEBUG Adding child group Test to parent all

16
3.155 DEBUG Finished loading from source: /tmp/awx_45115_aZ12p6/tmpYrrkcr

17
3.156 DEBUG Removing empty group cell_test_1

18
3.156 DEBUG Removing empty group cell_test_2

19
3.156 INFO Loaded 1 groups, 0 hosts

20
3.324 DEBUG Group “test” deleted

21
3.330 DEBUG Inventory variables unmodified

22
3.364 DEBUG Group “Test” added

23
3.485 INFO Inventory import completed for AJ_Test_WAS_Inventory in 1.4s

Has anyone else run into this problem?

Can I get help as to what either format (YAML or JSON) should look like to get the hosts to be added to the cell_test_* groups?

Thanks much!

The format is in the documentation
https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html

I don’t mean to be dumb, but I read the document page you indicated, and the example formats I have provide appear to me to fit into what is says should work, yet they are not.

This is why I posted the question.

Thanks!

Most people posting on the list don't read documentation, so I though this was the case now also. sorry about that.

Since you have a ini file, put that in a file called inv.yml and then run
  ansible-inventory -i inv.yml --list

Then you'll getting the output Ansible expect from a inventory script.

Normally, I would.

However, for this specific case of our, we are reading a database to generate the list of servers, so to prevent having to maintain the list of servers in 2 places, we are using a script to read the database and generate the JSON output.

Thanks!

You wanted to know how the JSON output should be for your ini example.
The method I posted will give you that output.

So when you have the correct output you can recreate that in your script.
I did not mean you should abandon your script and go for ini file.

FYI

I finally figured out a way to handle the structure.

A big note, for me, when I created a JSON item, if I ran it through a ‘json.dumps’ command, to validate, it would actually create an object that Tower did not like. However, if I just created a JSON string that was fine.

Thanks!