# Using variable in hosts group name

**URL:** <https://forum.ansible.com/t/using-variable-in-hosts-group-name/17573>\
**Category:** Ansible Project\
**Created:** [February 20, 2015, 12:16pm UTC](https://forum.ansible.com/t/using-variable-in-hosts-group-name/17573 "2015-02-20T12:16:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bumelant](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@bumelant](https://forum.ansible.com/u/bumelant)\
**Post date:** [February 20, 2015, 12:16pm UTC](https://forum.ansible.com/t/using-variable-in-hosts-group-name/17573/1 "2015-02-20T12:16:56Z")

</div>

Hi everyone,

Is it possible to use variables in hosts: name, like so:

- name: This does not work  
hosts: “security\_group\_ **{{ cluster\_name }}** ”  
roles:
- my-cluster

The variable {{ cluster\_name }} is defined in my-cluster variables and is generally accessible. Not from the hosts: section though.

Thanks,  
Maciej

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 20, 2015, 1:00pm UTC](https://forum.ansible.com/t/using-variable-in-hosts-group-name/17573/2 "2015-02-20T13:00:01Z")

</div>

You CAN use variables for hosts: but not from inventory as you need  
to load hosts: to get the appropriate data.

also it would be:

hosts: "{{ 'security\_group\_' + cluster\_name }}"

---

<div class="post-metadata">

**Author:** ![bumelant](https://avatars.discourse-cdn.com/v4/letter/b/57b2e6/32.png) [@bumelant](https://forum.ansible.com/u/bumelant)\
**Post date:** [February 20, 2015, 6:17pm UTC](https://forum.ansible.com/t/using-variable-in-hosts-group-name/17573/3 "2015-02-20T18:17:35Z")

</div>

Hi Brian,

It did work as you wrote, thanks! I had to pass the variables form the command line with -e, as the variables I’ve tried to use before pulled from roles. Need to figure out a way to pull these to the playbook level apparently.

Regards,  
Maciej

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 20, 2015, 6:23pm UTC](https://forum.ansible.com/t/using-variable-in-hosts-group-name/17573/4 "2015-02-20T18:23:54Z")

</div>

no way, pretty much extra-vars are only one you have available at that point
