summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Martz <matt@sivel.net>2022-03-21 09:32:41 -0500
committerGitHub <noreply@github.com>2022-03-21 09:32:41 -0500
commitc1a34d5a6337002edf74dbd63d4bc68ce8e085e8 (patch)
tree0663d627096a0427b0a2590ebbe0c91363ba067f
parent1100289a45e5b5444bc5af59052fc2d63452eff6 (diff)
downloadansible-c1a34d5a6337002edf74dbd63d4bc68ce8e085e8.tar.gz
Update systemd strategy to not confuse NetworkManager (#77243)
* Update systemd strategy to not confuse NetworkManager. Fixes #76958 * Add tests
-rw-r--r--changelogs/fragments/76958-hostname-systemd-nm.yaml3
-rw-r--r--lib/ansible/modules/hostname.py13
-rw-r--r--test/integration/targets/hostname/tasks/test_normal.yml28
3 files changed, 39 insertions, 5 deletions
diff --git a/changelogs/fragments/76958-hostname-systemd-nm.yaml b/changelogs/fragments/76958-hostname-systemd-nm.yaml
new file mode 100644
index 0000000000..e6d54e9cf5
--- /dev/null
+++ b/changelogs/fragments/76958-hostname-systemd-nm.yaml
@@ -0,0 +1,3 @@
+bugfixes:
+- hostname - Update the systemd strategy in the ``hostname`` module to not interfere with
+ NetworkManager (https://github.com/ansible/ansible/issues/76958)
diff --git a/lib/ansible/modules/hostname.py b/lib/ansible/modules/hostname.py
index 5159eb14d4..05017670c3 100644
--- a/lib/ansible/modules/hostname.py
+++ b/lib/ansible/modules/hostname.py
@@ -332,15 +332,18 @@ class SystemdStrategy(BaseStrategy):
def set_permanent_hostname(self, name):
if len(name) > 64:
self.module.fail_json(msg="name cannot be longer than 64 characters on systemd servers, try a shorter name")
- cmd = [self.hostnamectl_cmd, '--pretty', 'set-hostname', 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))
- cmd = [self.hostnamectl_cmd, '--static', 'set-hostname', name]
+ cmd = [self.hostnamectl_cmd, '--pretty', '--static', 'set-hostname', 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))
+ def update_current_and_permanent_hostname(self):
+ # Must set the permanent hostname prior to current to avoid NetworkManager complaints
+ # about setting the hostname outside of NetworkManager
+ self.update_permanent_hostname()
+ self.update_current_hostname()
+ return self.changed
+
class OpenRCStrategy(BaseStrategy):
"""
diff --git a/test/integration/targets/hostname/tasks/test_normal.yml b/test/integration/targets/hostname/tasks/test_normal.yml
index e8606a4bfd..ed5ac73586 100644
--- a/test/integration/targets/hostname/tasks/test_normal.yml
+++ b/test/integration/targets/hostname/tasks/test_normal.yml
@@ -1,3 +1,17 @@
+- name: Ensure hostname doesn't confuse NetworkManager
+ when: ansible_os_family == 'RedHat'
+ block:
+ - name: slurp /var/log/messages
+ slurp:
+ path: /var/log/messages
+ become: yes
+ register: messages_before
+
+ - assert:
+ that:
+ - >
+ 'current hostname was changed outside NetworkManager' not in messages_before.content|b64decode
+
- name: Run hostname module for real now
become: 'yes'
hostname:
@@ -8,6 +22,20 @@
command: hostname
register: current_after_hn2
+- name: Ensure hostname doesn't confuse NetworkManager
+ when: ansible_os_family == 'RedHat'
+ block:
+ - name: slurp /var/log/messages
+ slurp:
+ path: /var/log/messages
+ become: yes
+ register: messages_after
+
+ - assert:
+ that:
+ - >
+ 'current hostname was changed outside NetworkManager' not in messages_after.content|b64decode
+
- name: Run hostname again to ensure it does not change
become: 'yes'
hostname: