# Using inventory\_hostnames in lookup()

**URL:** <https://forum.ansible.com/t/using-inventory-hostnames-in-lookup/13837>\
**Category:** Ansible Project\
**Created:** [November 4, 2013, 11:31am UTC](https://forum.ansible.com/t/using-inventory-hostnames-in-lookup/13837 "2013-11-04T11:31:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [November 4, 2013, 11:31am UTC](https://forum.ansible.com/t/using-inventory-hostnames-in-lookup/13837/1 "2013-11-04T11:31:22Z")

</div>

Hi,

My infrastructure has separate “clusters” of interconnected machines. For example, several loadbalancer / backend servers combinations of the same kind. Such a host would have both a group for its cluster and a group for its role. For example:

[backend]  
be[1-4].example.com

[production]  
be[1-2].example.com

[staging]  
be[3-4].example.com

I use group vars to make sure every cluster groups defines a “cluster” variable with the cluster name.

The load balancer would need to know which servers to configure for, so the haproxy role accepts a variable backend\_servers (a list of hostnames). In my playbook, I would like to fill this variable with the intersection “backend:&{{ cluster }}”. I was trying to use something that started with lookup(‘inventory\_hostnames’):

- hosts: haproxy
- role: haproxy  
backend\_hosts: lookup(‘inventory\_hostnames’, [‘backend:&{{ cluster }}’])

But the call to lookup fails with an exception, even in a simple debug task:

[…]

File “/usr/lib/python2.7/dist-packages/ansible/runner/lookup\_plugins/inventory\_hostnames.py”, line 37, in **init**  
self.host\_list = kwargs[‘runner’].inventory.host\_list  
KeyError: ‘runner’

How would I get the list of hostnames to give to the haproxy role?

Regards,  
Joost

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 4, 2013, 11:27pm UTC](https://forum.ansible.com/t/using-inventory-hostnames-in-lookup/13837/2 "2013-11-04T23:27:34Z")

</div>

Syntax would be closer to this:

backend\_hosts: “{{ lookup(‘inventory\_hostnames’, ‘backend:&’ + cluster) }}”

I think you are freaking the system out a bit by the above, but should also reconsider your approach as Ansible likes things to be simple, and this is not.

Make a group called “backend\_hosts” and fill it up with multiple subgroups

backend\_hosts\_cluster1

and then you can do, simply

groups[“backend\_hosts\_” + cluster]

any time you want the hosts in a particular cluster

the group “backend\_hosts” would still contain all of them, for all clusters.

---

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [November 5, 2013, 11:45pm UTC](https://forum.ansible.com/t/using-inventory-hostnames-in-lookup/13837/3 "2013-11-05T23:45:23Z")

</div>

> Syntax would be closer to this:
> 
> backend\_hosts: "{{ lookup('inventory\_hostnames', 'backend:&' + cluster) }}"
> 
> I think you are freaking the system out a bit by the above, ...

I dived into the code a bit, and the exception seems to be caused by  
the fact that the "inventory\_hostnames" lookup function requires a  
"runner" parameter, which is passed when it is called as  
"with\_inventory\_hostnames", but not when used in the "lookup"  
function. I don't know whether this is a bug, but as I accept your  
suggestion below I am not creating a GitHub issue (yet).

> ... but should also  
> reconsider your approach as Ansible likes things to be simple, and this is  
> not.
> 
> Make a group called "backend\_hosts" and fill it up with multiple subgroups
> 
> backend\_hosts\_cluster1
> 
> and then you can do, simply
> 
> groups["backend\_hosts\_" + cluster]
> 
> any time you want the hosts in a particular cluster  
> the group "backend\_hosts" would still contain all of them, for all clusters.

Alright, that sounds like a good idea. I am dividing up the inventory  
into "production", "staging" and dynamic environments and I like to  
create the "backend\_hosts" group in a non-centralized way, which  
Ansible let's me do. The resulting inventory files are a bit  
repetitive, but not too much:
