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
Ansible sub-role loads variables from parent role #19649
Comments
fix issues when using role as dependencies ansible/ansible#19649
+1 I am facing similar issue when running a dependency role declare in meta/main.yml, where both roles has include_vars to load distribution specific variables. - name: Gather OS Specific Variables
include_vars: "{{ item }}"
with_first_found:
- "{{ ansible_distribution }}-{{ ansible_distribution_version }}.yml"
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
- "{{ ansible_distribution }}.yml"
- "{{ ansible_os_family }}.yml"
- "default.yml"
>>> Working on ansible version : latest
'source ~/.bashrc && workon ansible_latest && ansible-playbook /etc/ansible/roles/role_under_test/playbooks/test.yml && deactivate ; (exit $?)'
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [yabhinav.common : Gather OS Specific Variables] **************************
ok: [localhost] => (item=/etc/ansible/roles/yabhinav.common/vars/CentOS-7.yml)
TASK [yabhinav.common : Ensure NTP Synchronised] *******************************
ok: [localhost]
TASK [role_under_test : Gather OS Specific Variables] **************************
ok: [localhost] => (item=/etc/ansible/roles/role_under_test/vars/RedHat.yml)
TASK [role_under_test : Install IPA Client Packages] ***************************
changed: [localhost] => (item=[u'ipa-client', u'ipa-admintools', u'sssd-ipa', u'sssd-ldap', u'ipa-python', u'libipa_hbac', u'python-libipa_hbac', u'python-iniparse'])
As you can see above dependency role 'source ~/.bashrc && workon ansible_latest && ansible-playbook /etc/ansible/roles/role_under_test/playbooks/test.yml && deactivate ; (exit $?)'
/root/.virtualenvs/ansible_latest/lib/python2.6/site-packages/cryptography/__init__.py:26: DeprecationWarning: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of cryptography will drop support for Python 2.6
DeprecationWarning
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [yabhinav.common : Gather OS Specific Variables] **************************
ok: [localhost] => (item=/etc/ansible/roles/role_under_test/vars/RedHat.yml)
TASK [yabhinav.common : Install Common Packages] *******************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "'common_pkgs' is undefined"}
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=1 And as you can see the yabhinav.common/tasks/main.yml is loading variables from Even using relative path in the file names doesnt work - name: Gather OS Specific Variables
include_vars: "{{ item }}"
with_first_found:
- "../vars/{{ ansible_distribution }}-{{ ansible_distribution_version }}.yml"
- "../vars/{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
- "../vars/{{ ansible_distribution }}.yml"
- "../vars/{{ ansible_os_family }}.yml"
- "../vars/default.yml" This bug exist in all previous and latest ansible versions and needs an immediate path... reproduced in ansible versions 2.0.0.0, 2.1.0.0, 2.2.0.0 and 2.2.1.0 |
So I worked around the issue adding
Not convenient, but works. Still waiting for a fix. |
@afunix Thanks for the work around!! I would suggest a slightly better improvement : custom-role-name can be differently set in ansible galaxy but by default it will be same as gitHub-role-name, but still considering the . prefix its better we use {{role_path}} or {{role_path|basename}} - name: Gather OS Specific Variables
include_vars: "{{ item }}"
with_first_found:
- files:
- "{{ansible_distribution}}-{{ansible_distribution_version}}.yml"
- "{{ansible_distribution}}-{{ansible_distribution_major_version}}.yml"
- "{{ansible_distribution}}.yml"
- "{{ansible_os_family}}.yml"
- "{{ansible_system}}.yml"
paths:
- "{{ role_path }}/vars" I also notice that I have name my var PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [role_under_test : Gather OS Specific Variables] **************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "No file was found when using with_first_found. Use the 'skip: true' option to allow this task to be skipped if no files are found"}
PLAY RECAP *********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1 PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [role_under_test : Gather OS Specific Variables] **************************
ok: [localhost] => (item=/etc/ansible/roles/role_under_test/vars/RedHat.yml)
Although this has nothing to with current bug. |
hotfixed issue: ansible/ansible#19649
This is working as intended as described in the documentation. Adding We should probably improve the documentation to describe the behavior more explicitly, possibly adding an example to the |
Closing as per above |
ISSUE TYPE
COMPONENT NAME
include_vars
ANSIBLE VERSION
CONFIGURATION
OS / ENVIRONMENT
N/A
SUMMARY
include_vars
documentation says thatfile
parameteris relative, it will look for the file in vars/ subdirectory of a role or relative to playbook
.However when I use
include_vars
withwith_first_found
in sub-role variables are loaded from master role.I have lots of roles which try to load variables based on
ansible_distribution
,ansible_os_family
oransible_system
.I also have lots of roles which depend on each other.
I need a way to load variables relative to the role itself, just like documentation states.
STEPS TO REPRODUCE
playbook.yml:
roles/master/meta/main.yml:
roles/slave/tasks/main.yml:
roles/master/vars/test_master.yml:
roles/slave/vars/test_slave.yml:
EXPECTED RESULTS
Expected
variable
to beslave
as there is notest_master.yml
inrole/slave/vars
, so sensible should loadtest_slave.yml
according to documentation.ACTUAL RESULTS
variable
ismaster
.The text was updated successfully, but these errors were encountered: