--- - name: hostname | Add entries to /etc/hosts when: explicitly_set_hostname 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: 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