使用ansible的playbook实现自动化安装httpd
实验环境:
主机 | os | 软件 | ip |
---|---|---|---|
ansible节点 | centos7.6 | 安装ansible | 172.16.2.132 |
ansible-host节点1 | centos7.6 | 172.16.2.137 | |
ansible-host节点2 | centos7.6 | 172.16.2.138 |
*在所有节点实现相互之间ssh key验证
实验设想
通过ansible 自动部署httpd服务,达到以下效果:
- ansible-host节点1:172.16.2.137
建立httpd服务器,要求提供两个基于名称的虚拟主机:
(1)www.X.com,页面文件目录为/web/vhosts/x;错误日志为/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名
- ansible-host节点2:172.16.2.138
建立httpd服务器,要求提供两个基于名称的虚拟主机:
(1)www.a.com,页面文件目录为/web/vhosts/a;错误日志为/var/log/httpd/a.err,访问日志为/var/log/httpd/a.access
(2)www.b.com,页面文件目录为/web/vhosts/b;错误日志为 /var/log/httpd/bwww2.err,访问日志为/var/log/httpd/b.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名
实现步骤
1.使用epel源 安装ansible
[root@node2 ~]# yum install ansible -y
2.修改主机hosts文件
vim /etc/ansible/hosts
[httpdservers]
172.16.2.137
172.16.2.138
3.基于role创建如下目录及文件
[root@node2 playbook]# tree
.
├── host_vars
│ ├── 172.16.2.137
│ └── 172.16.2.138
├── http_role.yml
└── roles
└── httpd
├── files
├── handlers
│ ├── main.yml
│ └── restart.yml
├── tasks
│ ├── config.yml
│ ├── index1.yml
│ ├── index2.yml
│ ├── install.yml
│ ├── main.yml
│ ├── service.yml
│ └── webdir.yml
├── templates
│ ├── index1.html.j2
│ ├── index2.html.j2
│ └── test.conf.j2
└── vars
4.准备需要的templates文件:
配置httpd模板
[root@node2 templates]# vim test.conf.j2
<virtualhost *:80>
documentroot /web/vhosts/{{ webdir1 }} //配置文件个性化之处使用变量处理
servername {{ vhost1 }}
<Directory "/web/vhosts/{{ webdir1 }}">
Require all granted
</Directory>
CustomLog "/var/log/httpd/{{ accesslog1 }}" combined
ErrorLog "/var/log/httpd/{{ errorlog1 }}"
</virtualhost>
<virtualhost *:80>
documentroot /web/vhosts/{{ webdir2 }}
servername {{ vhost2 }}
<Directory "/web/vhosts/{{ webdir2 }}">
Require all granted
</Directory>
CustomLog "/var/log/httpd/{{ accesslog2 }}" combined
ErrorLog "/var/log/httpd/{{ errorlog2 }}"
</virtualhost>
配置index.html模板
vim index1.html.j2
{{ vhost1 }}
vim index2.html.j2
{{ vhost2 }}
5.编辑task
[root@node2 tasks]# vim install.yml
- name: install //安装服务
yum: name=httpd
[root@node2 tasks]# vim config.yml
- name: config //复制conf到目标主机
template: src=test.conf.j2 dest=/etc/httpd/conf.d/test.conf
notify: restart //关联handles
[root@node2 tasks]# vim webdir.yml
- name: webdir1 //创建网站目录
file: path=//web/vhosts/{{webdir1}} state=directory
- name: webdir2
file: path=//web/vhosts/{{webdir2}} state=directory
[root@node2 tasks]# vim index1.yml
- name: index1 //复制index文件
template: src=index1.html.j2 dest=/web/vhosts/{{webdir1}}/index.html
[root@node2 tasks]# vim index2.yml
- name: index2
template: src=index2.html.j2 dest=/web/vhosts/{{webdir2}}/index.html
[root@node2 tasks]# vim service.yml
- name: service //启动服务
service: name=httpd state=started enabled=yes
[root@node2 tasks]# vim main.yml
- include: install.yml
- include: config.yml
- include: webdir.yml
- include: index1.yml
- include: index2.yml
- include: service.yml
6.编辑handles
[root@node2 handlers]# vim restart.yml
- name: restart
service: name=httpd state=restarted
[root@node2 handlers]# vim main.yml
- include: restart.yml
7.编辑基于各主机host_vars
*注意host_vars目录的位置
[root@node2 host_vars]# vim 172.16.2.137
webdir1: x
webdir2: y
errorlog1: "e.err"
errorlog2: "www2.err"
accesslog1: "x.access"
accesslog2: "y.access"
vhost1: "www.X.com"
vhost2: "www.Y.com"
[root@node2 host_vars]# vim 172.16.2.138
webdir1: a
webdir2: b
errorlog1: "ae.err"
errorlog2: "bwww2.err"
accesslog1: "a.access"
accesslog2: "b.access"
vhost1: "www.A.com"
vhost2: "www.B.com"
8.编辑基于role的yml
[root@node2 playbook]# cat http_role.yml
- hosts: httpdservers
roles:
- httpd
9.检查配置
[root@node2 playbook]# ansible-playbook -C http_role.yml
PLAY [httpdservers] *****************************************************************************************
TASK [Gathering Facts] **************************************************************************************
ok: [172.16.2.137]
ok: [172.16.2.138]
TASK [httpd : install] **************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]
TASK [httpd : config] ***************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]
TASK [httpd : webdir1] **************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]
TASK [httpd : webdir2] **************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]
TASK [httpd : index1] ***************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]
TASK [httpd : index2] ***************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]
TASK [httpd : service] **************************************************************************************
changed: [172.16.2.138]
changed: [172.16.2.137]
RUNNING HANDLER [httpd : restart] ***************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]
PLAY RECAP **************************************************************************************************
172.16.2.137 : ok=9 changed=8 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.2.138 : ok=9 changed=8 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.正式部署
[root@node2 playbook]# ansible-playbook http_role.yml
11.验证
[root@node2 templates]# cat /etc/hosts //查看管理主机的hosts文件
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.2.137 www.X.com www.Y.com //同一个ip对应两个不同的虚拟主机地址
172.16.2.138 www.a.com www.b.com
[root@node2 templates]# curl www.X.com 使用curl访问
www.X.com
[root@node2 templates]# curl www.Y.com
www.Y.com
[root@node2 templates]# curl www.a.com
www.A.com
[root@node2 templates]# curl www.b.com
www.B.com
建立httpd服务器,要求提供两个基于名称的虚拟主机
实验环境:
服务器端: centos7.6 ip:172.16.2.137 安装httpd
客户端: centos7.6 ip:172.16.2.132 修改hosts文件增加两个虚拟主机ip
实验设想
建立httpd服务器,要求提供两个基于名称的虚拟主机:
(1)www.X.com,页面文件目录为/web/vhosts/x;错误日志为/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名
实验步骤
1.安装服务
yum install -y httpd
2.编译配置文档
[root@node3 etc]# vim /etc/httpd/conf.d/test.conf
<virtualhost *:80>
documentroot /web/vhosts/x //网站主目录
servername www.X.com //设置域名
<Directory "/web/vhosts/x">
Require all granted //权限
</Directory>
CustomLog "/var/log/httpd/x.access" combined //设置log
ErrorLog "/var/log/httpd/e.err"
</virtualhost>
<virtualhost *:80>
documentroot /web/vhosts/y
servername www.Y.com
<Directory "/web/vhosts/y">
Require all granted
</Directory>
CustomLog "/var/log/httpd/y.access" combined
ErrorLog "/var/log/httpd/www2.err"
</virtualhost>
3.创建目录:
[root@node3 etc]# mkdir /web/vhosts/{x,y}
4.创建主页文件:
[root@node3 etc]# cat /web/vhosts/x/index.html
www.X.com
[root@node3 etc]# cat /web/vhosts/y/index.html
www.Y.com
5.启动服务:
systemctl start httpd
6.验证:
[root@node2 templates]# cat /etc/hosts //查看客户端host是文件
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.2.137 www.X.com www.Y.com //同一个ip对应两个不同的虚拟主机地址
[root@node2 templates]# curl www.X.com
www.X.com
[root@node2 templates]# curl www.Y.com
www.Y.com