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 <akasurde@redhat.com>
This commit is contained in:
Brian Coca 2020-08-05 10:43:31 -04:00 committed by GitHub
parent 504ef607f3
commit 662d34b9a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 6 deletions

View file

@ -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).

View file

@ -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)

View file

@ -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)

View file

@ -0,0 +1 @@
shippable/posix/group2

View file

@ -0,0 +1,6 @@
- hosts: localhost
gather_facts: false
vars:
lipsum: jinja2 uses me internally
tasks:
- debug:

View file

@ -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'