# Is there any way to pass an inventory dictionary directly into the Inventory object?

**URL:** <https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513>\
**Category:** Ansible Project\
**Created:** [September 20, 2013, 11:41am UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513 "2013-09-20T11:41:31Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jasper\_Capel](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@Jasper\_Capel](https://forum.ansible.com/u/Jasper_Capel)\
**Post date:** [September 20, 2013, 11:41am UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/1 "2013-09-20T11:41:31Z")

</div>

Hi,

I am attempting to integrate Ansible with our systems management framework (python/Django) as a way to run commands on remote machines. We’ve already been using Ansible for a while and the application I’m integrating it is already responsible for generating the Ansible inventory (which I have available in my application as a python dictionary).

I’m trying to find a way to make the Ansible runner use this inventory without having to execute an external application, or having to dump it to a temporary file (in ini format).  
I see the Inventory **init** () method already accepts the host\_list as a (python) list, but this way any group-information and host variables are lost. Is there a way to do this with a dictionary instead?

Thanks,  
Jasper

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 20, 2013, 3:25pm UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/2 "2013-09-20T15:25:16Z")

</div>

I don’t believe there’s a way to do this right now, without dumping the data to a temporary file.

It sounds like you might need a new inventory type, similar to the script.py one but instead taking the data structure directly rather than reading it from the output of the file and a small modification to lib/ansible/inventory/ **init**.py to check if it’s a dictionary. If you’re interested in contributing something like that, let us know.

---

<div class="post-metadata">

