2020-09-03第十六周

1、使用ansible的playbook实现自动化安装httpd
yum安装

yum install ansible -y

配置主机清单

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

创建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服务器,要求提供两个基于名称的虚拟主机:
(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,内容分别为其对应的主机名
建立主页面

[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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。