From d53f160afbfe7079c1a26d11d7e5f4badc70d902 Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Fri, 25 Oct 2019 19:49:32 +0200 Subject: [PATCH] Set the hostname identical to the inventory hostname if no 'hostname' variable has been defined. --- library/roles/deb-set-hostname/tasks/main.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/library/roles/deb-set-hostname/tasks/main.yml b/library/roles/deb-set-hostname/tasks/main.yml index 7f7aae65..6f3ebab5 100644 --- a/library/roles/deb-set-hostname/tasks/main.yml +++ b/library/roles/deb-set-hostname/tasks/main.yml @@ -1,9 +1,14 @@ --- -- name: Set the hostname +- name: Set the hostname when different from the inventory one. hostname: name={{ hostname }} when: hostname is defined tags: [ 'bootstrap', 'set_hostname' ] +- name: Set the hostname as defined in the inventory + hostname: name={{ inventory_hostname }} + when: hostname is not defined + tags: [ 'bootstrap', 'set_hostname' ] + - name: Add the hostname to /etc/hosts shell: grep -v {{ ansible_default_ipv4.address }} /etc/hosts > /etc/hosts.tmp ; echo "{{ ansible_default_ipv4.address }} {{ hostname }} {{ ansible_hostname }}" >> /etc/hosts.tmp ; /bin/mv /etc/hosts.tmp /etc/hosts when: