From 3abcb3be5104aafcbc400e446b36e2365fd3968c Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Mon, 10 Feb 2025 17:13:35 +0100 Subject: [PATCH] Add the hostname into /etc/hosts. --- tasks/hostname.yml | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/tasks/hostname.yml b/tasks/hostname.yml index 1444646..decfec7 100644 --- a/tasks/hostname.yml +++ b/tasks/hostname.yml @@ -1,17 +1,24 @@ --- -- name: Set the hostname when different from the inventory one. - hostname: name={{ hostname }} - when: hostname is defined +- name: hostname | Add entries to /etc/hosts + when: custom_etc_hosts_entries | length > 0 tags: [ 'systemsetup', 'hostname' ] + block: + - name: hostname | Set the hostname when different from the inventory one. + ansible.builtin.hostname: + name: "{{ hostname }}" + when: hostname is defined -- name: Set the hostname as defined in the inventory - hostname: name={{ inventory_hostname }} - when: hostname is not defined - tags: [ 'systemsetup', 'hostname' ] + - name: hostname | Set the hostname as defined in the inventory + ansible.builtin.hostname: + name: "{{ inventory_hostname }}" + when: hostname is not defined + + - name: hostname | Add the hostname into the /etc/hosts file + ansible.builtin.blockinfile: + path: /etc/hosts + marker_begin: 'ansible_hostname_start' + marker_end: 'ansible_hostname_end' + marker: "# {mark} hostname entry managed by ansible" + block: "{{ ansible_default_ipv4.address }} {{ hostname }} {{ ansible_hostname }}" + state: present -- 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: - - hostname is defined - - ansible_virtualization_type == 'xen' - tags: [ 'systemsetup', 'hostname' ]