From 0c32a4f7931262d48b3f4e85f2448c30dd317dae Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Thu, 27 Aug 2020 20:46:52 +0200 Subject: [PATCH] Emit proper error for `x in y` when y is undefined (#70990) (#71011) Fixes #70984 (cherry picked from commit bf7276a4e88de6e102ad06aa1d0716ae799d87ea) --- .../70984-templating-ansibleundefined-in-operator.yml | 2 ++ lib/ansible/template/__init__.py | 4 ++++ test/integration/targets/template/tasks/main.yml | 9 +++++++++ 3 files changed, 15 insertions(+) create mode 100644 changelogs/fragments/70984-templating-ansibleundefined-in-operator.yml diff --git a/changelogs/fragments/70984-templating-ansibleundefined-in-operator.yml b/changelogs/fragments/70984-templating-ansibleundefined-in-operator.yml new file mode 100644 index 00000000000..2c33155947c --- /dev/null +++ b/changelogs/fragments/70984-templating-ansibleundefined-in-operator.yml @@ -0,0 +1,2 @@ +bugfixes: + - templating - fix error message for ``x in y`` when y is undefined (https://github.com/ansible/ansible/issues/70984) diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 2184050aaab..e19c91ff6ab 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -309,6 +309,10 @@ class AnsibleUndefined(StrictUndefined): def __repr__(self): return 'AnsibleUndefined' + def __contains__(self, item): + # Return original Undefined object to preserve the first failure context + return self + class AnsibleContext(Context): ''' diff --git a/test/integration/targets/template/tasks/main.yml b/test/integration/targets/template/tasks/main.yml index 10ea474d7cf..da803436860 100644 --- a/test/integration/targets/template/tasks/main.yml +++ b/test/integration/targets/template/tasks/main.yml @@ -706,5 +706,14 @@ - 'diff_result.stdout == ""' - "diff_result.rc == 0" +- debug: + msg: "{{ 'x' in y }}" + ignore_errors: yes + register: error + +- name: check that proper error message is emitted when in operator is used + assert: + that: "\"'y' is undefined\" in error.msg" + # aliases file requires root for template tests so this should be safe - include: backup_test.yml