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>
pull/70721/merge
Brian Coca 4 years ago committed by GitHub
parent 504ef607f3
commit 662d34b9a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

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

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

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

@ -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'
Loading…
Cancel
Save