--- - name: Hosts | populate inventory into hosts file ansible.builtin.blockinfile: dest: /etc/hosts block: |- {% for item in groups['os-cluster'] %} {{ hostvars[item]['ip'] }} {{ item }}.{{ domain_name }} {{ item }} {% endfor %} state: present create: true backup: true marker: "# Ansible inventory hosts {mark}" when: populate_inventory_to_hosts_file