From d608eb95308238158cf110de86d7a718e9a91dca Mon Sep 17 00:00:00 2001 From: Pilou Date: Fri, 2 Feb 2018 18:25:13 +0100 Subject: [PATCH] Check that AnsibleUndefinedVariable doesn't occur when an unused variable references an undefined variable (#35571) * Check that AnsibleUndefinedVariable doesn't occur * AnsibleUndefinedVariable exc.: don't modify type --- lib/ansible/template/vars.py | 4 +++- test/integration/targets/template/runme.sh | 8 ++++++++ test/integration/targets/template/template.yml | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100755 test/integration/targets/template/runme.sh create mode 100644 test/integration/targets/template/template.yml diff --git a/lib/ansible/template/vars.py b/lib/ansible/template/vars.py index 1b95a3eba4..9bca62a300 100644 --- a/lib/ansible/template/vars.py +++ b/lib/ansible/template/vars.py @@ -23,7 +23,7 @@ from collections import Mapping from jinja2.utils import missing -from ansible.errors import AnsibleError +from ansible.errors import AnsibleError, AnsibleUndefinedVariable from ansible.module_utils.six import iteritems from ansible.module_utils._text import to_native @@ -105,6 +105,8 @@ class AnsibleJ2Vars(Mapping): value = None try: value = self._templar.template(variable) + except AnsibleUndefinedVariable: + raise except Exception as e: msg = getattr(e, 'message') or to_native(e) raise AnsibleError("An unhandled exception occurred while templating '%s'. " diff --git a/test/integration/targets/template/runme.sh b/test/integration/targets/template/runme.sh new file mode 100755 index 0000000000..74a3344d93 --- /dev/null +++ b/test/integration/targets/template/runme.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -eux + +ANSIBLE_ROLES_PATH=../ ansible-playbook template.yml -i ../../inventory -e @../../integration_config.yml -v "$@" + +# Test for #35571 +ansible testhost -i testhost, -m debug -a 'msg={{ hostvars["localhost"] }}' -e "vars1={{ undef }}" -e "vars2={{ vars1 }}" diff --git a/test/integration/targets/template/template.yml b/test/integration/targets/template/template.yml new file mode 100644 index 0000000000..d33293bed8 --- /dev/null +++ b/test/integration/targets/template/template.yml @@ -0,0 +1,4 @@ +- hosts: testhost + gather_facts: yes + roles: + - { role: template }