Hedeesa
Hedeesa

Reputation: 178

run a role twice in Ansible

I wrote my playbook this way:

- name: install kubernetes 
  hosts:  [kuber]
  roles:
    - A
    - B
    - C
    - B

which means I wanted to run B role twice, but based on result, the second B hasn't even run.

What should I do to run a role multiple times?

Upvotes: 2

Views: 2652

Answers (2)

Jack
Jack

Reputation: 6158

Use include_role:

- name: install kubernetes 
  hosts:  [kuber]
  tasks:
    - include_role:
      name: A
    - include_role:
      name: B
    - include_role:
      name: C
    - include_role:
      name: B

Upvotes: 0

Zeitounator
Zeitounator

Reputation: 44615

Ref: https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html#role-duplication-and-execution

To make roles run more than once, there are two options:

  1. Pass different parameters in each role definition.
  2. Add allow_duplicates: true to the meta/main.yml file for the role.

So an easy workaround in your case could be as follow:

- name: install kubernetes
  hosts:  [kuber]
  roles:
   - role: A
   - role: B
     vars:
       fake_param: firstinclude
   - role: C
   - role: B
     vars:
       fake_param: secondinclude

Upvotes: 3

Related Questions