# Possible to use a variable value for the playbook "hosts" attribute?

**URL:** <https://forum.ansible.com/t/possible-to-use-a-variable-value-for-the-playbook-hosts-attribute/17137>\
**Category:** Ansible Project\
**Created:** [December 23, 2014, 7:39pm UTC](https://forum.ansible.com/t/possible-to-use-a-variable-value-for-the-playbook-hosts-attribute/17137 "2014-12-23T19:39:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JD11](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@JD11](https://forum.ansible.com/u/JD11)\
**Post date:** [December 23, 2014, 7:39pm UTC](https://forum.ansible.com/t/possible-to-use-a-variable-value-for-the-playbook-hosts-attribute/17137/1 "2014-12-23T19:39:54Z")

</div>

Hi ! I would like to use a variable value for the playbook “hosts” clause, eg.:

- name: PLAY\_NAME  
hosts: “{{myvariable}}”  
sudo: yes  
tasks:  
…

where the variable is defined elsewhere as eg.:

myvariable: “_1.domain_.com”

or:

myvariable: “group1:group2:group3”

However, when I attempt something like this, I always get the “skipping: no hosts matched” message when running the playbook (even if I put myvariable: “exact.host.name”).

Is it at all possible to use a variable here? Putting the pattern itself to every playbook is extremely unDRY, and driving me nuts.

---

<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:** [December 23, 2014, 7:45pm UTC](https://forum.ansible.com/t/possible-to-use-a-variable-value-for-the-playbook-hosts-attribute/17137/2 "2014-12-23T19:45:05Z")

</div>

It is possible to use a variable, your issue might be where you are defining it.

if you pass on the command line -e 'myvariable=group1:group2:group3',  
it should work

---

<div class="post-metadata">

**Author:** ![JD11](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@JD11](https://forum.ansible.com/u/JD11)\
**Post date:** [December 23, 2014, 7:53pm UTC](https://forum.ansible.com/t/possible-to-use-a-variable-value-for-the-playbook-hosts-attribute/17137/3 "2014-12-23T19:53:20Z")

</div>

So to clarify, it’s impossible to put the variable in eg. a group\_vars/all file?

It it possible to put in in any other place than the command line?

---

<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:** [December 23, 2014, 7:54pm UTC](https://forum.ansible.com/t/possible-to-use-a-variable-value-for-the-playbook-hosts-attribute/17137/4 "2014-12-23T19:54:38Z")

</div>

you can put the variable in the groups\_vars/all file, it just won't be  
evaluated for the - hosts: entry, the play needs to know which hosts  
it targets before using the inventory variables.

---

<div class="post-metadata">

**Author:** ![JD11](https://avatars.discourse-cdn.com/v4/letter/j/a9a28c/32.png) [@JD11](https://forum.ansible.com/u/JD11)\
**Post date:** [December 23, 2014, 7:58pm UTC](https://forum.ansible.com/t/possible-to-use-a-variable-value-for-the-playbook-hosts-attribute/17137/5 "2014-12-23T19:58:17Z")

</div>

Ok, now I understand, thank you for help !
