--- - name: "Configure /etc/hostname" become: true ansible.builtin.copy: dest: "/etc/hostname" mode: "0644" content: | {{ hostname }} when: hostname is defined - name: "Configure /etc/hosts" become: true ansible.builtin.copy: dest: "/etc/hosts" mode: "0644" content: | 127.0.0.1 localhost 127.0.1.1 {{ hostname }} ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters when: hostname is defined