# tree/hierarchic structure for inventory?

**URL:** <https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772>\
**Category:** Ansible Project\
**Created:** [July 15, 2015, 12:01pm UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772 "2015-07-15T12:01:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![esco\_real](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@esco\_real](https://forum.ansible.com/u/esco_real)\
**Post date:** [July 15, 2015, 12:01pm UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772/1 "2015-07-15T12:01:04Z")

</div>

Hello,

I would like to discuss the idea for a hierarchic structure for the inventory. Would it possible to support something like that?

For example with this structure:

> server  
> – atlanta  
> `-- web `–hosts:web1  
> – raleigh  
> `-- web `– host:web2

You would need this config:  
{  
“web” : { “hosts”: [“web1”, “web2”] },  
“server” : { “hosts”: [“web1”, “web2”] },  
“atlanta” : { “hosts”: [“web1”] },  
“raleigh” : { “hosts”: [“web2”] }  
}

To work with “children” the group names would have to be unique in the whole tree. So “web” would break this up.

Wouldn’t it be nice to use something like that?  
{  
“server”  
{ “atlanta” { “web” {“hosts”: [“web1”] } } },  
{ “raleigh” { “web” {“hosts”: [“web2”] } } }  
}

I came up with this by using a KeePass database as inventory source:  
[https://github.com/escoreal/ansible\_kdb\_inventory](https://github.com/escoreal/ansible_kdb_inventory)  
Here I have to build the tree for each host and add it to all parents. But this doesn’t scale to well.

Thanks,  
esco

---

<div class="post-metadata">

**Author:** ![Mischa\_ter\_Smitten](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Mischa\_ter\_Smitten](https://forum.ansible.com/u/Mischa_ter_Smitten)\
**Post date:** [July 16, 2015, 8:50pm UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772/2 "2015-07-16T20:50:14Z")

</div>

We are working on the same problem. Right know we are using a custom piece of python code with nested dicts to represent our group and return them as json.

---

<div class="post-metadata">

**Author:** ![Paul\_Markham](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@Paul\_Markham](https://forum.ansible.com/u/Paul_Markham)\
**Post date:** [July 19, 2015, 4:01am UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772/3 "2015-07-19T04:01:40Z")

</div>

You can write a dynamic inventory script which allows you to specify the inventory in another format, or fetch it from a configuration management database.

I’m not sure if this is exactly what you want, but I wrote one that lets you specify servers and which parent groups they belong in; basically the inverse of the normal inventory. The code is here [https://github.com/pmarkham/ansible-inventory-inverted](https://github.com/pmarkham/ansible-inventory-inverted).

---

<div class="post-metadata">

**Author:** ![esco\_real](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@esco\_real](https://forum.ansible.com/u/esco_real)\
**Post date:** [July 19, 2015, 8:08am UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772/4 "2015-07-19T08:08:46Z")

</div>

Hello Paul,

yes, I did likely the same but used a KeePass 2 database instead of an ini file as inventory source. So here we have similar thoughts (if you are using KeePass 2 you should try my script 😉 ). Adding each host to his parents is possible. But apart from the scaling problem you would have trouble with group\_vars for non unique group names in different branches. And I guess there are a lot more users which manage their inventory as tree.

esco

---

<div class="post-metadata">

**Author:** ![Martin2](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@Martin2](https://forum.ansible.com/u/Martin2)\
**Post date:** [July 21, 2015, 12:08pm UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772/5 "2015-07-21T12:08:08Z")

</div>

Hi Paul,

I think the inverted inventory is a _great idea_.

IIRC clusterssh went from a format that was similiar to the “normal” ansible inventory to something like this:

node1 group1,group2,group3  
node2 group3  
node3 group1,group3

I think that this is a more natural way of specifying it rather than the sectioning of ini files. The group\_vars/group1, group\_vars/group2, … files (or dirs) could be used to specify common attributes, the dynamic inventory script could read these and output just the right information 🙂

Of course this create yet another config file format but that is just because I’m comparing to a tool where I found the inventory management to be somewhat working. When dealing with manual inventory management (of course going for larger numbers of servers nothing beats an actuall automated inventory) 🙂

/Martin

---

<div class="post-metadata">

**Author:** ![Paul\_Markham](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@Paul\_Markham](https://forum.ansible.com/u/Paul_Markham)\
**Post date:** [July 22, 2015, 4:50am UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772/6 "2015-07-22T04:50:22Z")

</div>

Hi Martin,

I originally had the inventory in yaml format, but changed to ini format for some reason (which I can’t remember). The clusterssh format would work well if you just wanted the host and group relationships, but I also wanted to support host and group variables like the default static inventory format does.

Ideally a CMDB of some sort would be used, but we don’t have one so we need to maintain a manual inventory.

Fortunately they’ve made it flexible enough to support whatever format you want to use; I’m currently playing with getting the inventory from etcd so we can dynamically add and remove hosts.

---

<div class="post-metadata">

**Author:** ![esco\_real](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@esco\_real](https://forum.ansible.com/u/esco_real)\
**Post date:** [July 22, 2015, 7:28am UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772/7 "2015-07-22T07:28:28Z")

</div>

Hello,

to build a tree with ini format I think something like that could be used:

[server.atlanta.web]  
web1

[server.raleigh.web]

web2

esco

---

<div class="post-metadata">

**Author:** ![Martin2](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@Martin2](https://forum.ansible.com/u/Martin2)\
**Post date:** [July 22, 2015, 8:31am UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772/8 "2015-07-22T08:31:30Z")

</div>

Hi,

yes the host and group variables are the most important thing and I’d personally go and separate them out to the group\_vars/… and host\_vars/… directories. That is just a matter preference.

I have no emotional attachement to the the file format. I’m just saying that I saw this format working and it was working very convenient to work with.

I think a large part of the way will be discussing this as it is nearly infinitely more complicated to create something that is then very simple to use 🙂

/Martin

---

<div class="post-metadata">

**Author:** ![esco\_real](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@esco\_real](https://forum.ansible.com/u/esco_real)\
**Post date:** [July 29, 2015, 4:44pm UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772/9 "2015-07-29T16:44:28Z")

</div>

Hello Martin,

you are right. Supporting a tree structure for this with plain files/directories (group\_vars…) wouldn’t be that nice. And it is very sensible at the moment.

But I made this work for me by resolving the group\_vars in the tree directly to host\_vars. So group names don’t have to be unique in the inventory. And now I am very happy with the combination of Ansible and KeePass…

esco

---

<div class="post-metadata">

**Author:** ![esco\_real](https://avatars.discourse-cdn.com/v4/letter/e/c89c15/32.png) [@esco\_real](https://forum.ansible.com/u/esco_real)\
**Post date:** [September 7, 2015, 7:10pm UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772/10 "2015-09-07T19:10:15Z")

</div>

If someone is interested in this topic:  
Now, I use the name and the UUID of the groups (name\_uuid) to build up the tree (children, group\_vars…). So group names don’t have to be unique and performance and scaling isn’t a problem anymore…

If you use a CMDB your groups usually would have some IDs, too.

esco

---

<div class="post-metadata">

**Author:** ![Krzysztof\_Zarzycki](https://avatars.discourse-cdn.com/v4/letter/k/94ad74/32.png) [@Krzysztof\_Zarzycki](https://forum.ansible.com/u/Krzysztof_Zarzycki)\
**Post date:** [May 11, 2016, 12:53pm UTC](https://forum.ansible.com/t/tree-hierarchic-structure-for-inventory/18772/11 "2016-05-11T12:53:02Z")

</div>

If someone come up with a solution (like a dynamic inventory script that reads some inverted inventory dsl), please share, I would be very interested!  
I see the need for such inverted inventory in the case, where I have a lot of interconnected services, where number of services is really large. This is how hadoop environment looks like with tens of different services.  
Thanks,  
Krzysztof

W dniu poniedziałek, 7 września 2015 21:10:15 UTC+2 użytkownik esco real napisał:
