1、将hosts信息部署到web服务器的index.html文件中,访问web服务器时显示如下信息:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.168.140 centos78master
192.168.168.21 node01
192.168.168.22 node02
[xiaoming@centos7 chap06]$ cat index.html.j2
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{% for host in groups.all %}
{{ hostvars[host].ansible_facts.default_ipv4.address }} {{ hostvars[host].ansible_facts.hostname }}
{% endfor %}
[xiaoming@centos7 chap06]$ cat play6.yml
---
- hosts: all
tasks:
- template:
src: index.html.j2
dest: /var/www/html/index.html
- service:
name: httpd
state: started
- service:
name: firewalld
state: stopped
[xiaoming@centos7 chap06]$ cat /var/www/html/index.html
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.40.122 centos7
192.168.40.121 node01
192.168.40.120 node02
网页测试如下: