-1

In my playbook I have:

roles:
  - role: role_1
  - role: role_2

In my roles directory structure I have defined vars with different values under role_1 and role_2

When invoking the playbook, I am trying to ask it to only consider the role I am interested in at the moment, by using limit:

ansible-playbook -i ./hosts.yml  myplaybook.yml --limit role_1

What I expect: It will disregard the variable values under role_2, because I asked it to limit to role_1 What I get: role_2 variables values override role_1, because role_2 is lexicographically later. ugh!

What else I tried: I tried to use tags:

roles:
  - role: role_1
    tags: [ role_1_tag ]
  - role: role_2
    tags: [ role_2_tag ]

Result is still the undesired

Edit: After following the first answer

The syntax for include_role as given here (and in the documentation) does not work without modifications. In the end I made this:

tasks:
  - name: include role_1
    include_role:
      name: role1
      apply:
        tags:
          - role_1_tag
    tags: always

  - name: include role_2
    include_role:
      name: role_2
      apply:
        tags:
          - role_2_tag
    tags: always

But even then, When I run it, nothing is executed. I run it like this:

ansible-playbook -i ./hosts.yml % --limit role_1 -t role_1_tag

Trying another way:

tags: always
tasks:
  - name: include role_1
    include_role:
      name: role1
      apply:
        tags:
          - role_1_tag


  - name: include role_2
    include_role:
      name: role_2
      apply:
        tags:
          - role_2_tag

Ansible tries to proceed with the other tasks, but without accessing any of the vars under the role, resulting in variable not found error

Since I put the tags: outside of tasks I guessed the tags need to be mentioned for every task. Even doing that, I got the same result, which is:

fatal: [host_group]: FAILED! => {"msg": "'my_vars' is undefined"}

Edit2: Using public: yes seems to get Ansible to read the vars, however, again, it reads them all, and --limit has no effect

gk_2000
  • 194
  • 3
  • 16

1 Answers1

0

Q: "Only consider the role I am interested in at the moment."

A: Use include_role. For example

- include_role:
    name: role_1
- include_role:
    name: role_2

By default, the parameter public is no

This option dictates whether the role's vars and defaults are exposed to the playbook. If set to yes the variables will be available to tasks following the include_role task. This functionality differs from standard variable exposure for roles listed under the roles header or import_role as they are exposed at playbook parsing time, and available to earlier roles and tasks as well.


Notes

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63