# possible stupid question: selection of host groups and use of variables?

**URL:** <https://forum.ansible.com/t/possible-stupid-question-selection-of-host-groups-and-use-of-variables/12122>\
**Category:** Ansible Project\
**Created:** [December 6, 2012, 6:44am UTC](https://forum.ansible.com/t/possible-stupid-question-selection-of-host-groups-and-use-of-variables/12122 "2012-12-06T06:44:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan1](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Jonathan1](https://forum.ansible.com/u/Jonathan1)\
**Post date:** [December 6, 2012, 6:44am UTC](https://forum.ansible.com/t/possible-stupid-question-selection-of-host-groups-and-use-of-variables/12122/1 "2012-12-06T06:44:20Z")

</div>

Apologies in advance - I feel like this has been solved before but I am not coming up with much after searching ansible.cc and the group records.

I am trying to make a playbook that I can use between different environments. I have application X deployed in a set of environments, dev, load testing, deployment testing, production, etc… and I want to use the same playbook to do some work and select the environment based on a variable. The host groups in question are named in a pattern to make this possible

app\_x\_load\_test  
app\_x\_deployment\_test  
app\_x\_production

and I want to reference them with something like:

- hosts: app\_x\_$env  
tasks:
- name: do something incredibly awesome  
action: dd bs=1M if=/dev/zero of=/tmp/foo

but that doesn’t work so well…

PLAY [app\_x\_$env] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
no hosts matched

I have a sneaking suspicion I am doing something stupid or terribly obvious, but any advice would be most welcome.

cheers,  
Jonathan

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [December 6, 2012, 7:16am UTC](https://forum.ansible.com/t/possible-stupid-question-selection-of-host-groups-and-use-of-variables/12122/2 "2012-12-06T07:16:41Z")

</div>

We have in a similar way dev, test en production hosts.  
Those are grouped per application, e.g.

[app\_x\_dev]  
app\_x\_dev\_node1  
app\_x\_dev\_node2  
[app\_x\_test]  
app\_x\_test\_node1

Then a super group per app:

[app\_x:children]  
app\_x\_dev  
app\_x\_test

And then we have some more global groups, regrouping all test  
machines, all dev machines and all production machines.

[dev:children]  
app\_x\_dev  
app\_y\_dev

At playbook level we define a generic play for a certain app,  
deploying it to the [app\_x] group.  
When ran, we limit (--limit option to ansible-playbook) playbook  
execution to e.g. [dev] group. This is handled by some wrapper scripts  
calling ansible-playbook.

HTH

Serge

---

<div class="post-metadata">

**Author:** ![Andreas\_Davour](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@Andreas\_Davour](https://forum.ansible.com/u/Andreas_Davour)\
**Post date:** [December 6, 2012, 8:56am UTC](https://forum.ansible.com/t/possible-stupid-question-selection-of-host-groups-and-use-of-variables/12122/3 "2012-12-06T08:56:55Z")

</div>

I'm not sure if I understand what you're doing, since it looks complicated. But, this is how I use the same playbook against different tagets.

hosts: $hostlist

and then invoke ansible-playbook with --extra-vars="hostlist=webservers\_dev".

Maybe some variation thereof is what you're looking for?

/andreas

---

<div class="post-metadata">

**Author:** ![Jonathan1](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Jonathan1](https://forum.ansible.com/u/Jonathan1)\
**Post date:** [December 6, 2012, 7:02pm UTC](https://forum.ansible.com/t/possible-stupid-question-selection-of-host-groups-and-use-of-variables/12122/4 "2012-12-06T19:02:39Z")

</div>

Andreas,

Many thanks for the pointer. I stand by my prior statement that I was doing something a bit silly. What I was trying to do is to use the variable to replace a substring in the hostname - the “app\_x\_” section of the hostname is fixed, where the “app\_x\_$env” is variable.

Basically was trying to be too tricky and I can easily use the variable name vs trying to do the substring replacement.

cheers  
Jonathan

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [December 7, 2012, 4:28pm UTC](https://forum.ansible.com/t/possible-stupid-question-selection-of-host-groups-and-use-of-variables/12122/5 "2012-12-07T16:28:20Z")

</div>

The hosts line is templated, so doing things like

hosts: foo\_$x

actually works fine, provided the variable "x" comes from --extra-vars

It won't work if coming from inventory/elsewhere for that particular  
field as that hasn't been loaded quite yet.

---

<div class="post-metadata">

**Author:** ![Jonathan1](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Jonathan1](https://forum.ansible.com/u/Jonathan1)\
**Post date:** [December 7, 2012, 8:10pm UTC](https://forum.ansible.com/t/possible-stupid-question-selection-of-host-groups-and-use-of-variables/12122/6 "2012-12-07T20:10:10Z")

</div>

hrrm, must be doing something funny here (w/ v0.9)

_# ansible-playbook -vvv --extra-vars “container=tomcat app\_package\_name=test env=dev version=1.2”_

_PLAY [database\_$env\_master] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*_  
_no hosts matched_

playbook contents:

_- hosts: database\_$env\_master_  
_tasks:_  
_- name: do something awesome_  
_action: blah blah blah_

will dig into this further if its possible this is an actual issue (vs. me doing something stupid)
