1、使用ansible的playbook實現(xiàn)自動化安裝httpd
主機:兩臺幢哨,一臺ansible主控端 192.168.17.7/24赡勘,一臺被控端 192.168.17.17/24
[root@ansible ~]# cat /etc/yum.repos.d/base.repo
[development]
name=dvdbase repo
baseurl=file:///mnt/cdrom/
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-7
[aliyunEpel]
name=aliyun epel
baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-$releasever
[root@ansible ~]# yum install -y ansible? #安裝Ansible
配置主機清單,并配置基于key驗證的ssh連接
[root@ansible ~]# vim /etc/ansible/hosts
#添加以下內(nèi)容
[websrvs]
192.168.17.17
#基于Key驗證
[root@ansible ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:qehCr9s+C0MGZ4U17a+9FyqIsKJMAoGuRYFJRFxSK4s root@ansible
The key's randomart image is:
+---[RSA 2048]----+
|=B+=+.? ? ? ? ? |
|+.oo...? ? ? ? ? |
|+.+. .? ? ? ? ? |
|o*o? .? .? ? ? |
|Eo+? ? .S? ? ? ? |
|++.? . .. .? ? ? |
|o=oo...o . .? ? |
|* o=+ o o .? ? ? |
|+.o==o ..o? ? ? |
+----[SHA256]-----+
[root@ansible ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.17.17
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.17.17 (192.168.17.17)' can't be established.
ECDSA key fingerprint is SHA256:TX1biesR1B8Gsz8MuCTPDZKPX3i4E051OkhQK/C9kBI.
ECDSA key fingerprint is MD5:83:8c:bb:e5:b9:a7:45:99:38:fc:5d:c8:89:cf:fb:5f.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.17.17's password:
Number of key(s) added: 1
Now try logging into the machine, with:? "ssh 'root@192.168.17.17'"
and check to make sure that only the key(s) you wanted were added.
建立playbook文件
[root@ansible ~]# ll files/httpd.conf #準(zhǔn)備httpd的配置文件
-rw-r--r-- 1 root root 11753 Feb 25 19:43 files/httpd.conf
[root@ansible ~]# vim httpd.yml
---
- hosts: websrvs
? remote_user: root
? tasks:
? ? - name: Install httpd
? ? ? yum: name=httpd state=present
? ? - name: Install configure file
? ? ? copy: src=files/httpd.conf dest=/etc/httpd/conf/
? ? - name: start service
? ? ? service: name=httpd state=started enabled=yes
使用playbook文件進(jìn)行httpd服務(wù)的安裝捞镰,并測試
[root@ansible ~]# ansible-playbook httpd.yml
PLAY [websrvs] **********************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [192.168.17.17]
TASK [Install httpd] ****************************************************************************************************************************
changed: [192.168.17.17]
TASK [Install configure file] *******************************************************************************************************************
ok: [192.168.17.17]
TASK [start service] ****************************************************************************************************************************
changed: [192.168.17.17]
PLAY RECAP **************************************************************************************************************************************
192.168.17.17? ? ? ? ? ? ? : ok=4? ? changed=2? ? unreachable=0? ? failed=0? ? skipped=0? ? rescued=0? ? ignored=0
[root@centos7 ~]# ss -ntl|grep 80? ? #被控端查看80端口已經(jīng)打開
LISTEN? ? 0? ? ? 128? ? ? ? :::80? ? ? ? ? ? ? ? ? ? ? :::*? ?
[root@centos7 ~]# ps -aux |grep httpd? ? #服務(wù)也已啟動
root? ? ? 7900? 0.0? 0.2 230408? 5148 ?? ? ? ? Ss? 19:48? 0:00 /usr/sbin/httpd -DFOREGROUND
apache? ? 7902? 0.0? 0.1 232492? 3152 ?? ? ? ? S? ? 19:48? 0:00 /usr/sbin/httpd -DFOREGROUND
apache? ? 7903? 0.0? 0.1 232492? 3152 ?? ? ? ? S? ? 19:48? 0:00 /usr/sbin/httpd -DFOREGROUND
apache? ? 7904? 0.0? 0.1 232492? 3152 ?? ? ? ? S? ? 19:48? 0:00 /usr/sbin/httpd -DFOREGROUND
apache? ? 7905? 0.0? 0.1 232492? 3152 ?? ? ? ? S? ? 19:48? 0:00 /usr/sbin/httpd -DFOREGROUND
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/www2.err,訪問日志為/var/log/httpd/y.access
(3)為兩個虛擬主機建立各自的主頁文件index.html拷邢,內(nèi)容分別為其對應(yīng)的主機名
#建立主機網(wǎng)頁目錄
[root@centos7 ~]# mkdir -p /web/vhosts/{x,y}
#授權(quán)apache用戶訪問
[root@centos7 ~]# chown -R root:apache /web
#建立各虛擬主機的主頁文件index.html
[root@centos7 ~]# echo www.x.com > /web/vhosts/x/index.html
[root@centos7 ~]# echo www.y.com > /web/vhosts/y/index.html
#建立虛擬主機配置
[root@centos7 ~]# vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
? ? ? ? ServerName www.x.com
? ? ? ? DocumentRoot "/web/vhosts/x"
? ? ? ? ErrorLog "/var/log/httpd/x.err"? ? #錯誤日志
? ? ? ? CustomLog "/var/log/httpd/x.access" combined? ? #訪問日志
? ? ? ? <Directory "/web/vhosts/x">
? ? ? ? ? ? ? ? Options None
? ? ? ? ? ? ? ? AllowOverride None
? ? ? ? ? ? ? ? Require all granted
? ? ? ? </Directory>
</VirtualHost>
<VirtualHost *:80>
? ? ? ? ServerName www.y.com
? ? ? ? DocumentRoot "/web/vhosts/y"
? ? ? ? ErrorLog "/var/log/httpd/www2.err"? ? #錯誤日志
? ? ? ? CustomLog "/var/log/httpd/y.access" combined? ? #訪問日志
? ? ? ? <Directory "/web/vhosts/y">
? ? ? ? ? ? ? ? Options None
? ? ? ? ? ? ? ? AllowOverride None
? ? ? ? ? ? ? ? Require all granted
? ? ? ? </Directory>
</VirtualHost>
#重啟httpd服務(wù)
[root@centos7 ~]# systemctl restart httpd
#創(chuàng)建本地解析
[root@centos7 ~]# vim /etc/hosts
127.0.0.1? localhost localhost.localdomain localhost4 localhost4.localdomain4
::1? ? ? ? localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.17.17? www.x.com? ? #添加此行
192.168.17.17? www.y.com? ? #添加此行
#本地訪問測試
[root@centos7 ~]# curl www.x.com
www.x.com
[root@centos7 ~]# curl www.y.com
www.y.com