Skip to content

Commit

Permalink
Prevent vars premature templating (ansible#56117)
Browse files Browse the repository at this point in the history
Avoid premature vars templating
  * added tests
  * avoid 'is template' warning in vars, since we want them for latter templating
  • Loading branch information
bcoca authored May 23, 2019
1 parent 3095df0 commit 1da47bf
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/vars_prematurely_template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- we don't really need to template vars on definition as we do this on demand in templating.
6 changes: 4 additions & 2 deletions lib/ansible/playbook/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,9 @@ def post_validate(self, templar):

if attribute.static:
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, '
'it will not be templated and will be used "as is".' % (name, value))
continue
Expand Down Expand Up @@ -592,7 +594,7 @@ class Base(FieldAttributeBase):
_remote_user = FieldAttribute(isa='string', default=context.cliargs_deferred_get('remote_user'))

# variables
_vars = FieldAttribute(isa='dict', priority=100, inherit=False)
_vars = FieldAttribute(isa='dict', priority=100, inherit=False, static=True)

# module default params
_module_defaults = FieldAttribute(isa='list', extend=True, prepend=True)
Expand Down
1 change: 1 addition & 0 deletions test/integration/targets/var_templating/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shippable/posix/group3
12 changes: 12 additions & 0 deletions test/integration/targets/var_templating/runme.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 0 deletions test/integration/targets/var_templating/undall.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- hosts: localhost
gather_facts: false
tasks:
- debug:
vars:
mytest: '{{ und }}'
13 changes: 13 additions & 0 deletions test/integration/targets/var_templating/undefined.yml
Original file line number Diff line number Diff line change
@@ -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']

0 comments on commit 1da47bf

Please sign in to comment.