Skip to content

Commit

Permalink
Templating: make sure only one variable results are cached (#67429)
Browse files Browse the repository at this point in the history
* Make sure only one variable results are cached.

* Add changelog.

* Add test.
  • Loading branch information
felixfontein committed Feb 19, 2020
1 parent c61c0f7 commit c520d70
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/67429-jinja2-caching.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "Templating - Ansible was caching results of Jinja2 expressions in some cases where these expressions could have dynamic results, like password generation (https://github.com/ansible/ansible/issues/34144)."
2 changes: 1 addition & 1 deletion lib/ansible/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ def template(self, variable, convert_bare=False, preserve_trailing_newlines=True
# we only cache in the case where we have a single variable
# name, to make sure we're not putting things which may otherwise
# be dynamic in the cache (filters, lookups, etc.)
if cache:
if cache and only_one:
self._cached_result[sha1_hash] = result

return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,18 @@
- name: set with_dict
shell: echo "{{ item.key + '=' + item.value }}"
with_dict: "{{ mydict }}"

# BUG #34144 bad template caching

- name: generate two random passwords
set_fact:
password1: "{{ lookup('password', '/dev/null length=20') }}"
password2: "{{ lookup('password', '/dev/null length=20') }}"
# If the passwords are generated randomly, the chance that they
# coincide is neglectable (< 1e-18 assuming 120 bits of randomness
# per password).

- name: make sure passwords are not the same
assert:
that:
- password1 != password2

0 comments on commit c520d70

Please sign in to comment.