Windows: Use the correct newline sequence for the platform (#21846)

This change to the template action plugin make template use the
platform's native newline_sequence for Jinja.

We also added the option `newline_sequence` to change the newline
sequence using by Jinja if you need to use another newline sequence than
the platform default.

This was previously discussed in
https://github.com/ansible/ansible/issues/16255#issuecomment-278289414

And also relates to issue #21128
This commit is contained in:
Dag Wieers 2017-03-24 03:47:10 +01:00 committed by Matt Davis
parent ef36d7de68
commit ac43a1bbbc
16 changed files with 381 additions and 71 deletions

View file

@ -0,0 +1,3 @@
BEGIN
templated_var_loaded
END

View file

@ -62,14 +62,14 @@
copy: src=foo.txt dest={{output_dir}}/foo.txt
- name: compare templated file to known good
shell: diff -w {{output_dir}}/foo.templated {{output_dir}}/foo.txt
shell: diff -uw {{output_dir}}/foo.templated {{output_dir}}/foo.txt
register: diff_result
- name: verify templated file matches known good
assert:
that:
- 'diff_result.stdout == ""'
- "diff_result.rc == 0"
assert:
that:
- 'diff_result.stdout == ""'
- "diff_result.rc == 0"
# VERIFY MODE
@ -251,3 +251,121 @@
assert:
that:
- "template_result|changed"
- name: change var for the template
set_fact:
templated_var: "templated_var_loaded"
# UNIX TEMPLATE
- name: fill in a basic template (Unix)
template:
src: foo2.j2
dest: '{{ output_dir }}/foo.unix.templated'
register: template_result
- name: verify that the file was marked as changed (Unix)
assert:
that:
- 'template_result|changed'
- name: fill in a basic template again (Unix)
template:
src: foo2.j2
dest: '{{ output_dir }}/foo.unix.templated'
register: template_result2
- name: verify that the template was not changed (Unix)
assert:
that:
- 'not template_result2|changed'
# VERIFY UNIX CONTENTS
- name: copy known good into place (Unix)
copy:
src: foo.unix.txt
dest: '{{ output_dir }}/foo.unix.txt'
- name: Dump templated file (Unix)
command: hexdump -C {{ output_dir }}/foo.unix.templated
- name: Dump expected file (Unix)
command: hexdump -C {{ output_dir }}/foo.unix.txt
- name: compare templated file to known good (Unix)
command: diff -u {{ output_dir }}/foo.unix.templated {{ output_dir }}/foo.unix.txt
register: diff_result
- name: verify templated file matches known good (Unix)
assert:
that:
- 'diff_result.stdout == ""'
- "diff_result.rc == 0"
# DOS TEMPLATE
- name: fill in a basic template (DOS)
template:
src: foo2.j2
dest: '{{ output_dir }}/foo.dos.templated'
newline_sequence: '\r\n'
register: template_result
- name: verify that the file was marked as changed (DOS)
assert:
that:
- 'template_result|changed'
- name: fill in a basic template again (DOS)
template:
src: foo2.j2
dest: '{{ output_dir }}/foo.dos.templated'
newline_sequence: '\r\n'
register: template_result2
- name: verify that the template was not changed (DOS)
assert:
that:
- 'not template_result2|changed'
# VERIFY DOS CONTENTS
- name: copy known good into place (DOS)
copy:
src: foo.dos.txt
dest: '{{ output_dir }}/foo.dos.txt'
- name: Dump templated file (DOS)
command: hexdump -C {{ output_dir }}/foo.dos.templated
- name: Dump expected file (DOS)
command: hexdump -C {{ output_dir }}/foo.dos.txt
- name: compare templated file to known good (DOS)
command: diff -u {{ output_dir }}/foo.dos.templated {{ output_dir }}/foo.dos.txt
register: diff_result
- name: verify templated file matches known good (DOS)
assert:
that:
- 'diff_result.stdout == ""'
- "diff_result.rc == 0"
# VERIFY DOS CONTENTS
- name: copy known good into place (Unix)
copy:
src: foo.unix.txt
dest: '{{ output_dir }}/foo.unix.txt'
- name: Dump templated file (Unix)
command: hexdump -C {{ output_dir }}/foo.unix.templated
- name: Dump expected file (Unix)
command: hexdump -C {{ output_dir }}/foo.unix.txt
- name: compare templated file to known good (Unix)
command: diff -u {{ output_dir }}/foo.unix.templated {{ output_dir }}/foo.unix.txt
register: diff_result
- name: verify templated file matches known good (Unix)
assert:
that:
- 'diff_result.stdout == ""'
- "diff_result.rc == 0"

View file

@ -0,0 +1,3 @@
BEGIN
{{ templated_var }}
END

View file

@ -0,0 +1,3 @@
BEGIN
[% templated_var %]
END

View file

@ -0,0 +1,3 @@
BEGIN
templated_var_loaded
END

View file

@ -0,0 +1,3 @@
BEGIN
templated_var_loaded
END

View file

@ -16,58 +16,109 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- name: fill in a basic template
# win_template: src=foo.j2 dest={{win_output_dir}}/foo.templated mode=0644
win_template: src=foo.j2 dest={{win_output_dir}}/foo.templated
register: template_result
- assert:
that:
- "'changed' in template_result"
# - "'dest' in template_result"
# - "'group' in template_result"
# - "'gid' in template_result"
# - "'checksum' in template_result"
# - "'owner' in template_result"
# - "'size' in template_result"
# - "'src' in template_result"
# - "'state' in template_result"
# - "'uid' in template_result"
- name: verify that the file was marked as changed
assert:
that:
- "template_result.changed == true"
- name: fill in a basic template again
# DOS TEMPLATE
- name: fill in a basic template (DOS)
win_template:
src: foo.j2
dest: "{{win_output_dir}}/foo.templated"
dest: '{{ win_output_dir }}/foo.dos.templated'
register: template_result
- name: verify that the file was marked as changed (DOS)
assert:
that:
- 'template_result|changed'
- name: fill in a basic template again (DOS)
win_template:
src: foo.j2
dest: '{{ win_output_dir }}/foo.dos.templated'
register: template_result2
- name: verify that the template was not changed
- name: verify that the template was not changed (DOS)
assert:
that:
- "not template_result2|changed"
- 'not template_result2|changed'
# VERIFY CONTENTS
# VERIFY DOS CONTENTS
- name: copy known good into place (DOS)
win_copy:
src: foo.dos.txt
dest: '{{ win_output_dir }}\\foo.dos.txt'
- name: copy known good into place
win_copy: src=foo.txt dest={{win_output_dir}}\\foo.txt
- name: compare templated file to known good
raw: fc.exe {{win_output_dir}}\\foo.templated {{win_output_dir}}\\foo.txt
- name: compare templated file to known good (DOS)
raw: fc.exe {{ win_output_dir }}\\foo.dos.templated {{ win_output_dir }}\\foo.dos.txt
register: diff_result
- debug: var=diff_result
- debug:
var: diff_result
- name: verify templated file matches known good
- name: verify templated file matches known good (DOS)
assert:
that:
# - 'diff_result.stdout == ""'
- 'diff_result.stdout_lines[1] == "FC: no differences encountered"'
- '"FC: no differences encountered" in diff_result.stdout'
- "diff_result.rc == 0"
# UNIX TEMPLATE
- name: fill in a basic template (Unix)
win_template:
src: foo.j2
dest: '{{ win_output_dir }}/foo.unix.templated'
newline_sequence: '\n'
register: template_result
- name: verify that the file was marked as changed (Unix)
assert:
that:
- 'template_result|changed'
- name: fill in a basic template again (Unix)
win_template:
src: foo.j2
dest: '{{ win_output_dir }}/foo.unix.templated'
newline_sequence: '\n'
register: template_result2
- name: verify that the template was not changed (Unix)
assert:
that:
- 'not template_result2|changed'
# VERIFY UNIX CONTENTS
- name: copy known good into place (Unix)
win_copy:
src: foo.unix.txt
dest: '{{ win_output_dir }}\\foo.unix.txt'
- name: compare templated file to known good (Unix)
raw: fc.exe {{ win_output_dir }}\\foo.unix.templated {{ win_output_dir }}\\foo.unix.txt
register: diff_result
- debug:
var: diff_result
- name: verify templated file matches known good (Unix)
assert:
that:
- '"FC: no differences encountered" in diff_result.stdout'
# VERIFY DOS CONTENTS
- name: copy known good into place (DOS)
win_copy:
src: foo.dos.txt
dest: '{{ win_output_dir }}\\foo.dos.txt'
- name: compare templated file to known good (DOS)
raw: fc.exe {{ win_output_dir }}\\foo.dos.templated {{ win_output_dir }}\\foo.dos.txt
register: diff_result
- debug:
var: diff_result
- name: verify templated file matches known good (DOS)
assert:
that:
- '"FC: no differences encountered" in diff_result.stdout'
# VERIFY MODE
# can't set file mode on windows so commenting this test out
#- name: set file mode

View file

@ -1 +1,3 @@
BEGIN
{{ templated_var }}
END

View file

@ -0,0 +1,3 @@
BEGIN
[% templated_var %]
END