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

1,225 views
Skip to first unread message

Jasper Capel

unread,
Sep 20, 2013, 7:41:31 AM9/20/13
to ansible...@googlegroups.com
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

James Cammarata

unread,
Sep 20, 2013, 11:25:16 AM9/20/13
to ansible...@googlegroups.com
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.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

James Cammarata <jcamm...@ansibleworks.com>
Sr. Software Engineer, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Antonio Messina

unread,
Sep 20, 2013, 11:44:49 AM9/20/13
to ansible...@googlegroups.com
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.
antonio....@gmail.com
antonio...@uzh.ch +41 (0)44 635 42 22
GC3: Grid Computing Competence Center http://www.gc3.uzh.ch/
University of Zurich
Winterthurerstrasse 190
CH-8057 Zurich Switzerland

Antonio Messina

unread,
Sep 20, 2013, 1:51:40 PM9/20/13
to ansible...@googlegroups.com
I just sent a pull request with a test implementation of this idea.

.a.

Michael DeHaan

unread,
Sep 21, 2013, 9:09:17 AM9/21/13
to ansible...@googlegroups.com
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.


Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Jasper Capel

unread,
Sep 23, 2013, 3:36:02 AM9/23/13
to ansible...@googlegroups.com
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

Antonio Messina

unread,
Sep 23, 2013, 7:32:56 AM9/23/13
to ansible...@googlegroups.com
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.

Michael DeHaan

unread,
Sep 23, 2013, 7:49:01 AM9/23/13
to ansible...@googlegroups.com
"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.


Bryon Fryer

unread,
Oct 31, 2013, 4:11:33 PM10/31/13
to ansible...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages