一、使用ansible的playbook實(shí)現(xiàn)自動(dòng)化安裝httpd
二栓辜、建立httpd服務(wù)器,要求提供兩個(gè)基于名稱的虛擬主機(jī):
1、www.X.com嚣镜,頁面文件目錄為/web/vhosts/x;錯(cuò)誤日志為/var/log/httpd/x.err,訪問日志為/var/log/httpd/x.access
#創(chuàng)建配置文件
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;錯(cuò)誤日志為 /var/log/httpd/www2.err,訪問日志為/var/log/httpd/y.access
# 創(chuàng)建配置文件
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计福、為兩個(gè)虛擬主機(jī)建立各自的主頁文件index.html跌捆,內(nèi)容分別為其對(duì)應(yīng)的主機(jī)名
# 分別創(chuàng)建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
# 保存退出
# 模擬執(zhí)行一次看是否有異常
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
# 沒有問題直接執(zhí)行
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
# 出現(xiàn)警告提示用file創(chuàng)建文件夾更好
(2)編輯ansible主機(jī)的hosts文件
vim /etc/hosts
10.50.6.10 www.x.com
10.50.6.8 www.y.com
(3)測(cè)試訪問是否正確
# 雖然兩個(gè)主機(jī)都配置了x與y的頁面,為了區(qū)分測(cè)試了2個(gè)主機(jī)不同的頁面象颖,也可以將hosts文件內(nèi)的域名與主機(jī)調(diào)換一下做測(cè)試
[root@node3 opt]# curl http://www.x.com
www.x.com
[root@node3 opt]# curl http://www.y.com
www.y.com