From e9e6fec5da7f8dd8673a7c1d86ebdc614ab72bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Sat, 31 Aug 2024 08:54:30 +0200 Subject: [PATCH] chore(hostname): use handler for on-demand hostname setting --- roles/hostname/handlers/main.yml | 5 +++++ roles/hostname/tasks/main.yml | 19 +------------------ 2 files changed, 6 insertions(+), 18 deletions(-) create mode 100644 roles/hostname/handlers/main.yml diff --git a/roles/hostname/handlers/main.yml b/roles/hostname/handlers/main.yml new file mode 100644 index 0000000..ff09681 --- /dev/null +++ b/roles/hostname/handlers/main.yml @@ -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" diff --git a/roles/hostname/tasks/main.yml b/roles/hostname/tasks/main.yml index 7c4cfba..3a15e73 100644 --- a/roles/hostname/tasks/main.yml +++ b/roles/hostname/tasks/main.yml @@ -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"]