# Querying the inventory/hostvars from a plugin

**URL:** <https://forum.ansible.com/t/querying-the-inventory-hostvars-from-a-plugin/33432>\
**Category:** Ansible Developer\
**Created:** [July 19, 2020, 6:29pm UTC](https://forum.ansible.com/t/querying-the-inventory-hostvars-from-a-plugin/33432 "2020-07-19T18:29:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quentin\_Stafford-Fra](https://avatars.discourse-cdn.com/v4/letter/q/b2d939/32.png) [@Quentin\_Stafford-Fra](https://forum.ansible.com/u/Quentin_Stafford-Fra)\
**Post date:** [July 19, 2020, 6:29pm UTC](https://forum.ansible.com/t/querying-the-inventory-hostvars-from-a-plugin/33432/1 "2020-07-19T18:29:46Z")

</div>

Hello All -

I’m writing a local plugin where I’d like to be able to pass in the name of an Ansible host group and find out particular details about the relevant hosts - basically, I need to access groups and hostvars from a Python plugin.

Is this possible? I’ve done lots of Ansible but very limited plugin development for it, so apologies if this is a foolish question. Please point me in the right direction if there’s some documentation I’ve missed!

Background:

My actual aim is to create Amazon ELB loadbalancer target rules which point at the hosts in a particular group. To do this, I need the EC2 instance IDs of those hosts.

I can get the instance info for all my hosts using ec2\_instance\_info, and I can match them up based on IP addresses with the information in `groups` and `hostvars`, but this kind of thing is very convoluted and unreadable in Jinja tags and filters. It would be easy and readable in Python.

I know how to do the EC2 side. I just need to know how to do the inventory side, so I can get rid of things like:

`groups[groupname] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list)`

Suggestions of alternative approaches also welcome!

Many thanks!  
Quentin
