diff --git a/roles/hostname/tasks/main.yml b/roles/hostname/tasks/main.yml index 563c240..9859c42 100644 --- a/roles/hostname/tasks/main.yml +++ b/roles/hostname/tasks/main.yml @@ -21,8 +21,17 @@ {% endfor %} marker: "# {mark} ANSIBLE MANAGED BLOCK" +# 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 + register: current_hostname + - name: Set hostname via hostnamectl to avoid a reboot command: cmd: "hostnamectl set-hostname {{ hostname_fqdn }}" - when: hostname_fqdn != "{{ ansible_fqdn }}." - + when: hostname_fqdn != hostname_current + vars: + hostname_current: "{{ current_hostname.stdout }}."