From 662d34b9a7a1b3ab1d4847eeaef201a005826aef Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 5 Aug 2020 10:43:31 -0400 Subject: [PATCH] add jinja2 global to reserved names (#71088) * add jinja2 global to reserved names also allow expansion by additional context provided from caller fixes #41955 Co-authored-by: Abhijeet Kasurde --- changelogs/fragments/moar_reserved_vars.yml | 2 ++ lib/ansible/executor/task_queue_manager.py | 5 +---- lib/ansible/vars/reserved.py | 9 +++++++-- test/integration/targets/var_reserved/aliases | 1 + .../targets/var_reserved/reserved_varname_warning.yml | 6 ++++++ test/integration/targets/var_reserved/runme.sh | 5 +++++ 6 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/moar_reserved_vars.yml create mode 100644 test/integration/targets/var_reserved/aliases create mode 100644 test/integration/targets/var_reserved/reserved_varname_warning.yml create mode 100755 test/integration/targets/var_reserved/runme.sh diff --git a/changelogs/fragments/moar_reserved_vars.yml b/changelogs/fragments/moar_reserved_vars.yml new file mode 100644 index 00000000000..50a501e65d8 --- /dev/null +++ b/changelogs/fragments/moar_reserved_vars.yml @@ -0,0 +1,2 @@ +bugfixes: + - Handle more varnames that can create conflicts, expand a function in general, handle jinja2 globals in particular (https://github.com/ansible/ansible/issues/41955). diff --git a/lib/ansible/executor/task_queue_manager.py b/lib/ansible/executor/task_queue_manager.py index e7277c9183c..07ca5c42e81 100644 --- a/lib/ansible/executor/task_queue_manager.py +++ b/lib/ansible/executor/task_queue_manager.py @@ -31,13 +31,10 @@ from ansible.executor.stats import AggregateStats from ansible.executor.task_result import TaskResult from ansible.module_utils.six import string_types from ansible.module_utils._text import to_text, to_native -from ansible.playbook.block import Block from ansible.playbook.play_context import PlayContext from ansible.plugins.loader import callback_loader, strategy_loader, module_loader from ansible.plugins.callback import CallbackBase from ansible.template import Templar -from ansible.utils.collection_loader import AnsibleCollectionRef -from ansible.utils.helpers import pct_to_int from ansible.vars.hostvars import HostVars from ansible.vars.reserved import warn_if_reserved from ansible.utils.display import Display @@ -197,8 +194,8 @@ class TaskQueueManager: self.load_callbacks() all_vars = self._variable_manager.get_vars(play=play) - warn_if_reserved(all_vars) templar = Templar(loader=self._loader, variables=all_vars) + warn_if_reserved(all_vars, templar.environment.globals.keys()) new_play = play.copy() new_play.post_validate(templar) diff --git a/lib/ansible/vars/reserved.py b/lib/ansible/vars/reserved.py index d7daea07057..2a2ec8de1b5 100644 --- a/lib/ansible/vars/reserved.py +++ b/lib/ansible/vars/reserved.py @@ -65,12 +65,17 @@ def get_reserved_names(include_private=True): return result -def warn_if_reserved(myvars): +def warn_if_reserved(myvars, additional=None): ''' this function warns if any variable passed conflicts with internally reserved names ''' + if additional is None: + reserved = _RESERVED_NAMES + else: + reserved = _RESERVED_NAMES.union(additional) + varnames = set(myvars) varnames.discard('vars') # we add this one internally, so safe to ignore - for varname in varnames.intersection(_RESERVED_NAMES): + for varname in varnames.intersection(reserved): display.warning('Found variable using reserved name: %s' % varname) diff --git a/test/integration/targets/var_reserved/aliases b/test/integration/targets/var_reserved/aliases new file mode 100644 index 00000000000..765b70da796 --- /dev/null +++ b/test/integration/targets/var_reserved/aliases @@ -0,0 +1 @@ +shippable/posix/group2 diff --git a/test/integration/targets/var_reserved/reserved_varname_warning.yml b/test/integration/targets/var_reserved/reserved_varname_warning.yml new file mode 100644 index 00000000000..1bdb34a02fc --- /dev/null +++ b/test/integration/targets/var_reserved/reserved_varname_warning.yml @@ -0,0 +1,6 @@ +- hosts: localhost + gather_facts: false + vars: + lipsum: jinja2 uses me internally + tasks: + - debug: diff --git a/test/integration/targets/var_reserved/runme.sh b/test/integration/targets/var_reserved/runme.sh new file mode 100755 index 00000000000..3c3befbd9f9 --- /dev/null +++ b/test/integration/targets/var_reserved/runme.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +set -eux + +ansible-playbook reserved_varname_warning.yml "$@" 2>&1| grep 'Found variable using reserved name: lipsum'