# Inventory pattern matching from lookup plugin

**URL:** <https://forum.ansible.com/t/inventory-pattern-matching-from-lookup-plugin/24624>\
**Category:** Ansible Project\
**Created:** [June 1, 2017, 6:06pm UTC](https://forum.ansible.com/t/inventory-pattern-matching-from-lookup-plugin/24624 "2017-06-01T18:06:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![randy.syring](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@randy.syring](https://forum.ansible.com/u/randy.syring)\
**Post date:** [June 1, 2017, 6:06pm UTC](https://forum.ansible.com/t/inventory-pattern-matching-from-lookup-plugin/24624/1 "2017-06-01T18:06:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 1, 2017, 6:10pm UTC](https://forum.ansible.com/t/inventory-pattern-matching-from-lookup-plugin/24624/2 "2017-06-01T18:10:12Z")

</div>

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](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](http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-the-inventory)

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 1, 2017, 6:11pm UTC](https://forum.ansible.com/t/inventory-pattern-matching-from-lookup-plugin/24624/3 "2017-06-01T18:11:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![randy.syring](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@randy.syring](https://forum.ansible.com/u/randy.syring)\
**Post date:** [June 1, 2017, 8:24pm UTC](https://forum.ansible.com/t/inventory-pattern-matching-from-lookup-plugin/24624/4 "2017-06-01T20:24:46Z")

</div>

Great, thanks so much!

> **(attachments)**
>
> ![email-sigs-logo.png](https://us1.discourse-cdn.com/ansible/original/2X/5/5c09f4a39fe3b38bb6dd33d70829812818202c33.png)
