# Accessing Ansible inventory externally

**URL:** <https://forum.ansible.com/t/accessing-ansible-inventory-externally/12620>\
**Category:** Ansible Project\
**Created:** [April 3, 2013, 1:32pm UTC](https://forum.ansible.com/t/accessing-ansible-inventory-externally/12620 "2013-04-03T13:32:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [April 3, 2013, 1:32pm UTC](https://forum.ansible.com/t/accessing-ansible-inventory-externally/12620/1 "2013-04-03T13:32:05Z")

</div>

Hi all,  
I’ve got a homegrown app where I’d love to be able to use Ansible’s inventory… I could of course use an external inventory for both, but Ansible’s built-in way of doing inventory and vars is really all I need so it would be great if there’s some way to get Ansible to dump that inventory for extrenal consumption.

One way of doing this that occurred to me was creating an external inventory script that really just queries Ansible (then my other app could just call “./hosts --list” or “./hosts --host foo” for what it needs). I haven’t looked into that too much yet, though, in case there was some easier way to do this that I’m missing.

I figure Ansible commander will also make this trivial, but i’m wondering if there’s a way to do it now 🙂

thanks!  
matt

---

<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:** [April 3, 2013, 1:57pm UTC](https://forum.ansible.com/t/accessing-ansible-inventory-externally/12620/2 "2013-04-03T13:57:03Z")

</div>

Commander will allow access to inventory as a REST API.

(Injection and support for inventory scripts is not /quite/ there yet)

---

<div class="post-metadata">

**Author:** ![Seth\_Vidal](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@Seth\_Vidal](https://forum.ansible.com/u/Seth_Vidal)\
**Post date:** [April 3, 2013, 2:29pm UTC](https://forum.ansible.com/t/accessing-ansible-inventory-externally/12620/3 "2013-04-03T14:29:13Z")

</div>

the basic inventory seem to be:

import ansible.inventory

inv = ansible.inventory.Inventory()

for group in inv.get\_groups():  
&nbsp;&nbsp;&nbsp;&nbsp;print group.name  
&nbsp;&nbsp;&nbsp;&nbsp;for host in group.get\_hosts():  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print ' ' + host.name

-sv

---

<div class="post-metadata">

**Author:** ![Matt\_Coddington](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Matt\_Coddington](https://forum.ansible.com/u/Matt_Coddington)\
**Post date:** [April 3, 2013, 2:40pm UTC](https://forum.ansible.com/t/accessing-ansible-inventory-externally/12620/4 "2013-04-03T14:40:31Z")

</div>

Awesome, thanks for the pointer Seth, this should get me what I need while I wait for commander 🙂

matt
