Querying the inventory/hostvars from a plugin

17 views
Skip to first unread message

Quentin Stafford-Fraser

unread,
Jul 19, 2020, 2:29:46 PM7/19/20
to Ansible Development
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

Reply all
Reply to author
Forward
0 new messages