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

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

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

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

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

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.

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)