Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templating: make sure only one variable results are cached #67429

Merged
merged 4 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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