# Unified inventory

**URL:** <https://forum.ansible.com/t/unified-inventory/15777>\
**Category:** Ansible Project\
**Created:** [July 25, 2014, 1:56pm UTC](https://forum.ansible.com/t/unified-inventory/15777 "2014-07-25T13:56:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [July 25, 2014, 1:56pm UTC](https://forum.ansible.com/t/unified-inventory/15777/1 "2014-07-25T13:56:11Z")

</div>

Our inventory comes from multiple sources, a couple dynamic and one  
static. Ansible itself can pull them all together into a unified  
inventory, but we'd like to transform the dynamic sources on the way in,  
without having to hack the scripts that produce them, so we can use the  
stock versions from .../plugins/inventory. For example, we might want to  
set some variables based on what groups a host is in, add it to some  
additional groups (e.g. if a host is in 'prod' and 'webserver', also add  
it to 'prod-webservers'), etc.

I started out by writing a Python script which would take the output of  
those scripts, json.loads() it into a big data structure, and then create  
a new data structure that I could json.dumps() for Ansible to consume.  
Along the way I ran into 'import ansible.inventory', which seemed like it  
might be more appealing (and more Pythonic): Rather than slogging through  
these giant data structures, perhaps I'd like to create an empty  
ansible.inventory.Inventory object, and populate it with the information  
from the dynamic and static sources.

I had two questions about that:

(1) Does this seem like a reasonable approach?

(2) If so, is there an easy way to dump the contents of an  
&nbsp;&nbsp;&nbsp;&nbsp;ansible.inventory.Inventory object into the JSON that Ansible itself  
&nbsp;&nbsp;&nbsp;&nbsp;wants to consume? I didn't see an obvious method for that, but might  
&nbsp;&nbsp;&nbsp;&nbsp;have missed something.

Thanks in advance for any advice and input.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [July 25, 2014, 2:55pm UTC](https://forum.ansible.com/t/unified-inventory/15777/2 "2014-07-25T14:55:20Z")

</div>

> (1) Does this seem like a reasonable approach?

​Seems reasonable yes.  
I'd actually love to see a framework for such things. Shouldn't even be  
ansible specific.  
​

> (2) If so, is there an easy way to dump the contents of an  
> &nbsp;&nbsp;&nbsp;&nbsp;ansible.inventory.Inventory object into the JSON that Ansible itself  
> &nbsp;&nbsp;&nbsp;&nbsp;wants to consume? I didn't see an obvious method for that, but might  
> &nbsp;&nbsp;&nbsp;&nbsp;have missed something.

​nothing in ansible core.  
I have been hacking on a script ​that does just that, and I updated it to  
the inventory refactor of past weeks, but atm it's not bug free yet, i need  
to give it some more love:

[https://github.com/ginsys/ansible-plugins/blob/devel/bin/ansible-inventory](https://github.com/ginsys/ansible-plugins/blob/devel/bin/ansible-inventory)

Could also be used to generate a cached json file.

&nbsp;&nbsp;Serge

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 25, 2014, 7:26pm UTC](https://forum.ansible.com/t/unified-inventory/15777/3 "2014-07-25T19:26:49Z")

</div>

> Our inventory comes from multiple sources, a couple dynamic and one  
> static. Ansible itself can pull them all together into a unified  
> inventory, but we'd like to transform the dynamic sources on the way in,  
> without having to hack the scripts that produce them, so we can use the  
> stock versions from .../plugins/inventory. For example, we might want to  
> set some variables based on what groups a host is in, add it to some  
> additional groups (e.g. if a host is in 'prod' and 'webserver', also add  
> it to 'prod-webservers'), etc.

This sounds like you are on the verge of writing your own inventory script  
or CMDB system. That's ok. That option is there for when things get too  
site-specific to have a generic set of solutions.

> I started out by writing a Python script which would take the output of  
> those scripts, json.loads() it into a big data structure, and then create  
> a new data structure that I could json.dumps() for Ansible to consume.  
> Along the way I ran into 'import ansible.inventory', which seemed like it  
> might be more appealing (and more Pythonic): Rather than slogging through  
> these giant data structures, perhaps I'd like to create an empty  
> ansible.inventory.Inventory object, and populate it with the information  
> from the dynamic and static sources.
> 
> I had two questions about that:
> 
> (1) Does this seem like a reasonable approach?

\*\*\*maybe\*\*\*.

I think I would be tempted to see if you could write an inventory script  
but you may need to "build" the inventory.

Tower is a good place to store such a built inventory if you would want to  
use the REST API to store various groups, which may also be a more  
efficient route. You could then stop maintaining things in text files.

Only saying this as you are a user, generally this isn't too much of a  
topic for this list unless its' a really good fit for a use case. Should  
this be interesting, do be sure to use your support connections and we'd be  
glad to share more info.

> (2) If so, is there an easy way to dump the contents of an  
> &nbsp;&nbsp;&nbsp;&nbsp;ansible.inventory.Inventory object into the JSON that Ansible itself  
> &nbsp;&nbsp;&nbsp;&nbsp;wants to consume? I didn't see an obvious method for that, but might  
> &nbsp;&nbsp;&nbsp;&nbsp;have missed something.

Ansible's inventory trees are mainly an internal API.
