- name: Check if /etc/cloud/cloud.cfg exists ansible.builtin.stat: path: "/etc/cloud/cloud.cfg" register: common_out_cloud_cfg - name: Disable /etc/hosts management by cloud-init when: "common_out_cloud_cfg.stat.exists" block: - name: Disable /etc/hosts management by cloud-init (manage_etc_hosts) ansible.builtin.lineinfile: path: "/etc/cloud/cloud.cfg" search_string: 'manage_etc_hosts:' line: "manage_etc_hosts: false" insertafter: "preserve_hostname:" - name: Disable /etc/hosts management by cloud-init (update_etc_hosts) ansible.builtin.replace: path: "/etc/cloud/cloud.cfg" regexp: '^([^#]*update_etc_hosts.*)$' replace: '# \\1' - name: Configure /etc/hosts file ansible.builtin.template: src: "templates/hosts" dest: "/etc/hosts" mode: "0644"