From 6aac0e2460985daac132541f643cf1256430e572 Mon Sep 17 00:00:00 2001 From: Rogier Krieger Date: Fri, 14 Apr 2023 16:41:44 +0200 Subject: [PATCH] Extend OpenBSDStrategy to also update current hostname. (#80521) Fixes #80520 --- .../80520-fix-current-hostname-openbsd.yml | 2 ++ lib/ansible/modules/hostname.py | 21 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/80520-fix-current-hostname-openbsd.yml diff --git a/changelogs/fragments/80520-fix-current-hostname-openbsd.yml b/changelogs/fragments/80520-fix-current-hostname-openbsd.yml new file mode 100644 index 00000000000..382797c2419 --- /dev/null +++ b/changelogs/fragments/80520-fix-current-hostname-openbsd.yml @@ -0,0 +1,2 @@ +bugfixes: + - The ``hostname`` module now also updates both current and permanent hostname on OpenBSD. Before it only updated the permanent hostname (https://github.com/ansible/ansible/issues/80520). diff --git a/lib/ansible/modules/hostname.py b/lib/ansible/modules/hostname.py index f6284df2bed..f63c16a01dd 100644 --- a/lib/ansible/modules/hostname.py +++ b/lib/ansible/modules/hostname.py @@ -387,10 +387,29 @@ class OpenRCStrategy(BaseStrategy): class OpenBSDStrategy(FileStrategy): """ This is a OpenBSD family Hostname manipulation strategy class - it edits - the /etc/myname file. + the /etc/myname file for the permanent hostname and executes hostname + command for the current hostname. """ FILE = '/etc/myname' + COMMAND = "hostname" + + def __init__(self, module): + super(OpenBSDStrategy, self).__init__(module) + self.hostname_cmd = self.module.get_bin_path(self.COMMAND, True) + + def get_current_hostname(self): + cmd = [self.hostname_cmd] + rc, out, err = self.module.run_command(cmd) + if rc != 0: + self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" % (rc, out, err)) + return to_native(out).strip() + + def set_current_hostname(self, name): + cmd = [self.hostname_cmd, name] + rc, out, err = self.module.run_command(cmd) + if rc != 0: + self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" % (rc, out, err)) class SolarisStrategy(BaseStrategy):