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

1)定义要安装httpd的主机
[root@c1 ansible]# cat /etc/hosts
10.1.1.245 c4
10.1.1.246 c5
[root@c1 ansible]# pwd
/etc/ansible
[root@c1 ansible]# cat hosts |grep -v "#"
[test]
c4
c5 
2)编写yaml文件
[root@c1 ansible]# cat httpd.yml 
---
  - hosts: test

    tasks:
      - name: install Apache
        yum: name=httpd
      - name: start Apache
        service: name=httpd state=started enabled=yes
3)检查yaml文件的语法
[root@c1 ansible]# ansible-playbook -C httpd.yml 

PLAY [test] ***********************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************
ok: [c4]
ok: [c5]

TASK [install Apache] *************************************************************************************************
changed: [c5]
changed: [c4]

TASK [start Apache] ***************************************************************************************************
changed: [c5]
changed: [c4]

PLAY RECAP ************************************************************************************************************
c4                         : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
c5                         : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
4)执行安装
[root@c1 ansible]# ansible-playbook httpd.yml 

PLAY [test] ***********************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************
ok: [c5]
ok: [c4]

TASK [install Apache] *************************************************************************************************
changed: [c4]
changed: [c5]

TASK [start Apache] ***************************************************************************************************
changed: [c5]
changed: [c4]

PLAY RECAP ************************************************************************************************************
c4                         : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
c5                         : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
5)验证
[root@c1 ansible]# ansible test -m shell -a 'ss -tnl |grep 80'
c4 | CHANGED | rc=0 >>
LISTEN     0      128         :::80                      :::*                  
c5 | CHANGED | rc=0 >>
LISTEN     0      128         :::80                      :::*

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/www2.err,访问日志为/var/log/httpd/y.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名

1)安装httpd
使用第1节的方法
2)准备配置文件
[root@c4 conf.d]# pwd
/etc/httpd/conf.d
[root@c4 conf.d]# cat web.conf

    ServerName www.X.com
    DocumentRoot "/web/x.net/htdocs"
    ErrorLog "/var/log/httpd/x.err"
    CustomLog "/var/log/httpd/x.access" combined
    
        Options None
        AllowOverride None
        Require all granted
    



        ServerName www.Y.com
        DocumentRoot "/web/y.net/htdocs"
        ErrorLog "/var/log/httpd/y.err"
        CustomLog "/var/log/httpd/y.access" combined
        
                Options None
                AllowOverride None
                Require all granted
        

3)准备主页文件index.html
[root@c4 conf.d]# mkdir -pv /web/{x,y}.net/htdocs
[root@c4 ~]# echo "www.X.com" > /web/x.net/htdocs/index.html
[root@c4 ~]# echo "www.Y.com" > /web/y.net/htdocs/index.html
4)验证
[root@c4 ~]# echo "10.1.1.245 www.X.com www.Y.com" >> /etc/hosts
[root@c4 ~]# curl www.X.com
www.X.com
[root@c4 ~]# curl www.Y.com
www.Y.com

[root@c4 httpd]# pwd
/var/log/httpd
[root@c4 httpd]# tailf x.access 
192.168.10.33 - - [25/Apr/2020:19:34:17 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
192.168.10.33 - - [25/Apr/2020:19:34:17 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
192.168.10.33 - - [25/Apr/2020:19:34:18 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
192.168.10.33 - - [25/Apr/2020:19:34:18 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
192.168.10.33 - - [25/Apr/2020:19:34:18 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
192.168.10.33 - - [25/Apr/2020:19:34:18 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
192.168.10.33 - - [25/Apr/2020:19:34:18 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
192.168.10.33 - - [25/Apr/2020:19:35:00 +0800] "-" 408 - "-" "-"
192.168.10.33 - - [25/Apr/2020:19:35:00 +0800] "-" 408 - "-" "-"
10.1.1.245 - - [25/Apr/2020:19:41:08 +0800] "GET / HTTP/1.1" 200 10 "-" "curl/7.29.0"

[root@c4 httpd]# tailf y.access 
10.1.1.245 - - [25/Apr/2020:19:29:17 +0800] "GET / HTTP/1.1" 200 9 "-" "curl/7.29.0"
10.1.1.245 - - [25/Apr/2020:19:32:23 +0800] "GET / HTTP/1.1" 200 15 "-" "curl/7.29.0"
10.1.1.245 - - [25/Apr/2020:19:32:56 +0800] "GET / HTTP/1.1" 200 10 "-" "curl/7.29.0"
10.1.1.245 - - [25/Apr/2020:19:41:20 +0800] "GET / HTTP/1.1" 200 10 "-" "curl/7.29.0"