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
一、写template模块执行的j2文件
[root@server79 ansible]# vim host.conf.j2 #把http的配置样本文件用变量写在将j2文件里
<virtualhost {
{ansible_facts.default_ipv4.address}}:80>
servername {
{ansible_facts.default_ipv4.address}}
documentroot /var/www/html
</virtualhost>
<directory /var/www/html>
allowoverride none
require all granted
</directory>
[root@server79 ansible]# vim index.html.j2 #把网页主界面的index文件也用变量写在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 %} #这里采用循环来获取所有受管主机的主机名,采用魔法变量编写要显示的主机和ip
二、写playbook文件并检查执行
[root@server79 ansible]# vim http.yml #编写playbook文件
---
- name: http service conf play
hosts: all
tasks:
- name: httpd conf template
template:
src: ./host.conf.j2
dest: /etc/httpd/conf.d/host.conf
- name: restarted http
service:
name: httpd
state: restarted
- name: write index.html template
template:
src: ./index.html.j2
dest: /var/www/html/index.html
[root@server79 ansible]# ansible-playbook --syntax-check http.yml
playbook: http.yml #检查语法
[root@server79 ansible]# ansible-playbook http.yml #执行playbook文件
PLAY [http service conf play] **********************************************************
TASK [Gathering Facts] *****************************************************************
ok: [server79]
ok: [node1]
ok: [node0]
TASK [httpd conf template] *************************************************************
changed: [node1]
changed: [server79]
changed: [node0]
TASK [restarted http] ******************************************************************
changed: [server79]
changed: [node1]
changed: [node0]
TASK [write index.html template] *******************************************************
changed: [node1]
changed: [server79]
changed: [node0]
PLAY RECAP *****************************************************************************
node0 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node1 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server79 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
三、检查执行后的结果
#检查网页内容
[root@server79 ansible]# curl http://node0
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.32.147 node1-79
192.168.32.137 server79
192.168.32.138 node0-8
[root@server79 ansible]# curl http://node1
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.32.147 node1-79
192.168.32.137 server79
192.168.32.138 node0-8
[root@server79 ansible]# curl http://server79
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.32.147 node1-79
192.168.32.137 server79
192.168.32.138 node0-8