From f23c1004199f9ffbf2e59ca0700e7cccfbafd4ea Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Thu, 29 Mar 2018 14:57:40 -0700 Subject: [PATCH] Bkprt py3 haproxy (#37791) * Fix python3 interpreter issue (#34811) (#35176) * Fix python3 interpreter issue (#34811) * Update ansible.module_utils._text (#34811) * Convert to text later to account for multibyte characters (cherry picked from commit 340064bfb9678308f3295bf79b82105b972932e1) * Add a changelog fragment for haproxy python3 fix --- changelogs/fragments/py3-haproxy.yaml | 3 +++ lib/ansible/modules/net_tools/haproxy.py | 10 +++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/py3-haproxy.yaml diff --git a/changelogs/fragments/py3-haproxy.yaml b/changelogs/fragments/py3-haproxy.yaml new file mode 100644 index 00000000000..145815d31bc --- /dev/null +++ b/changelogs/fragments/py3-haproxy.yaml @@ -0,0 +1,3 @@ +bugfixes: +- Fix to send and receive bytes over a socket in the haproxy module which was + causing tracebacks on Python3 https://github.com/ansible/ansible/pull/35176 diff --git a/lib/ansible/modules/net_tools/haproxy.py b/lib/ansible/modules/net_tools/haproxy.py index f775f8e29de..e9ba6379674 100644 --- a/lib/ansible/modules/net_tools/haproxy.py +++ b/lib/ansible/modules/net_tools/haproxy.py @@ -202,6 +202,7 @@ import time from string import Template from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils._text import to_bytes, to_text DEFAULT_SOCKET_LOCATION = "/var/run/haproxy.sock" @@ -251,13 +252,16 @@ class HAProxy(object): """ self.client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.client.connect(self.socket) - self.client.sendall('%s\n' % cmd) - result = '' - buf = '' + self.client.sendall(to_bytes('%s\n' % cmd)) + + result = b'' + buf = b'' buf = self.client.recv(RECV_SIZE) while buf: result += buf buf = self.client.recv(RECV_SIZE) + result = to_text(result, errors='surrogate_or_strict') + if capture_output: self.capture_command_output(cmd, result.strip()) self.client.close()