2020-09-03第十六周

1、使用ansible的playbook實現(xiàn)自動化安裝httpd
yum安裝

yum install ansible -y

配置主機清單

vim /etc/ansible/hosts  
[websrvs]
172.16.100.46
172.16.100.47
[app]
172.16.100.48

創(chuàng)建ssh免密鑰登錄

[root@localhost ~]# ssh-keygen
[root@localhost ~]# ssh-copy-id 172.16.100.46
[root@localhost ~]# ssh-copy-id 172.16.100.47
[root@localhost ~]# ssh-copy-id 172.16.100.48

編寫yml文件

[root@localhost playbook]#vim httpd.yml 
---
- hosts: app
  remote_user: root
  tasks:
    - name: install
      yum: name=httpd
    - name: config
      copy: src=/data/playbook/httpd.conf dest=/etc/httpd/conf/
      notify: restart httpd
    - name: service
      service: name=httpd state=started enabled=yes
   
  handlers:
    - name: restart httpd
      service: name=httpd state=restarted

測試安裝

[root@localhost playbook]#ansible-playbook -C http.yml 

PLAY [app] *****************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [172.16.100.48]

TASK [install] *************************************************************************************************************************************************************
changed: [172.16.100.48]

TASK [config] **************************************************************************************************************************************************************
changed: [172.16.100.48]

TASK [service] *************************************************************************************************************************************************************
changed: [172.16.100.48]

RUNNING HANDLER [restart httpd] ********************************************************************************************************************************************
changed: [172.16.100.48]

PLAY RECAP *****************************************************************************************************************************************************************
172.16.100.48              : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost playbook]#ansible-playbook  http.yml 

PLAY [app] *****************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: [172.16.100.48]

TASK [install] *************************************************************************************************************************************************************
changed: [172.16.100.48]

TASK [config] **************************************************************************************************************************************************************
ok: [172.16.100.48]

TASK [service] *************************************************************************************************************************************************************
changed: [172.16.100.48]

PLAY RECAP *****************************************************************************************************************************************************************
172.16.100.48              : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

查看測試機是否啟動HTTP

[root@localhost local]# ss -ntlp
State      Recv-Q Send-Q                                                                                          Local Address:Port                                                                                                         Peer Address:Port              
LISTEN     0      128                                                                                                         *:22                                                                                                                      *:*                   users:(("sshd",pid=5038,fd=3))
LISTEN     0      100                                                                                                 127.0.0.1:25                                                                                                                      *:*                   users:(("master",pid=5573,fd=13))
LISTEN     0      128                                                                                                        :::80                                                                                                                     :::*                   users:(("httpd",pid=24562,fd=4),("httpd",pid=24561,fd=4),("httpd",pid=24560,fd=4),("httpd",pid=24559,fd=4),("httpd",pid=24558,fd=4),("httpd",pid=24557,fd=4))
LISTEN     0      128                                                                                                        :::22                                                                                                                     :::*                   users:(("sshd",pid=5038,fd=4))
LISTEN     0      100                                                                                                       ::1:25                                                                                                                     :::*                   users:(("master",pid=5573,fd=14))

2、建立httpd服務(wù)器,要求提供兩個基于名稱的虛擬主機:
(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/y.err,訪問日志為/var/log/httpd/y.access
(3)為兩個虛擬主機建立各自的主頁文件index.html,內(nèi)容分別為其對應(yīng)的主機名
建立主頁面

[root@localhost ~]# mkdir /web/vhosts/{x,y} -p
[root@localhost ~]# cd /web/vhosts/
[root@localhost vhosts]# echo www.X.com > x/index.html
[root@localhost vhosts]# echo www.Y.com > y/index.html

建立日志文件

[root@localhost vhosts]# cd /var/log/httpd/
[root@localhost httpd]# ls
access_log  error_log  error_log-20200913
[root@localhost httpd]# touch {x,y}.{err,access}
[root@localhost httpd]# ll
總用量 8
-rw-r--r-- 1 root root   0 9月  18 13:45 x.access
-rw-r--r-- 1 root root   0 9月  18 13:45 x.err
-rw-r--r-- 1 root root   0 9月  18 13:45 y.access
-rw-r--r-- 1 root root   0 9月  18 13:45 y.err

編寫配置文件

[root@localhost httpd]# vim /etc/httpd/conf.d/test.conf
<virtualhost *:80>
documentroot "/web/vhosts/x"
servername www.X.com
customlog "/var/log/httpd/x.assess" combined
errorlog  "/var/log/httpd/x.err"
  <Directory "/web/vhosts/x">
      Require all granted
  </Directory>
</virtualhost>

<virtualhost *:80>
documentroot "/web/vhosts/y"
servername www.Y.com
customlog "/var/log/httpd/y.assess" combined
errorlog  "/var/log/httpd/y.err"
  <Directory "/web/vhosts/y">
      Require all granted
  </Directory>
</virtualhost>

檢測語法是否正常

[root@localhost httpd]# httpd -t  

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@localhost conf]#  systemctl start httpd

增添解析配置

[root@localhost ~]# vi /etc/hosts
172.16.100.47 www.X.com www.Y.com

檢測

[root@localhost ~]# curl www.X.com
www.X.com
[root@localhost ~]# curl www.Y.com
www.Y.com

查看日志文件
[root@localhost ~]# tail -f /var/log/httpd/x.access
[root@localhost ~]# tail -f /var/log/httpd/y.access

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容