Inventory pattern matching from lookup plugin

I want to develop a lookup plugin that would take a string like “mygroup:!badhost” and return the list of host objects that match that pattern.

Based on my inspection of the Ansible source code, it seems like I could probably do that if I can get reference to an instance of the InventoryManager object. Can i get access to the inventory object from within a lookup plugin?

There is a lookup that already does this called “inventory_hostnames”

https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/lookup/inventory_hostnames.py

http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-the-inventory

I suppose to expand on that, once you have the name, you can then use

hostvars[item] to effectively get the data associated with the host.

Not an actual Host object, but it should have what you need

Great, thanks so much!

(attachments)

email-sigs-logo.png