**Author:** ![Antonio\_Messina](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Antonio\_Messina](https://forum.ansible.com/u/Antonio_Messina)\
**Post date:** [September 20, 2013, 3:44pm UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/3 "2013-09-20T15:44:49Z")

</div>

I would actually be interested in developing this.

An option could be create a `DictionaryInventory` to use as a parser  
to parse a dictionary and create a dictionary of Group() class, like  
what InventoryParser and InventoryScript are doing.  
In Inventory.\_\_init\_\_ then we could add a test, so that if host\_list  
is a dictionary, then self.parser = DictionaryInvenetory(host\_list)  
and self.groups = self.parser.groups.values().

.a.

---

<div class="post-metadata">

**Author:** ![Antonio\_Messina](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Antonio\_Messina](https://forum.ansible.com/u/Antonio_Messina)\
**Post date:** [September 20, 2013, 5:51pm UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/4 "2013-09-20T17:51:40Z")

</div>

I just sent a pull request with a test implementation of this idea.

.a.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 21, 2013, 1:09pm UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/5 "2013-09-21T13:09:17Z")

</div>

I’m not interested in this per se.

What you should do is use the Inventory API and Runner/Playbook API as per /usr/bin/ansible-playbook _OR_ write a suitable inventory plugin that reads the file format you like from whatever location.

---

<div class="post-metadata">

**Author:** ![Jasper\_Capel](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@Jasper\_Capel](https://forum.ansible.com/u/Jasper_Capel)\
**Post date:** [September 23, 2013, 7:36am UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/6 "2013-09-23T07:36:02Z")

</div>

So the preferred method of invoking Ansible from another Python application is dumping the inventory to a temporary location and telling Ansible to read it from that location, or having ansible execute an external script? That seems inefficient to me, as it’s passing data around through various locations that could easily be passed in directly.

I understand the desire to keep the codebase small/simple, but I don’t think the dict-implementation is fundamentally different from the string- and list-implementations, it’s just a more powerful variant.  
But if this is not to be implemented, is it possible to instantiate an empty Inventory object (giving it an empty list) and use the add\_group() method to populate the object myself? Or should I expect things to go wrong if I use it this way?

Thanks,  
Jasper

---

<div class="post-metadata">

**Author:** ![Antonio\_Messina](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Antonio\_Messina](https://forum.ansible.com/u/Antonio_Messina)\
**Post date:** [September 23, 2013, 11:32am UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/7 "2013-09-23T11:32:56Z")

</div>

I would be happy to maintain the code, we are using ansible as part of  
one of our software, and we are interested in this feature. So far we  
are using an external, temporary file, but we are not completely  
satisfied with this solution, (we are basically using an external file  
to copy data from a python class to another) and we would like to use  
the implementation I sent or similar approaches.

I also think that without this feature (programmatically create a  
complete inventory class with groups), there is no way to effectively  
use ansible from external programs, which is something that at least  
two people in this mailing list are trying to do.

.a.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 23, 2013, 11:49am UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/8 "2013-09-23T11:49:01Z")

</div>

“So the preferred method of invoking Ansible from another Python application is dumping the inventory to a temporary location and telling Ansible to read it from that location, or having ansible execute an external script?”

No.

I’m saying if you are doing Python things, you should use the Inventory class, rather than passing a raw datastructure.

This is how Ansible’s userland implements both external inventory scripts and it’s own API parsing.

Also, this is really a discussion for ansible-devel list.

---

<div class="post-metadata">

**Author:** ![Jasper\_Capel](https://avatars.discourse-cdn.com/v4/letter/j/46a35a/32.png) [@Jasper\_Capel](https://forum.ansible.com/u/Jasper_Capel)\
**Post date:** [September 23, 2013, 2:41pm UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/9 "2013-09-23T14:41:26Z")

</div>

Moving this discussion to ansible-devel. 🙂

I was actually trying to use the Inventory class and passing that to the runner. The problem arises when attempting to instantiate the Inventory object. In the **init** method, it wants either a string, or a list, or a file, or a script. Being able to pass a dict (containing also groups information and host-vars) would be very convenient.

Alternatively, I could just instantiate the Inventory object with an empty list and populate it myself afterwards. It just seemed like the less elegant option.

Jasper

---

<div class="post-metadata">

**Author:** ![Bryon\_Fryer](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@Bryon\_Fryer](https://forum.ansible.com/u/Bryon_Fryer)\
**Post date:** [October 31, 2013, 8:07pm UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/10 "2013-10-31T20:07:43Z")

</div>

Another non-eligant solution would be to have another pre-inventory class that upon being populated can be pinv. **dict**'d into json (the same as the result from an inventory script) into a proper inventory. Haven’t tried this yet.

---

<div class="post-metadata">

**Author:** ![Bryon\_Fryer](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@Bryon\_Fryer](https://forum.ansible.com/u/Bryon_Fryer)\
**Post date:** [October 31, 2013, 8:11pm UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/11 "2013-10-31T20:11:33Z")

</div>

For those following along at home, new thread is here …

[https://groups.google.com/forum/#!searchin/ansible-devel/inventory/ansible-devel/ev02HyLgOnc/-EYwOsUGmrQJ](https://groups.google.com/forum/#!searchin/ansible-devel/inventory/ansible-devel/ev02HyLgOnc/-EYwOsUGmrQJ)

---

<div class="post-metadata">

**Author:** ![Bryon\_Fryer](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@Bryon\_Fryer](https://forum.ansible.com/u/Bryon_Fryer)\
**Post date:** [October 31, 2013, 8:24pm UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/12 "2013-10-31T20:24:05Z")

</div>

Actually, nevermind, I dug into the code and this isn’t easily possible either, I guess I’m back to a NamedTemporaryFile…

---

<div class="post-metadata">

**Author:** ![randall\_harris](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@randall\_harris](https://forum.ansible.com/u/randall_harris)\
**Post date:** [April 28, 2014, 2:01pm UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/13 "2014-04-28T14:01:12Z")

</div>

It has been nearly 5 months since the last thread on this topic. I was wanting to know if there have been any significant updates.  
I am developing Python code to integrate with our Ansible solution for maintaining Ansible dict files with up-to-date Applications-RemoteHost-ApplicationsVersion information.  
I need some sleek Python code which will read an Ansible dict file (like the one below), then Add or Del certain RemoteHost-ApplicationsVersion then write back out to the same Ansible dict format.  
Does anyone inside or outside of Ansible have any working examples?  
Thanks.

---

<div class="post-metadata">

**Author:** ![randall\_harris](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@randall\_harris](https://forum.ansible.com/u/randall_harris)\
**Post date:** [April 28, 2014, 2:05pm UTC](https://forum.ansible.com/t/is-there-any-way-to-pass-an-inventory-dictionary-directly-into-the-inventory-object/13513/14 "2014-04-28T14:05:31Z")

</div>

And here’s my example Ansible dict file:
