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

Host variables adjacent to master playbook are unavailable to included tasks #32936

Closed
Azenet opened this issue Nov 15, 2017 · 3 comments
Closed
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@Azenet
Copy link

Azenet commented Nov 15, 2017

ISSUE TYPE
  • Bug Report
COMPONENT NAME

Core

ANSIBLE VERSION
ansible 2.5.0 (devel 53ade280a3) last updated 2017/11/15 16:48:34 (GMT +200)
  config file = /home/ashka/.ansible.cfg
  configured module search path = ['/home/ashka/build/ansible/library']
  ansible python module location = /home/ashka/build/ansible/lib/ansible
  executable location = /home/ashka/build/ansible/bin/ansible
  python version = 3.6.3 (default, Oct 24 2017, 14:48:20) [GCC 7.2.0]

Note that this happens on 2.4.1.0 as well, which is the version I first had the issue with:

ansible 2.4.1.0
  config file = /home/ashka/.ansible.cfg
  configured module search path = ['/home/ashka/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/ashka/.local/lib/python3.6/site-packages/ansible
  executable location = /home/ashka/.local/bin/ansible
  python version = 3.6.3 (default, Oct 24 2017, 14:48:20) [GCC 7.2.0]
CONFIGURATION

DEFAULT_GATHERING(/home/ashka/.ansible.cfg) = implicit
DEFAULT_HOST_LIST(/home/ashka/.ansible.cfg) = ['/home/ashka/.ansible/hosts']
DEFAULT_MODULE_PATH(env: ANSIBLE_LIBRARY) = ['/home/ashka/build/ansible/library']

OS / ENVIRONMENT

N/A

SUMMARY

When including tasks that are included in an included playbook, and when the included playbook is not in the same directory as the master playbook, the host vars contained in a folder named host_vars that is adjacent to the master playbook are not available in the tasks file. This happens using import_tasks as well as include_tasks.

STEPS TO REPRODUCE
  • Create a playbook.yml with the following content:
# playbook.yml
---
- hosts: 'localhost'
  become: false
- import_playbook: a/included.yml
  • Create an a folder and the included.yml to include inside the a folder:
# a/included.yml
---
- hosts: 'localhost'
  become: false
  tasks:
    - import_tasks: b/tasks.yml
  • Inside the a folder, create a b folder and add a tasks.yml file inside:
# a/b/tasks.yml
---
- name: debug in imported tasks in imported playbook
  debug:
    var: testing
  • Create variables:
# host_vars/localhost
---
testing: "Hello"
  • Run playbook:
$ ansible-playbook playbook.yml
EXPECTED RESULTS

Variable is defined and its value is displayed. This used to be the case in 2.3 using include. As far as I have tested, the same behavior is observed when used include.

ACTUAL RESULTS

Variable is not defined.

TASK [debug in imported tasks in imported playbook] ***
task path: /.../a/b/tasks.yml:2
ok: [localhost] => {
    "testing": "VARIABLE IS NOT DEFINED!: 'testing' is undefined"
}

When moving a/included.yml in the same folder as playbook.yml and editing playbook.yml to include included.yml, then editing included.yml to include a/b/tasks.yml, the variable is defined:

TASK [debug in imported tasks in imported playbook] ***
task path: /.../a/b/tasks.yml:2
ok: [localhost] => {
    "testing": "Hello"
}
@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 bug_report 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 Nov 15, 2017
@Azenet Azenet changed the title Host variables adjacent to folder are unavailable to included tasks Host variables adjacent to master playbook are unavailable to included tasks Nov 15, 2017
@samdoran
Copy link
Contributor

samdoran commented Nov 16, 2017

We're not sure this is a bug, and we don't mean for this to be confrontational. Let's explain what we're thinking:

  • In Ansible < 2.4.1, we do not pick up new variables adjacent to included playbooks
  • In Ansible < 2.4.1, Variables are included adjacent to the initial playbook, not any included playbooks in subdirectories
    * In Ansible >= 2.4.1, you can set PLAYBOOK_VARS_ROOT to change this behavior

If you need variables to be included adjacent to included playbooks in Ansible < 2.4.1, use vars_files, include_vars, or inventory group/host_vars.

As such, we're going to close this ticket. However, we're open to being corrected, should you wish to discuss. You can stop by one of our two mailing lists or IRC to talk about this and we might be persuaded otherwise.

Thank you once again for this and your interest in Ansible!

@s-hertel s-hertel removed the needs_triage Needs a first human triage before being processed. label Nov 17, 2017
@lufik
Copy link
Contributor

lufik commented Dec 19, 2017

I have the same problem with my playbooks with ansible 2.4.1. I think this is not related to tasks but to included playbooks from another directory (as group/host vars are loaded with every included/imported playbook - so the another_directory is the problem).

The PLAYBOOK_VARS_ROOT config doesn't change anything (it's only task related not playbook related). Shortly >=2.4.0 breaks the backward compatibility for including playbooks (maybe the caption of this issue should be changed also).

The workaround for this (on linux) is to symlink the group_vars and host_vars directories to every directory which contains included playbook otherwise you loose all group and host variables in the included playbook. The workaround for the example is: ln -s ../host_vars a/host_vars

I see the point with vars_files or include_vars but it's weird to load all group and host vars via those features, isn't it?

BTW the use case could even be more simplified and a little bit more descriptive.

  • Create a playbook.yml with the following content:
# playbook.yml
---
- hosts: 'localhost'
  become: false
  tasks:
    - name: debug testing
      debug: var=testing

- import_playbook: a/included.yml
  • Create an a folder and the included.yml to include inside the a folder:
# a/included.yml
---
- hosts: 'localhost'
  become: false
  tasks:
    - name: debug testing
      debug: var=testing
  • Create variable:
# host_vars/localhost
---
testing: "Hello"

@samdoran
Copy link
Contributor

@lufik

I see the point with vars_files or include_vars but it's weird to load all group and host vars via those features, isn't it?

You're correct. As you pointed out, using group_vars makes more sense and is the way to handle this.

See this comment for further details on how variables are handled with import_playbook. We also recently added documentation to explain this further.

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

5 participants