# Dynamic inventory script (--host flooding)

**URL:** <https://forum.ansible.com/t/dynamic-inventory-script-host-flooding/18035>\
**Category:** Ansible Project\
**Created:** [April 21, 2015, 11:14am UTC](https://forum.ansible.com/t/dynamic-inventory-script-host-flooding/18035 "2015-04-21T11:14:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![spezam](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@spezam](https://forum.ansible.com/u/spezam)\
**Post date:** [April 21, 2015, 11:14am UTC](https://forum.ansible.com/t/dynamic-inventory-script-host-flooding/18035/1 "2015-04-21T11:14:46Z")

</div>

Hello,  
I’m trying to develop a script for dynamic inventory, following this (poor) documentation  
[http://docs.ansible.com/developing\_inventory.html](http://docs.ansible.com/developing_inventory.html)

I tested the script with the command  
**ansible --list-hosts database**  
which works fine and return the database hosts.

I logged the calls to the script and I can see the first call made by ansible (–list) outputs the list of hosts and their groups

`{ "database": { "hosts": ["host1", "host2"] }, "apache": { "hosts": ["host3", "host4"] } }`

Ansible then iterates through all the hosts and call the script with --host .

[“–list”]  
[“–host”, “host1”]

[“–host”, “host2”]

[“–host”, “host3”]

[“–host”, “host4”]

The performance problem obviously appears when the list of hosts and groups grows.  
While I expect the ‘–host’ call to be done only on the database hosts, it seems ansible is iterating through the full catalog.

Is that expected? Any way I can avoid this behavior?

Thanks,  
Matteo

---

<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:** [April 21, 2015, 1:59pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-host-flooding/18035/2 "2015-04-21T13:59:46Z")

</div>

use the ‘\_meta’ key

[http://docs.ansible.com/developing\_inventory.html#tuning-the-external-inventory-script](http://docs.ansible.com/developing_inventory.html#tuning-the-external-inventory-script)

---

<div class="post-metadata">

**Author:** ![spezam](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@spezam](https://forum.ansible.com/u/spezam)\
**Post date:** [April 21, 2015, 2:28pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-host-flooding/18035/3 "2015-04-21T14:28:54Z")

</div>

Is it the only way?

I do really want to avoid to iterate through each host (instead of using arrays) and produce duplicate data…

---

<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:** [April 21, 2015, 2:36pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-host-flooding/18035/4 "2015-04-21T14:36:50Z")

</div>

​I don't understand this.​

Using the meta key allow you to return all host variables in respins to the  
--list call to the script, in one time.  
That is an aditional key for the per host vars of course.

---

<div class="post-metadata">

**Author:** ![spezam](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@spezam](https://forum.ansible.com/u/spezam)\
**Post date:** [April 21, 2015, 2:47pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-host-flooding/18035/5 "2015-04-21T14:47:27Z")

</div>

Yeah, my only problem is that I already have the hosts array data structure available, but to produce the \_meta information I have to split that information into single hosts.

I hoped I could avoid it, but I guess that’s the only way.

---

<div class="post-metadata">

**Author:** ![spezam](https://avatars.discourse-cdn.com/v4/letter/s/e47774/32.png) [@spezam](https://forum.ansible.com/u/spezam)\
**Post date:** [April 21, 2015, 5:38pm UTC](https://forum.ansible.com/t/dynamic-inventory-script-host-flooding/18035/6 "2015-04-21T17:38:59Z")

</div>

Since that was the only option, did some changes and implemented the \_meta output.

Performance issue solved.

Thanks.
