1

I have a role with some variables than I using several times with different parameters like below:

  roles:
    - role: my_role
      vars:
        role_uuid: uuud_1
        first_param: first
    - role: my_role
      vars:
        role_uuid: uuid_2
        second_param: second

Problem is that when my role is executed:

  • The first role instance, uuid_1, has a parameter second_param which is set to second
  • The second role instance, uuid_2, has a parameter first_param that is set to first.

To resume, both instance have the parameters first_param and second_param set.

It seems that the parameters of the instances of the role my_role are merged then only the part that differ is really different (here role_uuid).

Is there are way to avoid this merge ?

DevOps
  • 720
  • 5
  • 16

2 Answers2

3

This is expected with roles defined in the roles section.

Use include_role or import_role module in tasks (or pre_tasks) to avoid the problem:

tasks:
  - include_role:
      name: my_role
    vars:
      role_uuid: uuud_1
      first_param: first
  - include_role:
      name: my_role
    vars:
      role_uuid: uuud_2
      second_param: second
techraf
  • 4,243
  • 8
  • 29
  • 44
0

An option would be to dynamically include vars in my_role

tasks:
  - include_vars: "{{ role_id }}"
  - debug: msg="{{ role_id }}.first_param [ {{ first_param|default('undefined') }} ]"
  - debug: msg="{{ role_id }}.second_param [ {{ second_param|default('undefined') }} ]"

$ cat vars/id_1

first_param: 'first'

$ cat vars/id_2

second_param: 'second'

Then run the play

$ cat play.yml

- hosts: localhost
  tasks:
  - include_role:
      name: my_role
    vars:
      role_id: 'id_1'
  - include_role:
      name: my_role
    vars:
      role_id: 'id_2'

$ ansible-playbook play.yml | grep msg

"msg": "id_1.first_param [ first ]"
"msg": "id_1.second_param [ undefined ]"
"msg": "id_2.first_param [ undefined ]"
"msg": "id_2.second_param [ second ]"
Vladimir Botka
  • 5,138
  • 8
  • 20