mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Prevent vars premature templating (#56117)
Avoid premature vars templating * added tests * avoid 'is template' warning in vars, since we want them for latter templating
This commit is contained in:
parent
3095df099d
commit
1da47bfa8c
6 changed files with 38 additions and 2 deletions
2
changelogs/fragments/vars_prematurely_template.yaml
Normal file
2
changelogs/fragments/vars_prematurely_template.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- we don't really need to template vars on definition as we do this on demand in templating.
|
|
@ -348,7 +348,9 @@ class FieldAttributeBase(with_metaclass(BaseMeta, object)):
|
||||||
|
|
||||||
if attribute.static:
|
if attribute.static:
|
||||||
value = getattr(self, name)
|
value = getattr(self, name)
|
||||||
if templar.is_template(value):
|
|
||||||
|
# we don't template 'vars' but allow template as values for later use
|
||||||
|
if name not in ('vars',) and templar.is_template(value):
|
||||||
display.warning('"%s" is not templatable, but we found: %s, '
|
display.warning('"%s" is not templatable, but we found: %s, '
|
||||||
'it will not be templated and will be used "as is".' % (name, value))
|
'it will not be templated and will be used "as is".' % (name, value))
|
||||||
continue
|
continue
|
||||||
|
@ -592,7 +594,7 @@ class Base(FieldAttributeBase):
|
||||||
_remote_user = FieldAttribute(isa='string', default=context.cliargs_deferred_get('remote_user'))
|
_remote_user = FieldAttribute(isa='string', default=context.cliargs_deferred_get('remote_user'))
|
||||||
|
|
||||||
# variables
|
# variables
|
||||||
_vars = FieldAttribute(isa='dict', priority=100, inherit=False)
|
_vars = FieldAttribute(isa='dict', priority=100, inherit=False, static=True)
|
||||||
|
|
||||||
# module default params
|
# module default params
|
||||||
_module_defaults = FieldAttribute(isa='list', extend=True, prepend=True)
|
_module_defaults = FieldAttribute(isa='list', extend=True, prepend=True)
|
||||||
|
|
1
test/integration/targets/var_templating/aliases
Normal file
1
test/integration/targets/var_templating/aliases
Normal file
|
@ -0,0 +1 @@
|
||||||
|
shippable/posix/group3
|
12
test/integration/targets/var_templating/runme.sh
Executable file
12
test/integration/targets/var_templating/runme.sh
Executable file
|
@ -0,0 +1,12 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -eux
|
||||||
|
|
||||||
|
# this should succeed since we override the undefined variable
|
||||||
|
ansible-playbook undefined.yml -i inventory -v "$@" -e '{"mytest": False}'
|
||||||
|
|
||||||
|
# this should still work, just show that var is undefined in debug
|
||||||
|
ansible-playbook undefined.yml -i inventory -v "$@"
|
||||||
|
|
||||||
|
# this should work since we dont use the variable
|
||||||
|
ansible-playbook undall.yml -i inventory -v "$@"
|
6
test/integration/targets/var_templating/undall.yml
Normal file
6
test/integration/targets/var_templating/undall.yml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
- hosts: localhost
|
||||||
|
gather_facts: false
|
||||||
|
tasks:
|
||||||
|
- debug:
|
||||||
|
vars:
|
||||||
|
mytest: '{{ und }}'
|
13
test/integration/targets/var_templating/undefined.yml
Normal file
13
test/integration/targets/var_templating/undefined.yml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
- hosts: localhost
|
||||||
|
gather_facts: false
|
||||||
|
tasks:
|
||||||
|
- name: show defined/undefined var
|
||||||
|
debug: var=mytest
|
||||||
|
vars:
|
||||||
|
mytest: '{{ und }}'
|
||||||
|
register: var_undefined
|
||||||
|
|
||||||
|
- name: ensure either mytest is defined or debug finds it to be undefined
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- mytest is defined or 'VARIABLE IS NOT DEFINED!' in var_undefined['mytest']
|
Loading…
Reference in a new issue