chore(hostname): use handler for on-demand hostname setting

This commit is contained in:
Jan Christian Grünhage 2024-08-31 08:54:30 +02:00
parent d95eec5f19
commit e9e6fec5da
No known key found for this signature in database
GPG key ID: EEC1170CE56FA2ED
2 changed files with 6 additions and 18 deletions

View file

@ -0,0 +1,5 @@
- name: "Set hostname via hostnamectl to avoid a reboot"
command:
cmd: "hostnamectl set-hostname {{ hostname_fqdn }}"
when: ansible_os_family in ["Debian"]
listen: "set-hostname"

View file

@ -8,6 +8,7 @@
content: |
{{ hostname_fqdn }}
tags: ["deploy", "deploy-hostname"]
notify: "set-hostname"
- name: "Ensure /etc/hosts contains all hostnames and aliases of the server"
blockinfile:
@ -21,21 +22,3 @@
{% endfor %}
marker: "# {mark} ANSIBLE MANAGED BLOCK"
tags: ["deploy", "deploy-hostname"]
# Needed because ansibles `ansible_fqdn` is loaded
# from /etc/hostname, not from the running hostname
- name: "Query current hostname"
command:
cmd: "hostname"
changed_when: false
check_mode: false
register: "current_hostname"
tags: ["deploy", "deploy-hostname"]
- name: "Set hostname via hostnamectl to avoid a reboot"
command:
cmd: "hostnamectl set-hostname {{ hostname_fqdn }}"
when: "hostname_fqdn != hostname_current"
vars:
hostname_current: "{{ current_hostname.stdout }}."
tags: ["deploy", "deploy-hostname"]