1、使用ansible的playbook實(shí)現(xiàn)自動(dòng)化安裝httpd
主機(jī):兩臺(tái),一臺(tái)ansible主控端 192.168.17.7/24,一臺(tái)被控端 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
配置主機(jī)清單,并配置基于key驗(yàn)證的ssh連接
[root@ansible ~]# vim /etc/ansible/hosts
#添加以下內(nèi)容
[websrvs]
192.168.17.17
#基于Key驗(yàn)證
[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ù)的安裝,并測(cè)試
[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)打開(kāi)
LISTEN? ? 0? ? ? 128? ? ? ? :::80? ? ? ? ? ? ? ? ? ? ? :::*? ?
[root@centos7 ~]# ps -aux |grep httpd? ? #服務(wù)也已啟動(dòng)
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ù)器,要求提供兩個(gè)基于名稱的虛擬主機(jī):?
(1)www.X.com,頁(yè)面文件目錄為/web/vhosts/x;錯(cuò)誤日志為
/var/log/httpd/x.err,訪問(wèn)日志為/var/log/httpd/x.access
(2)www.Y.com,頁(yè)面文件目錄為/web/vhosts/y;錯(cuò)誤日志為?/var/log/httpd/www2.err,訪問(wèn)日志為/var/log/httpd/y.access
(3)為兩個(gè)虛擬主機(jī)建立各自的主頁(yè)文件index.html,內(nèi)容分別為其對(duì)應(yīng)的主機(jī)名
#建立主機(jī)網(wǎng)頁(yè)目錄
[root@centos7 ~]# mkdir -p /web/vhosts/{x,y}
#授權(quán)apache用戶訪問(wèn)
[root@centos7 ~]# chown -R root:apache /web
#建立各虛擬主機(jī)的主頁(yè)文件index.html
[root@centos7 ~]# echo www.x.com > /web/vhosts/x/index.html
[root@centos7 ~]# echo www.y.com > /web/vhosts/y/index.html
#建立虛擬主機(jī)配置
[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"? ? #錯(cuò)誤日志
? ? ? ? CustomLog "/var/log/httpd/x.access" combined? ? #訪問(wèn)日志
? ? ? ? <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"? ? #錯(cuò)誤日志
? ? ? ? CustomLog "/var/log/httpd/y.access" combined? ? #訪問(wèn)日志
? ? ? ? <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? ? #添加此行
#本地訪問(wèn)測(cè)試
[root@centos7 ~]# curl www.x.com
www.x.com
[root@centos7 ~]# curl www.y.com
www.y.com