Munchkin
Munchkin

Reputation: 4946

Determine hosts for a play

I have multiple different apps running on different servers, e.g.

In my dev-intentory file I listed up all my 4 devservers.

Now I'd like to write a parametrized playbook which "knows" on which hosts it has to run when I provide an app_name-variable.

Currently I have a var file containing something like this:

# vars/apps.yml
apps:
  app-1:
    servers:
      dev:
      - devserver-1
      - devserver-2

And a playbook like this:

# playbook.yml
vars_files:
  - vars/apps.yml
hosts: "{{ apps[app_name].servers[stage] | join(',') }}"

But for various reasons I'm not happy with this configuration and want to avoid the usage of vars_files (and use roles/include_vars instead).

So how can I determine the hosts for a playbook (which is shared between multiple apps) without using vars_files?

Upvotes: 0

Views: 41

Answers (1)

Jack
Jack

Reputation: 6158

Group them in your hosts file, like this:

[app-1]
devserver-1
devserver-2
[app-2]
devserver-3
devserver-4
[app-3]
devserver-1

Then have your playbook use the variable in the hosts line:

- hosts: "{{ app_name }}"

(I honestly don't remember whether you can have a - in a group name. Try it.)

Upvotes: 1

Related Questions