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

Errors can be wrongly replaced on dynamically imported static include #34665

Closed
johnboy2 opened this issue Jan 9, 2018 · 6 comments · Fixed by #39365
Closed

Errors can be wrongly replaced on dynamically imported static include #34665

johnboy2 opened this issue Jan 9, 2018 · 6 comments · Fixed by #39365
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@johnboy2
Copy link
Contributor

johnboy2 commented Jan 9, 2018

ISSUE TYPE
  • Bug Report
COMPONENT NAME

include_tasks

ANSIBLE VERSION
ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Nov 20 2017, 18:23:56) [GCC 5.4.0 20160609]
CONFIGURATION
ANSIBLE_PIPELINING(/mnt/speedyg/johnn/Documents/EclipseWS/cloud-network/ansible.cfg) = True
ANSIBLE_SSH_ARGS(/mnt/speedyg/johnn/Documents/EclipseWS/cloud-network/ansible.cfg) = -o ControlMaster=auto -o ControlPersist=300s -o UserKnownHostsFile=./ssh_known_hosts
DEFAULT_BECOME(/mnt/speedyg/johnn/Documents/EclipseWS/cloud-network/ansible.cfg) = True
DEFAULT_LOG_PATH(/mnt/speedyg/johnn/Documents/EclipseWS/cloud-network/ansible.cfg) = /mnt/speedyg/johnn/Documents/EclipseWS/cloud-network/ansible.log
DEFAULT_REMOTE_USER(/mnt/speedyg/johnn/Documents/EclipseWS/cloud-network/ansible.cfg) = automation
DEFAULT_ROLES_PATH(/mnt/speedyg/johnn/Documents/EclipseWS/cloud-network/ansible.cfg) = [u'/mnt/speedyg/johnn/Documents/EclipseWS/cloud-network/roles']
OS / ENVIRONMENT

Management node: Ubuntu 16.04
Remote node: Debian 9

SUMMARY

Untrue/incorrect error message shown when a dynamically-determined dynamic include contains an embedded reference to an unknown file.

STEPS TO REPRODUCE

test.yml:

- hosts: all
  tasks:
  - debug: var=ansible_system
  - include_tasks: '{{ ansible_system }}.yml'

Linux.yml:

- import_tasks: a_file_that_does_not_exist.yml

Execute command:

ansible-playbook -l "pick_an_inventory_host" test.yml
EXPECTED RESULTS

The debug task should execute as expected, showing the remote system's platform (expected in this case to be "Linux").

For the "include_tasks" task, it should result in an error along the lines of:

FAILED! => {"reason": "Unable to retrieve file contents\nCould not find or access '...'"}

(which is what you get with a straight-up static include of a missing file)

ACTUAL RESULTS

Debug task executes as expected (which also proves that ansible_system is defined).

The "import_tasks" task fails, as expected, but does so with an entirely incorrect error message:

FAILED! => {"reason": "'ansible_system' is undefined"}

Ansible thus hides the actual fault in this case behind a completely false error.

@ansibot
Copy link
Contributor

ansibot commented Jan 9, 2018

@johnboy2 Greetings! Thanks for taking the time to open this issue. In order for the community to handle your issue effectively, we need a bit more information.

Here are the items we could not find in your description:

  • component name

Please set the description of this issue with this template:
https://raw.githubusercontent.com/ansible/ansible/devel/.github/ISSUE_TEMPLATE.md

click here for bot help

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 bug_report needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Jan 9, 2018
@johnboy2
Copy link
Contributor Author

johnboy2 commented Jan 10, 2018

!component +modules/utilities/logic/include_tasks.py

@ansibot ansibot added the bot_broken The bot is misbehaving. NOT for failing CI. A staff member will investigate. label Jan 10, 2018
@sivel sivel removed the bot_broken The bot is misbehaving. NOT for failing CI. A staff member will investigate. label Jan 10, 2018
@sivel
Copy link
Member

sivel commented Jan 10, 2018

!component +lib/ansible/modules/utilities/logic/include_tasks.py
!component +lib/ansible/plugins/callback/default.py

@sivel sivel removed needs_info This issue requires further information. Please answer any outstanding questions. needs_triage Needs a first human triage before being processed. labels Jan 10, 2018
@ansibot ansibot added module This issue/PR relates to a module. and removed needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. labels Jan 10, 2018
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 1, 2018
@bcoca bcoca added this to Pending in include and import issues Apr 17, 2018
@sivel
Copy link
Member

sivel commented Apr 25, 2018

This actually has nothing to do with a_file_that_does_not_exist.yml not existing.

The problem is that, in order for a relatively pathed included file to be found, we look at the file of the parent include, to ensure we start with the correct directory.

In this case, the path of the parent include is defined as {{ ansible_system }}.yml. Because that contains a variable, we then try to template it.

When we try to template it in ansible.playbook.helpers.load_list_of_tasks we no longer have host context, and as such, we cannot template ansible_system as we have no record of that variable.

If we were to have defined ansible_system in another way, we would have gotten an error indicating that a_file_that_does_not_exist.yml does not exist.

!component -lib/ansible/plugins/callback/default.py
!component +lib/ansible/playbook/helpers.py

@sivel
Copy link
Member

sivel commented Apr 25, 2018

A not so correct solution, but a solution nonetheless:

diff --git a/lib/ansible/playbook/helpers.py b/lib/ansible/playbook/helpers.py
index 248a840bb0..6518ace88f 100644
--- a/lib/ansible/playbook/helpers.py
+++ b/lib/ansible/playbook/helpers.py
@@ -178,7 +178,10 @@ def load_list_of_tasks(ds, play, block=None, role=None, task_include=None, use_h
                         if not isinstance(parent_include, TaskInclude):
                             parent_include = parent_include._parent
                             continue
-                        parent_include_dir = os.path.dirname(templar.template(parent_include.args.get('_raw_params')))
+                        try:
+                            parent_include_dir = os.path.dirname(templar.template(parent_include.args.get('_raw_params')))
+                        except AnsibleUndefinedVariable:
+                            parent_include_dir = ''
                         if cumulative_path is None:
                             cumulative_path = parent_include_dir
                         elif not os.path.isabs(cumulative_path):

Ideally we would have to implement some form of path inheritance that didn't depend on host context. Which would mean every consolidated include result, would need a unique parent with vars pre-resolved for later.

That isn't a fix that is simple, and not something that can make it into 2.6.

The above fix just does the best we can, and assumes that the included file is immediately relative.

@sivel
Copy link
Member

sivel commented Apr 26, 2018

Resolved in #39365

@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.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. module This issue/PR relates to a module. 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.

3 participants