diff --git a/changelogs/fragments/44739-assemble-avoid-extra-newline-Py3.yaml b/changelogs/fragments/44739-assemble-avoid-extra-newline-Py3.yaml new file mode 100644 index 0000000000..2e86942648 --- /dev/null +++ b/changelogs/fragments/44739-assemble-avoid-extra-newline-Py3.yaml @@ -0,0 +1,2 @@ +bugfixes: + - assemble - avoid extra newline on Python 3 (https://github.com/ansible/ansible/issues/44739) diff --git a/lib/ansible/modules/files/assemble.py b/lib/ansible/modules/files/assemble.py index 04538733e8..37c42021d2 100644 --- a/lib/ansible/modules/files/assemble.py +++ b/lib/ansible/modules/files/assemble.py @@ -116,7 +116,7 @@ import re import tempfile from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.six import b +from ansible.module_utils.six import b, indexbytes from ansible.module_utils._text import to_native @@ -148,7 +148,11 @@ def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, igno tmp.write(delimiter) # always make sure there's a newline after the # delimiter, so lines don't run together - if delimiter[-1] != b('\n'): + + # byte indexing differs on Python 2 and 3, + # use indexbytes for compat + # chr(10) == '\n' + if indexbytes(delimiter, -1) != 10: tmp.write(b('\n')) tmp.write(fragment_content)