Skip to content

Variable Passing Issue in Repeated Role Invocation – components Undefined in Second Role CallΒ #3193

@jijeesh

Description

@jijeesh

🐞 Defect Description

When invoking the same role (test) multiple times in an Ansible playbook using the roles: keyword, variables such as components and operation are not passed correctly in the second invocation. This results in undefined errors inside the role.

Example:

- name: Role Testing variable passing
  hosts: all
  remote_user: root
  any_errors_fatal: true

  roles:
    - role: test
      vars:
        components: ["rep"]
        operation: "start"

- name: Role Testing2 variable passing
  hosts: all
  remote_user: root
  any_errors_fatal: true

  roles:
    - role: test
      vars:
        components: ["rep"]
        operation: "start"

In the second block, components and operation are not accessible inside the test role.

Workaround:
Replacing roles: with include_role: resolves the issue, and variables are passed correctly.


πŸ’Ό Justification for Business Impact

  • Causes unexpected behavior and runtime errors during playbook execution.
  • Breaks modularity and reusability of roles.
  • Increases debugging time and complexity for developers.
  • Impacts automation reliability in production environments.

πŸ§ͺ Environment Details

  • Ansible Version: 9.7.0
  • Ansible Core Version: 2.16.8
  • Base Image: docker.io/library/alpine:3.20

βœ… Suggested Action

  • Investigate variable scoping behavior in repeated roles: declarations.
  • Consider switching to include_role for dynamic variable passing.
  • Document this behavior in the role usage guidelines to prevent future issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs_triageNeeds a first human triage before being processed.new_contributorThis PR is the first contribution by a new community member.

    Type

    No type

    Projects

    Status

    πŸ†• Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions