From 0a114436fc14fc9eb5ec0ff9d42400beb3c63c60 Mon Sep 17 00:00:00 2001 From: bit Date: Thu, 28 Sep 2017 05:32:45 +0200 Subject: [PATCH] lxc_container: open files as text, fixes #30571 (#30572) call to_text on full config file fixes issue #30571 --- lib/ansible/modules/cloud/lxc/lxc_container.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/cloud/lxc/lxc_container.py b/lib/ansible/modules/cloud/lxc/lxc_container.py index 343557f9c0..4994d15a94 100644 --- a/lib/ansible/modules/cloud/lxc/lxc_container.py +++ b/lib/ansible/modules/cloud/lxc/lxc_container.py @@ -438,6 +438,7 @@ else: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.parsing.convert_bool import BOOLEANS_FALSE, BOOLEANS_TRUE from ansible.module_utils.six.moves import xrange +from ansible.module_utils._text import to_text, to_bytes # LXC_COMPRESSION_MAP is a map of available compression types when creating @@ -572,7 +573,7 @@ def create_script(command): (fd, script_file) = tempfile.mkstemp(prefix='lxc-attach-script') f = os.fdopen(fd, 'wb') try: - f.write(ATTACH_TEMPLATE % {'container_command': command}) + f.write(to_bytes(ATTACH_TEMPLATE % {'container_command': command}, errors='surrogate_or_strict')) f.flush() finally: f.close() @@ -722,7 +723,7 @@ class LxcContainerManagement(object): container_config_file = self.container.config_file_name with open(container_config_file, 'rb') as f: - container_config = f.readlines() + container_config = to_text(f.read(), errors='surrogate_or_strict').splitlines() # Note used ast literal_eval because AnsibleModule does not provide for # adequate dictionary parsing. @@ -763,7 +764,7 @@ class LxcContainerManagement(object): self.container.stop() with open(container_config_file, 'wb') as f: - f.writelines(container_config) + f.writelines([to_bytes(line, errors='surrogate_or_strict') for line in container_config]) self.state_change = True if container_state == 'running':