diff --git a/lib/ansible/modules/cloud/lxc/lxc_container.py b/lib/ansible/modules/cloud/lxc/lxc_container.py index 343557f9c09..4994d15a94c 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':