inventory file
[servers]
master ansible_ssh_host=172.18.23.69
node1 ansible_ssh_host=172.18.23.70
node2 ansible_ssh_host=172.18.23.71
node3 ansible_ssh_host=172.18.23.72
templates/etc/hosts.j2
{% for item in ansible_play_batch %}
{{ hostvars[item].ansible_ssh_host }} {{ item }}.example.com
{% endfor %}
playbook task
- hosts: servers
gather_facts: False
tasks:
- name: update /etc/hosts file
become: true
blockinfile:
dest: /etc/hosts
content: "{{ lookup('template', 'templates/etc/hosts.j2') }}"
state: present