From e0c75a6756f2c9d54239f882fed89cbeb31d2106 Mon Sep 17 00:00:00 2001 From: Lloyd Parkes Date: Wed, 6 Jan 2021 05:20:44 +1300 Subject: [PATCH] Replace the use of the function string.replace with the method str.replace (#68793) * Replace the use of the function string.replace() with the method str.replace() because that's what works for both Python 2 and 3. * Cleanup the unused string import. Added a changelog fragment. * The documentation for os.write() seems a bit iffy, but in Python 3 we definitely cannot pass it a string and we need to encode it into bytes. The Python documentation at https://docs.python.org/3/howto/pyporting.html#text-versus-binary-data says that this code will work in Python 2 as well. Co-authored-by: Lloyd Parkes --- changelogs/fragments/bsd_rcconf_string_replace.yaml | 2 ++ lib/ansible/modules/service.py | 9 ++++----- 2 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/bsd_rcconf_string_replace.yaml diff --git a/changelogs/fragments/bsd_rcconf_string_replace.yaml b/changelogs/fragments/bsd_rcconf_string_replace.yaml new file mode 100644 index 00000000000..1bacce1a09e --- /dev/null +++ b/changelogs/fragments/bsd_rcconf_string_replace.yaml @@ -0,0 +1,2 @@ +bugfixes: +- service - Fix for the BSD rcconf code using a Python 2 specific string replace function diff --git a/lib/ansible/modules/service.py b/lib/ansible/modules/service.py index f4eb709735d..59cd7e8113f 100644 --- a/lib/ansible/modules/service.py +++ b/lib/ansible/modules/service.py @@ -135,7 +135,6 @@ import platform import re import select import shlex -import string import subprocess import tempfile import time @@ -419,7 +418,7 @@ class Service(object): # Write out the contents of the list into our temporary file. for rcline in new_rc_conf: - os.write(TMP_RCCONF, rcline) + os.write(TMP_RCCONF, rcline.encode()) # Close temporary file. os.close(TMP_RCCONF) @@ -1116,7 +1115,7 @@ class DragonFlyBsdService(FreeBsdService): if os.path.isfile(rcfile): self.rcconf_file = rcfile - self.rcconf_key = "%s" % string.replace(self.name, "-", "_") + self.rcconf_key = "%s" % self.name.replace("-", "_") return self.service_enable_rcconf() @@ -1274,7 +1273,7 @@ class NetBsdService(Service): """ This is the NetBSD Service manipulation class - it uses the /etc/rc.conf file for controlling services started at boot, check status and perform - direct service manipulation. Init scripts in /etc/rcd are used for + direct service manipulation. Init scripts in /etc/rc.d are used for controlling services (start/stop) as well as for controlling the current state. """ @@ -1304,7 +1303,7 @@ class NetBsdService(Service): if os.path.isfile(rcfile): self.rcconf_file = rcfile - self.rcconf_key = "%s" % string.replace(self.name, "-", "_") + self.rcconf_key = "%s" % self.name.replace("-", "_") return self.service_enable_rcconf()