Update systemd strategy to not confuse NetworkManager (#77243)

* Update systemd strategy to not confuse NetworkManager. Fixes #76958

* Add tests
pull/77487/head
Matt Martz 3 years ago committed by GitHub
parent 1100289a45
commit c1a34d5a63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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)

@ -332,15 +332,18 @@ class SystemdStrategy(BaseStrategy):
def set_permanent_hostname(self, name): def set_permanent_hostname(self, name):
if len(name) > 64: if len(name) > 64:
self.module.fail_json(msg="name cannot be longer than 64 characters on systemd servers, try a shorter name") 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] 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))
cmd = [self.hostnamectl_cmd, '--static', 'set-hostname', name]
rc, out, err = self.module.run_command(cmd) rc, out, err = self.module.run_command(cmd)
if rc != 0: if rc != 0:
self.module.fail_json(msg="Command failed rc=%d, out=%s, err=%s" % (rc, out, err)) 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): class OpenRCStrategy(BaseStrategy):
""" """

@ -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 - name: Run hostname module for real now
become: 'yes' become: 'yes'
hostname: hostname:
@ -8,6 +22,20 @@
command: hostname command: hostname
register: current_after_hn2 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 - name: Run hostname again to ensure it does not change
become: 'yes' become: 'yes'
hostname: hostname:

Loading…
Cancel
Save