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:
parent
504ef607f3
commit
662d34b9a7
6 changed files with 22 additions and 6 deletions
2
changelogs/fragments/moar_reserved_vars.yml
Normal file
2
changelogs/fragments/moar_reserved_vars.yml
Normal 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).
|
|
@ -31,13 +31,10 @@ from ansible.executor.stats import AggregateStats
|
||||||
from ansible.executor.task_result import TaskResult
|
from ansible.executor.task_result import TaskResult
|
||||||
from ansible.module_utils.six import string_types
|
from ansible.module_utils.six import string_types
|
||||||
from ansible.module_utils._text import to_text, to_native
|
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.playbook.play_context import PlayContext
|
||||||
from ansible.plugins.loader import callback_loader, strategy_loader, module_loader
|
from ansible.plugins.loader import callback_loader, strategy_loader, module_loader
|
||||||
from ansible.plugins.callback import CallbackBase
|
from ansible.plugins.callback import CallbackBase
|
||||||
from ansible.template import Templar
|
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.hostvars import HostVars
|
||||||
from ansible.vars.reserved import warn_if_reserved
|
from ansible.vars.reserved import warn_if_reserved
|
||||||
from ansible.utils.display import Display
|
from ansible.utils.display import Display
|
||||||
|
@ -197,8 +194,8 @@ class TaskQueueManager:
|
||||||
self.load_callbacks()
|
self.load_callbacks()
|
||||||
|
|
||||||
all_vars = self._variable_manager.get_vars(play=play)
|
all_vars = self._variable_manager.get_vars(play=play)
|
||||||
warn_if_reserved(all_vars)
|
|
||||||
templar = Templar(loader=self._loader, variables=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 = play.copy()
|
||||||
new_play.post_validate(templar)
|
new_play.post_validate(templar)
|
||||||
|
|
|
@ -65,12 +65,17 @@ def get_reserved_names(include_private=True):
|
||||||
return result
|
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 '''
|
''' 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 = set(myvars)
|
||||||
varnames.discard('vars') # we add this one internally, so safe to ignore
|
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)
|
display.warning('Found variable using reserved name: %s' % varname)
|
||||||
|
|
||||||
|
|
||||||
|
|
1
test/integration/targets/var_reserved/aliases
Normal file
1
test/integration/targets/var_reserved/aliases
Normal file
|
@ -0,0 +1 @@
|
||||||
|
shippable/posix/group2
|
|
@ -0,0 +1,6 @@
|
||||||
|
- hosts: localhost
|
||||||
|
gather_facts: false
|
||||||
|
vars:
|
||||||
|
lipsum: jinja2 uses me internally
|
||||||
|
tasks:
|
||||||
|
- debug:
|
5
test/integration/targets/var_reserved/runme.sh
Executable file
5
test/integration/targets/var_reserved/runme.sh
Executable 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'
|
Loading…
Reference in a new issue