一、使用ansible的playbook实现自动化安装httpd
二、建立httpd服务器,要求提供两个基于名称的虚拟主机:
1、www.X.com,页面文件目录为/web/vhosts/x;错误日志为/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
#创建配置文件
vim /opt/http_conf/y_com.conf
<VirtualHost *:80>
ServerName www.X.com
DocumentRoot "/web/vhosts/x"
<Directory "/web/vhosts/x">
Options None
AllowOverride None
Require all granted
</Directory>
ErrorLog "logs/x.err"
CustomLog "logs/x.access" combined
</VirtualHost>
2、www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
# 创建配置文件
vim /opt/http_conf/y_com.conf
<VirtualHost *:80>
ServerName www.Y.com
DocumentRoot "/web/vhosts/y"
<Directory "/web/vhosts/y">
Options None
AllowOverride None
Require all granted
</Directory>
ErrorLog "logs/www2.err"
CustomLog "logs/y.access" combined
</VirtualHost>
3、为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名
# 分别创建index文件
echo "www.x.com" >/opt/http_index/x_index.html
echo "www.y.com" >/opt/http_index/y_index.html
(1)使用ansible推送配置文件以及主页文件
# 编辑playbooks文件
vim http_conf.yml
---
- hosts: webserver
remote_user: root
tasks:
- name: mkdir directory
shell: mkdir -p /web/vhosts/{x,y}
- name: copy x_com.conf to remotehost
copy: src=/opt/http_conf/x_com.conf dest=/etc/httpd/conf.d/x_com.conf
- name: copy x_com index file
copy: src=/opt/http_index/x_index.html dest=/web/vhosts/x/index.html
- name: copy y_com.conf to remotehost
copy: src=/opt/http_conf/y_com.conf dest=/etc/httpd/conf.d/y_com.conf
- name: copy y_com index file
copy: src=/opt/http_index/y_index.html dest=/web/vhosts/y/index.html
- name: restart httpd
service: name=httpd state=restarted
# 保存退出
# 模拟执行一次看是否有异常
ansible-playbook -C http_conf.yml
PLAY [webserver] *********************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [10.50.6.10]
ok: [10.50.6.8]
TASK [mkdir virtualhost documentroot directory] **************************************************************************************************************
skipping: [10.50.6.10]
skipping: [10.50.6.8]
TASK [copy x_com.conf to remotehost] *************************************************************************************************************************
changed: [10.50.6.10]
changed: [10.50.6.8]
TASK [copy x_com index file] *********************************************************************************************************************************
changed: [10.50.6.10]
changed: [10.50.6.8]
TASK [copy y_com.conf to remotehost] *************************************************************************************************************************
changed: [10.50.6.10]
changed: [10.50.6.8]
TASK [copy y_com index file] *********************************************************************************************************************************
changed: [10.50.6.10]
changed: [10.50.6.8]
TASK [restart httpd] *****************************************************************************************************************************************
changed: [10.50.6.8]
changed: [10.50.6.10]
PLAY RECAP ***************************************************************************************************************************************************
10.50.6.10 : ok=6 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
10.50.6.8 : ok=6 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
# 没有问题直接执行
ansible-playbook http_conf.yml
PLAY [webserver] *********************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [10.50.6.10]
ok: [10.50.6.8]
TASK [mkdir virtualhost documentroot directory] **************************************************************************************************************
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If you need to use command because file is insufficient you can
add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [10.50.6.10]
changed: [10.50.6.8]
TASK [copy x_com.conf to remotehost] *************************************************************************************************************************
changed: [10.50.6.8]
changed: [10.50.6.10]
TASK [copy x_com index file] *********************************************************************************************************************************
changed: [10.50.6.10]
changed: [10.50.6.8]
TASK [copy y_com.conf to remotehost] *************************************************************************************************************************
changed: [10.50.6.8]
changed: [10.50.6.10]
TASK [copy y_com index file] *********************************************************************************************************************************
changed: [10.50.6.10]
changed: [10.50.6.8]
SK [restart httpd] *****************************************************************************************************************************************
changed: [10.50.6.10]
changed: [10.50.6.8]
PLAY RECAP ***************************************************************************************************************************************************
10.50.6.10 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.50.6.8 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
# 出现警告提示用file创建文件夹更好
(2)编辑ansible主机的hosts文件
vim /etc/hosts
10.50.6.10 www.x.com
10.50.6.8 www.y.com
(3)测试访问是否正确
# 虽然两个主机都配置了x与y的页面,为了区分测试了2个主机不同的页面,也可以将hosts文件内的域名与主机调换一下做测试
[root@node3 opt]# curl http://www.x.com
www.x.com
[root@node3 opt]# curl http://www.y.com
www.y.com