Add the hostname into /etc/hosts.

This commit is contained in:
Andrea Dell'Amico 2025-02-10 17:13:35 +01:00
parent 29f5615a63
commit 3abcb3be51
Signed by: adellam
GPG Key ID: 147ABE6CEB9E20FF
1 changed files with 20 additions and 13 deletions

View File

@ -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' ]