Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid duplicate role execution

Tags:

ansible

I have a playbook like

- hosts: hostA
  roles:
    - roleA
    - roleB

- hosts: hostB
  roles:
    - roleA
    - roleC

and an inventory

all:
  children:
    hostA:
      hosts:
        machine1
    hostB:
      hosts:
        machine2

So far, everything works fine. But now I have a second environment with inventory

all:
  children:
    hostA:
      hosts:
        machine3
    hostB:
      hosts:
        machine3

RoleA gets executed twice when I run the playbook. Is there a way to prevent that?

like image 883
techtech Avatar asked Jun 20 '26 13:06

techtech


1 Answers

The most DRY, straightforwad and easy way to "fix" this is:


- name: run common roles for both groups
  hosts: hostA:hostB
  roles:
    - roleA

- name: run specific roles for group A
  hosts: hostA
  gather_facts: false
  roles:
    - roleB

- name: run specific roles for group B
  hosts: hostB
  gather_facts: false
  roles:
    - roleC

If you are unclear with the meaning of hosts: hostA:hostB in the first play, have a look at ansible hosts/group patterns

like image 123
Zeitounator Avatar answered Jun 23 '26 12:06

Zeitounator