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

Variable in handlers of nested included role is undefined if left set to default #80459

Closed
1 task done
dmotte opened this issue Apr 10, 2023 · 2 comments · Fixed by #81524
Closed
1 task done

Variable in handlers of nested included role is undefined if left set to default #80459

dmotte opened this issue Apr 10, 2023 · 2 comments · Fixed by #81524
Labels
affects_2.14 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. verified This issue has been verified/reproduced by maintainer

Comments

@dmotte
Copy link

dmotte commented Apr 10, 2023

Summary

When I try to reference a variable in a handler of an included role B included whitin another role A and the role A is called from my playbook without specifying a value for the variable (hence leaving it set to the default value) Ansible gives me an error saying that the variable is undefined, while it should be set to the default value from role A. This is really difficult to explain, but easy to understand from the example below.

Issue Type

Bug Report

Component Name

default variable values handling

Ansible Version

$ ansible --version
ansible [core 2.14.4]
  config file = None
  configured module search path = ['/home/codespace/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/codespace/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/codespace/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/codespace/.local/bin/ansible
  python version = 3.10.4 (main, Mar 13 2023, 19:44:25) [GCC 9.4.0] (/usr/local/python/3.10.4/bin/python)
  jinja version = 3.1.2
  libyaml = True

Configuration

$ ansible-config dump --only-changed -t all
CONFIG_FILE() = None

OS / Environment

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.5 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.5 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

Steps to Reproduce

File playbook.yml:

- hosts: localhost
  connection: local
  tasks:
    - ansible.builtin.include_role: { name: ./role01 }

File role01/defaults/main.yml:

myvar01: value-for-myvar01-from-defaults

File role01/tasks/main.yml:

- ansible.builtin.debug:
    msg: myvar01 in role01 tasks is {{ myvar01 }}

- ansible.builtin.include_role: { name: ./role02 }
  vars:
    myvar02: "{{ myvar01 }}"

File role02/defaults/main.yml:

myvar02: value-for-myvar02-from-defaults

File role02/handlers/main.yml:

- name: myhandler01
  ansible.builtin.debug:
    msg: myvar02 in role02 handlers is {{ myvar02 }}

File role02/tasks/main.yml:

- ansible.builtin.debug:
    msg: myvar02 in role02 tasks is {{ myvar02 }}

- ansible.builtin.command:
    cmd: uptime
  changed_when: true
  notify: myhandler01

- name: Force all notified handlers to run at this point
  ansible.builtin.meta: flush_handlers

Then run ansible-playbook playbook.yml to see the result.

Expected Results

I expect the value of the variable to be value-for-myvar01-from-defaults instead of undefined, like this:

$ ansible-playbook playbook.yml 
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Main play] *********************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [ansible.builtin.include_role : ./role01] ***************************************************************************************************************************************************************************

TASK [./role01 : ansible.builtin.debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "myvar01 in role01 tasks is value-for-myvar01-from-defaults"
}

TASK [ansible.builtin.include_role : ./role02] ***************************************************************************************************************************************************************************

TASK [./role02 : ansible.builtin.debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "myvar02 in role02 tasks is value-for-myvar01-from-defaults"
}

TASK [./role02 : ansible.builtin.command] ********************************************************************************************************************************************************************************
changed: [localhost]

TASK [./role02 : Force all notified handlers to run at this point] *******************************************************************************************************************************************************

RUNNING HANDLER [./role02 : myhandler01] *********************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "myvar02 in role02 handlers is value-for-myvar01-from-defaults"
}

PLAY RECAP ***************************************************************************************************************************************************************************************************************
localhost                  : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Actual Results

$ ansible-playbook playbook.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Main play] *********************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [ansible.builtin.include_role : ./role01] ***************************************************************************************************************************************************************************

TASK [./role01 : ansible.builtin.debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "myvar01 in role01 tasks is value-for-myvar01-from-defaults"
}

TASK [ansible.builtin.include_role : ./role02] ***************************************************************************************************************************************************************************

TASK [./role02 : ansible.builtin.debug] **********************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "myvar02 in role02 tasks is value-for-myvar01-from-defaults"
}

TASK [./role02 : ansible.builtin.command] ********************************************************************************************************************************************************************************
changed: [localhost]

TASK [./role02 : Force all notified handlers to run at this point] *******************************************************************************************************************************************************

RUNNING HANDLER [./role02 : myhandler01] *********************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: {{ myvar01 }}: 'myvar01' is undefined. 'myvar01' is undefined. {{ myvar01 }}: 'myvar01' is undefined. 'myvar01' is undefined\n\nThe error appears to be in '/workspaces/test-ansible-nested/role02/handlers/main.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: myhandler01\n  ^ here\n"}

PLAY RECAP ***************************************************************************************************************************************************************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibot
Copy link
Contributor

ansibot commented Apr 10, 2023

Files identified in the description:
None

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.14 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. labels Apr 10, 2023
@s-hertel s-hertel added needs_verified This issue needs to be verified/reproduced by maintainer and removed needs_triage Needs a first human triage before being processed. labels Apr 11, 2023
mkrizek added a commit to mkrizek/ansible that referenced this issue Aug 16, 2023
@mkrizek mkrizek added verified This issue has been verified/reproduced by maintainer and removed needs_verified This issue needs to be verified/reproduced by maintainer labels Aug 16, 2023
@ansibot ansibot added the has_pr This issue has an associated PR. label Aug 16, 2023
s-hertel pushed a commit that referenced this issue Aug 17, 2023
* include_role: expose vars from parent roles to role's handlers

Fixes #80459
@dmotte
Copy link
Author

dmotte commented Aug 17, 2023

Thank you so much guys! 😄 I almost lost hope haha but I'm glad you finally fixed it

mkrizek added a commit to mkrizek/ansible that referenced this issue Aug 21, 2023
…le#81524)

* include_role: expose vars from parent roles to role's handlers

Fixes ansible#80459

(cherry picked from commit 98f1627)
mkrizek added a commit to mkrizek/ansible that referenced this issue Aug 31, 2023
@ansible ansible locked and limited conversation to collaborators Aug 31, 2023
sivel pushed a commit that referenced this issue Aug 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.14 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. verified This issue has been verified/reproduced by maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants