1. ansible inventory 下面的 hosts内容如下:
bash
[all_host]
app1 ansible_host=10.2.162.147
app2 ansible_host=10.2.162.148
app3 ansible_host=10.2.162.149
app4 ansible_host=10.2.162.150
app5 ansible_host=10.2.162.151
[nginx]
app1
2. hosts.j2内容如下
bash
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{% for host in groups.all_host %}
{{ hostvars[host]['ansible_host'] }} {{ hostvars[host]['inventory_hostname'] }}
{% endfor %}
3. 渲染结果:
bash
cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.2.162.147 app1
10.2.162.148 app2
10.2.162.149 app3
10.2.162.150 app4
10.2.162.151 app5