Наиболее простой вариант настройки файла /etc/hosts
для всех хостов в одном play:
---
- name: Make sure all hosts are reachable via FQDN
hosts: all
become: true
gather_facts: true
tasks:
- name: Generate /etc/hosts file
ansible.builtin.template:
src: hosts.j2
dest: /etc/hosts
owner: root
group: root
mode: "0644"
tags:
- preconfigure
Файл hosts.j2
:
{{ ansible_managed | comment }}
127.0.0.1 localhost localhost.localdomain
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
{% for item in play_hosts %}
{% set short_name = hostvars[item]['ansible_facts']['fqdn'].split('.') %}
{% set fqdn_name = hostvars[item]['ansible_facts']['fqdn'] %}
{{ hostvars[item]['ansible_host'] }} {{ fqdn_name }} {{ short_name[0] }}
{% endfor %}
Про ansible_managed
можно почитать тут