# Creating name of inventory group from a variable

**URL:** <https://forum.ansible.com/t/creating-name-of-inventory-group-from-a-variable/18095>\
**Category:** Ansible Project\
**Created:** [April 27, 2015, 6:56pm UTC](https://forum.ansible.com/t/creating-name-of-inventory-group-from-a-variable/18095 "2015-04-27T18:56:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajres](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@ajres](https://forum.ansible.com/u/ajres)\
**Post date:** [April 27, 2015, 6:56pm UTC](https://forum.ansible.com/t/creating-name-of-inventory-group-from-a-variable/18095/1 "2015-04-27T18:56:12Z")

</div>

I have a task where I am updating a line in a file with a comma separated list of inventory hosts, this works for the hard coded case:

- name: Set host list  
lineinfile: dest=/home/project/pom.xml regexp=‘.\*’ line='{{ groups.tag\_Type\_targettype | intersect(‘groups.key\_mykey) | join(’, ‘) }}’

key\_mykey is a group generated from ec2 properties in a dynamic inventory using:

- name: Create groups based on ec2 access key  
group\_by: key=key\_{{ ec2\_key\_name }}

The issue I’m facing is that I need to be able to generate the intersection ‘key\_’ from a variable so that I can generate different intersections at playbook runtime.

I have tried different ways to use a variable for , most give a runtime error, the following does not, but it also generates an empty hosts list.

vars:  
ec2keyname: mykey

intersect(‘groups.key\_’ + ec2keyname)

Is there a way to use a variable in the intersection groups name?

Andy

---

<div class="post-metadata">

**Author:** ![ajres](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@ajres](https://forum.ansible.com/u/ajres)\
**Post date:** [April 28, 2015, 3:06pm UTC](https://forum.ansible.com/t/creating-name-of-inventory-group-from-a-variable/18095/2 "2015-04-28T15:06:45Z")

</div>

The solution turned out to be simply to use variable bracket notation instead of dot notation, the value in the brackets can be formed from the concatenation of a string and a variable

line=‘{{ groups.tag\_Type\_syncgateway | intersect(groups[key\_’ + ec2keyname]) | join(‘,’) }}’
