wolodyjowski
wolodyjowski

Reputation: 15

How to run ansible playbook on same hosts from different groups

is it possible to create such ansible inventory where it is possible to run task only on one group, on two groups or on all of them, but within groups there are hosts that are called the same?

The aliases for host are the same in groups as in playbook, but in each group there are different vars for each server and vm. What is more, to connect to VM from localhost it is needed to use server as jumphost.

Scenario should looks more or less like this: Inventory:

[group1]
server
vm
[group2]
server
vm
[group3]
server
vm

Playbook:

- hosts: server
  tasks:
  - name: ....

- hosts: vm
  tasks:
  - name: ... 

Upvotes: 0

Views: 1257

Answers (1)

Zeitounator
Zeitounator

Reputation: 44799

Given the following inventory:

[server]
machine1.somewhere.com
machine2.somewhere.com
machine3.somewhere.com

[vm]
vm1.virtual.com
vm2.virtual.com
vm3.virtual.com

[group1]
machine1.somewhere.com
vm1.virtual.com

[group2]
machine2.somewhere.com
vm2.virtual.com

[group3]
machine3.somewhere.com
vm3.virtual.com

you can use your above playbook scaffold and limit its usage to a specific group:

# All groups
ansible-playbook -i inventory.ini playbook.yml

# Group 1 only
ansible-playbook -i inventory.ini playbook.yml -l group1

# Group 1 and 3 
ansible-playbook- i inventory.ini playbook.yom -l group1:group3

You can also use such patterns for the hosts stanza in your play. For more on patterns, see the documentation

Upvotes: 0

Related Questions