Dynamic inventory script (--host flooding)

Hello,
I’m trying to develop a script for dynamic inventory, following this (poor) documentation
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

use the ‘_meta’ key

http://docs.ansible.com/developing_inventory.html#tuning-the-external-inventory-script

Is it the only way?

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

​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.

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.

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

Performance issue solved.

Thanks.