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
template: Add option to lstrip_blocks' and fix setting
trim_blocks` inline
#37478
template: Add option to lstrip_blocks' and fix setting
trim_blocks` inline
#37478
Conversation
This looks good. Please add integration tests and I'll merge it. |
Added integration tests as requested. |
Please do not merge this yet. It seems that |
72bb9e2
to
65ef531
Compare
Fixed |
lstrip_blocks' and fix setting
trim_blocks` inline
65ef531
to
f8f0255
Compare
It seems to me that tests fail on centos6 because the |
Thanks for adding tests! Raise an Also, please state in the option description that it requires Jinja2 >= 2.7. |
# VERIFY trim_blocks | ||
|
||
- name: Render a template with "trim_blocks" set to False | ||
template: src=trim_blocks.j2 dest={{output_dir}}/trim_blocks_false.templated trim_blocks=False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use multi-line YAML rather than k=v
syntax. I know the rest of the tasks use k=v
syntax, but they need to be changed at some point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted, it's just that my commit for trim_blocks
fixes exactly that, that the k=v
syntax for trim_blocks
was not working. I will gladly change to multi-line syntax though if you insist.
copy: src=trim_blocks_false.expected dest={{output_dir}}/trim_blocks_false.expected | ||
|
||
- name: compare templated file to known good trim_blocks_false | ||
shell: diff -u {{output_dir}}/trim_blocks_false.templated {{output_dir}}/trim_blocks_false.expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than a shell
task, you can use the stat
module and assert
that the checksum
matches. See lineinfile tests for examples.
f8f0255
to
ac8d1ec
Compare
Added a new commit to check for Jinja2 version. Note that while assertion in tests is now done with checksum, the checksum is not hardcoded in tests. The "expected" files still remain, in case anything goes wrong to know what the expected output actually is. It also seems to me that github did not get the commits in the right order? Possibly due to overwriting and force-pushing? The commits in the branch should be in the correct order nevertheless. Ready for review. |
ac8d1ec
to
6bf83ae
Compare
d292362
to
a8744fc
Compare
The test
The test
|
5207149
to
185b495
Compare
The test
|
e5ea8f0
to
18122ca
Compare
CI failure in integration tests: https://app.shippable.com/github/ansible/ansible/runs/58672/40/tests
|
This is why the integration tests fail: The command inside the
This is an expected behavior. The same command in the
I had also noticed that previously when we attempted to access the I don't know why the YAML and the Python module use different environments, I haven't checked the exact setup used to run the tests, yet. Also note that this happens only in the centos6 tests. Same goes for the |
I really wish this could be set to |
# VERIFY lstrip_blocks | ||
|
||
- name: Check support for lstrip_blocks in Jinja2 | ||
shell: python -c 'import jinja2; jinja2.defaults.LSTRIP_BLOCKS' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use {{ ansible_python_interpreter }}
instead of python
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this indeed did the trick. I just did grep -R jinja2.__version__
in test
and saw another shell:
module using plain python -c
. My bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes our existing tests do things that they shouldn't, such as hard-coding references to python
instead of using {{ ansible_python_interpreter }}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason this fixes the test failure is because there is another integration test, template_jinja2_latest
, which sets up a virtualenv with a newer version of Jinja2. During that test, the shell task calling python
executes to the Python version inside the virtualenv, which passes the Jinja2 version check. But the Python being used by Ansible is /usr/bin/python
, which would fail the Jinja2 version test and doesn't support lstrip_blocks
.
This causes the "Render a template with "lstrip_blocks" set to True" task to run but fail when in should be skipped, making checksum
undefined in lstrip_blocks_true_result
.
Fix passing `trim_blocks' option to the template module as inline argument. Previously passing the `trim_blocks' option inline instead of using the YAML dictionary format resulted in it always being set to `True', even if `trim_blocks=False' was used. Signed-off-by: Alex Tsitsimpis <alextsi@arrikto.com>
Add option to set `lstrip_blocks' when using the template module to render Jinja templates. The Jinja documentation suggests that `trim_blocks' and `lstrip_blocks' is a great combination and the template module already provides an option for `trim_blocks'. Note that although `trim_blocks' in Ansible is enabled by default since version 2.4, in order to avoid breaking things keep `lstrip_blocks' disabled by default. Maybe in a future version it could be enabled by default. This seems to address issue ansible#10725 in a more appropriate way than the suggested. Signed-off-by: Alex Tsitsimpis <alextsi@arrikto.com>
Since the `lstrip_blocks' option was added in Jinja2 version 2.7, raise an exception when `lstrip_blocks' is set but Jinja2 does not support it. Check support for `lstrip_blocks' option by checking `jinja2.defaults' for `LSTRIP_BLOCKS' and do not use `jinja2.__version__' because the latter is set to `unknown' in some cases, perhaps due to bug in `pkg_resources' in Python 2.6.6. Also update option description to state that Jinja2 version >=2.7 is required. Signed-off-by: Alex Tsitsimpis <alextsi@arrikto.com>
Signed-off-by: Alex Tsitsimpis <alextsi@arrikto.com>
Signed-off-by: Alex Tsitsimpis <alextsi@arrikto.com>
18122ca
to
073235a
Compare
lstrip_blocks_test.zip
trim_blocks_test.zip
SUMMARY
Add option to set
lstrip_blocks
when using the template module to render Jinja templates. The Jinja documentation suggests thattrim_blocks
andlstrip_blocks
is a great combination and the template module already provides an option for `trim_blocks'.Note that although
trim_blocks
in Ansible is enabled by default since version 2.4, in order to avoid breaking things keeplstrip_blocks
disabled by default. Maybe in a future version it could be enabled by default.Also fix the
trim_blocks
argument to make it possible to set it toFalse
inline.This seems to address issue #10725 in a more appropriate way than the
suggested.
ISSUE TYPE
COMPONENT NAME
template
ANSIBLE VERSION
ADDITIONAL INFORMATION
Attached is a simple test. The zip file contains the playbook
lstrip_blocks_test.yml
and the templatetest.j2
. The playbook contains two tasks. The first task should render the template without setting thelstrip_blocks
option, creating the filetest_default
in the working directory.The second task should render the template with the
lstrip_blocks
options set toTrue
, creating thetest_with_lstrip_blocks
file in the working directory.To run the playbook simply run
ansible-playbook lstrip_blocks_test.yml
.To test the
trim_blocks
inline option, we provide another test. The zip file contains a playbooktrim_blocks_test.yml
and the templatetrim_blocks.j2
. The playbook has a single task, to render thetrim_blocks.j2
template withtrim_blocks
set toFalse
, creating the filetrim_blocks.templated
in the working directory. Before this PR the result is the following:After the PR the result is the following (which is the correct and expected):
To run the playbook simply run
ansible-playbook trim_blocks_test.yml
.