From ceec679302d76a29ed44458eb5757478f59355b9 Mon Sep 17 00:00:00 2001 From: Michael Scherer Date: Mon, 24 Oct 2016 09:05:01 +0200 Subject: [PATCH] Make blockinfile work with python3 Traceback (most recent call last): File \"/tmp/ansible_ueg52c0b/ansible_module_blockinfile.py\", line 319, in main() File \"/tmp/ansible_ueg52c0b/ansible_module_blockinfile.py\", line 259, in main if line.startswith(marker0): TypeError: startswith first arg must be bytes or a tuple of bytes, not str Also clean imports while on it. --- .../modules/extras/files/blockinfile.py | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/ansible/modules/extras/files/blockinfile.py b/lib/ansible/modules/extras/files/blockinfile.py index 285a109744..5448aafa17 100755 --- a/lib/ansible/modules/extras/files/blockinfile.py +++ b/lib/ansible/modules/extras/files/blockinfile.py @@ -149,7 +149,9 @@ EXAMPLES = r""" import re import os import tempfile - +from ansible.module_utils.six import b +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils._text import to_bytes def write_changes(module, contents, dest): @@ -227,8 +229,8 @@ def main(): insertbefore = params['insertbefore'] insertafter = params['insertafter'] - block = params['block'] - marker = params['marker'] + block = to_bytes(params['block']) + marker = to_bytes(params['marker']) present = params['state'] == 'present' if not present and not path_exists: @@ -244,8 +246,8 @@ def main(): else: insertre = None - marker0 = re.sub(r'{mark}', 'BEGIN', marker) - marker1 = re.sub(r'{mark}', 'END', marker) + marker0 = re.sub(b(r'{mark}'), b('BEGIN'), marker) + marker1 = re.sub(b(r'{mark}'), b('END'), marker) if present and block: # Escape seqeuences like '\n' need to be handled in Ansible 1.x if module.ansible_version.startswith('1.'): @@ -284,9 +286,9 @@ def main(): lines[n0:n0] = blocklines if lines: - result = '\n'.join(lines) - if original is None or original.endswith('\n'): - result += '\n' + result = b('\n').join(lines) + if original is None or original.endswith(b('\n')): + result += b('\n') else: result = '' if original == result: @@ -313,7 +315,6 @@ def main(): msg, changed = check_file_attrs(module, changed, msg) module.exit_json(changed=changed, msg=msg) -# import module snippets -from ansible.module_utils.basic import * + if __name__ == '__main__': main()