Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified "vars:" not visible inside include_role. #69799

Open
vbotka opened this issue Jun 1, 2020 · 10 comments
Open

Modified "vars:" not visible inside include_role. #69799

vbotka opened this issue Jun 1, 2020 · 10 comments
Labels
affects_2.9 This issue/PR affects Ansible v2.9 affects_2.16 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. module This issue/PR relates to a module. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@vbotka
Copy link
Contributor

vbotka commented Jun 1, 2020

IMHO. Not included in https://github.com/ansible/ansible/projects/19

tested with ansible 2.9.6

SUMMARY

A modification of a variable declared in "vars" is not visible inside the included role.

    - include_role:
        name: test
        tasks_from: task.yml
      vars:
        my_var: A
ISSUE TYPE
  • Bug Report
COMPONENT NAME

include_role

ANSIBLE VERSION
ansible 2.9.6
  config file = /export/home/vlado.config/.ansible/ansible-examples/examples/example-136/ansible.cfg
  configured module search path = ['/home/vlado/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
CONFIGURATION
DEFAULT_HOST_LIST(/export/home/vlado.config/.ansible/ansible-examples/examples/example-136/an$
DEFAULT_ROLES_PATH(/export/home/vlado.config/.ansible/ansible-examples/examples/example-136/a$
OS / ENVIRONMENT

Linux host9 5.4.0-31-generic #35-Ubuntu SMP Thu May 7 20:20:34 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

STEPS TO REPRODUCE

Fresh role with this task only

shell> cat roles/test/tasks/task.yml
- debug:
    var: my_var
- set_fact:
    my_var: B
- debug:
    var: my_var
- hosts: localhost
  connection: local
  tasks:
    - include_role:
        name: test
        tasks_from: task.yml
        apply:
          vars:
            my_var: A
    - debug:
        var: my_var

# Variable must be declared by "apply: vars".
#
#    "my_var": "A"
#    "my_var": "B"
#    "my_var": "B"

- hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - include_role:
        name: test
        tasks_from: task.yml
      vars:
        my_var: A
    - debug:
        var: my_var

# New copy visible outside the role only will be created if the variable
# is declared by "vars:".
#
#    "my_var": "A"
#    "my_var": "A"
#    "my_var": "B"
EXPECTED RESULTS

Fix the documentation

- name: Pass variables to role
  include_role:
    name: myrole
    apply:
      vars:
        rolevar1: value from task
ACTUAL RESULTS

Quoting from Examples
https://docs.ansible.com/ansible/latest/modules/include_role_module.html#examples

- name: Pass variables to role
  include_role:
    name: myrole
  vars:
    rolevar1: value from task

@vbotka
Copy link
Contributor Author

vbotka commented Jun 1, 2020

@Akasurde
Copy link
Member

Akasurde commented Jun 1, 2020

@vbotka "Edit on GitHub fail" is known and reported here - #69683 (comment)

@bcoca
Copy link
Member

bcoca commented Jun 1, 2020

for full fix #69040

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. needs_triage Needs a first human triage before being processed. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Jun 2, 2020
@samdoran samdoran removed the needs_triage Needs a first human triage before being processed. label Jun 4, 2020
@vbotka
Copy link
Contributor Author

vbotka commented Jun 5, 2020

Is (should be/will be) any difference between these options?

    - include_role:
        name: test
        tasks_from: task.yml
        apply:
          vars:
            my_var: A

and

    - include_role:
        name: test
        tasks_from: task.yml
      vars:
          my_var: A

@bcoca
Copy link
Member

bcoca commented Jun 5, 2020

@vbotka mostly no, those should be almost the same, vars should be one of the few things that includes are both affected by and apply to included tasks, the only difference should be that the apply version defined variables would not be available to the include itself.

@bcoca bcoca added this to Triage in include and import issues via automation Jun 5, 2020
@bcoca
Copy link
Member

bcoca commented Jul 23, 2021

other possible fix #75287

@bcoca bcoca added the needs_verified This issue needs to be verified/reproduced by maintainer label Jun 1, 2022
@ansibot ansibot added the module This issue/PR relates to a module. label Jul 12, 2023
@nitzmahone nitzmahone removed the needs_verified This issue needs to be verified/reproduced by maintainer label Feb 21, 2024
@nitzmahone
Copy link
Member

Closing, since it looks like this was likely fixed awhile back. Feel free to reopen if this is still reproducible on currently-supported versions of Ansible Core.

include and import issues automation moved this from Triage to Done Feb 21, 2024
@vbotka
Copy link
Contributor Author

vbotka commented Feb 21, 2024

The problem, for your convenience also described here, is reproducible in 2.16.2

It seems, I can't reopen this issue. What do you mean by "Feel free to reopen" ?

@jborean93 jborean93 reopened this Feb 22, 2024
include and import issues automation moved this from Done to In Progress Feb 22, 2024
@sivel
Copy link
Member

sivel commented Feb 22, 2024

Just to confirm something here, the behavior questioned in this issue is expected.

This is because vars: on an include_* are considered as include params. The variable precedence from https://docs.ansible.com/ansible-core/devel/playbook_guide/playbooks_variables.html#understanding-variable-precedence shows set_fact at 19 and include params at 21.

See the following for the code definition of include params:

def get_include_params(self):
all_vars = dict()
if self._parent:
all_vars |= self._parent.get_include_params()
if self.action in C._ACTION_ALL_INCLUDES:
all_vars |= self.vars
return all_vars

As a result, set_fact inside of a role that uses vars: at the include_role level has less precedence than the vars:. Outside the role, the set_fact has the higher precedence.

When using apply: for vars: they are no longer considered include params but instead are considered block vars which are 16 on the precedence list, so a set_fact will always win over apply vars from include_*.

Based on this, it appears this is just a misunderstanding across multiple pieces of documentation. Perhaps the only thing we may be missing is a description of what include params covers.

@vbotka
Copy link
Contributor Author

vbotka commented Feb 22, 2024

vars: on an include_* are considered as include params.

Good to know.

... missing is a description of what include params covers.

... and this is known for two years.
ansible/ansible-documentation#95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects_2.9 This issue/PR affects Ansible v2.9 affects_2.16 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. module This issue/PR relates to a module. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
Development

Successfully merging a pull request may close this issue.

8 participants