# Dynamic Inventory Script - Groups in Groups

**URL:** <https://forum.ansible.com/t/dynamic-inventory-script-groups-in-groups/31074>\
**Category:** Ansible Project\
**Created:** [August 19, 2019, 6:12pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-groups-in-groups/31074 "2019-08-19T18:12:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_Drew](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@A\_Drew](https://forum.ansible.com/u/A_Drew)\
**Post date:** [August 19, 2019, 6:12pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-groups-in-groups/31074/1 "2019-08-19T18:12:54Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 19, 2019, 6:42pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-groups-in-groups/31074/2 "2019-08-19T18:42:31Z")

</div>

The format is in the documentation  
[https://docs.ansible.com/ansible/latest/dev\_guide/developing\_inventory.html](https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html)

---

<div class="post-metadata">

**Author:** ![A\_Drew](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@A\_Drew](https://forum.ansible.com/u/A_Drew)\
**Post date:** [August 19, 2019, 6:49pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-groups-in-groups/31074/3 "2019-08-19T18:49:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 19, 2019, 7:17pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-groups-in-groups/31074/4 "2019-08-19T19:17:03Z")

</div>

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  
&nbsp;&nbsp;ansible-inventory -i inv.yml --list

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

---

<div class="post-metadata">

**Author:** ![A\_Drew](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@A\_Drew](https://forum.ansible.com/u/A_Drew)\
**Post date:** [August 19, 2019, 7:20pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-groups-in-groups/31074/5 "2019-08-19T19:20:49Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 19, 2019, 7:36pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-groups-in-groups/31074/6 "2019-08-19T19:36:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![A\_Drew](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@A\_Drew](https://forum.ansible.com/u/A_Drew)\
**Post date:** [August 21, 2019, 4:41pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-groups-in-groups/31074/7 "2019-08-21T16:41:19Z")

</div>

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!
