一、使用ansible的playbook實(shí)現(xiàn)自動(dòng)化安裝httpd
1、環(huán)境描述
ansible:172.16.31.7 centos7.4
httpd:172.16.31.6 centos7.4
2、安裝ansible
ansible服務(wù)端
yum install -y ansible
3、打通ansible服務(wù)端至客戶端ssh免密登陸
ansible服務(wù)端
ssh-keygen
ssh-copy-id root@172.16.31.6
4、編寫yml
配置/etc/ansible/hosts
vi /etc/ansible/hosts
[httpd]
172.16.31.6
編寫playbook
vi /data/httpd.yml
---
- hosts: httpd
remote_user: root
tasks:
- name: install
yum: name=httpd
- name: start
service: name=httpd state=started enabled=yes
檢查配置文件內(nèi)容
ansible-playbook -C /data/httpd.yml
PLAY [httpd] *********************************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [172.16.31.6]
TASK [install] *******************************************************************************************
changed: [172.16.31.6]
TASK [start] *********************************************************************************************
changed: [172.16.31.6]
PLAY RECAP ***********************************************************************************************
172.16.31.6 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
5、運(yùn)行
ansible-playbook -C /data/httpd.yml
PLAY [httpd] *********************************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [172.16.31.6]
TASK [install] *******************************************************************************************
changed: [172.16.31.6]
TASK [start] *********************************************************************************************
changed: [172.16.31.6]
PLAY RECAP ***********************************************************************************************
172.16.31.6 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible ansible]# ansible-playbook /data/httpd.yml
PLAY [httpd] *********************************************************************************************
TASK [Gathering Facts] ***********************************************************************************
ok: [172.16.31.6]
TASK [install] *******************************************************************************************
changed: [172.16.31.6]
TASK [start] *********************************************************************************************
changed: [172.16.31.6]
PLAY RECAP ***********************************************************************************************
172.16.31.6 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
6、檢查httpd客戶機(jī)狀態(tài)
ps -ef |grep httpd
root 14905 1 0 15:22 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 14906 14905 0 15:22 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 14907 14905 0 15:22 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 14908 14905 0 15:22 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 14909 14905 0 15:22 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 14910 14905 0 15:22 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 14918 1328 0 15:22 pts/0 00:00:00 grep --color=auto httpd
二、2、建立httpd服務(wù)器,要求提供兩個(gè)基于名稱的虛擬主機(jī):
(1)www.X.com,頁(yè)面文件目錄為/web/vhosts/x;錯(cuò)誤日志為
/var/log/httpd/x.err,訪問日志為/var/log/httpd/x.access
(2)www.Y.com,頁(yè)面文件目錄為/web/vhosts/y;錯(cuò)誤日志為 /var/log/httpd/www2.err,訪問日志為/var/log/httpd/y.access
(3)為兩個(gè)虛擬主機(jī)建立各自的主頁(yè)文件index.html,內(nèi)容分別為其對(duì)應(yīng)的主機(jī)名
1、創(chuàng)建目錄/web/vhosts/x /web/vhosts/y,添加index文件
mkdir -p /web/vhosts/{x,y}
echo "ServerName www.X.com" >/web/vhosts/x/index.html
echo "ServerName www.Y.com" >/web/vhosts/y/index.html
2、創(chuàng)建日志目錄/var/log/httpd
mkdir -p /var/log/httpd
3、開啟httpd-vhosts
修改httpd主配置文件
vi /etc/httpd/conf/httpd.conf
#注釋掉默認(rèn)document以及默認(rèn)directory設(shè)置
#DocumentRoot "/var/www/html"
#
# Relax access to content within /var/www.
#
#<Directory "/var/www">
# AllowOverride None
# # Allow open access:
# Require all granted
#</Directory>
#<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
# Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
# AllowOverride None
#
# Controls who can get stuff from this server.
#
# Require all granted
#</Directory>
#最后行添加引入vhost配置文件
Include conf/extra/*.conf
4、創(chuàng)建vhosts配置文件
vi /etc/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost www.X.com:80> #主機(jī)名www.X.com
DocumentRoot "/web/vhosts/x" #根目錄
ServerName www.X.com
ErrorLog "/var/log/httpd/x.err" #錯(cuò)誤日志
CustomLog "/var/log/httpd/x.access" common #訪問日志
<Directory "/web/vhosts/x">
options FollowSymLinks #禁止目錄瀏覽
AllowOverride All
Require all granted #允許訪問目錄權(quán)限
DirectoryIndex index.html index.htm
</Directory>
</VirtualHost>
<VirtualHost www.Y.com:80>
DocumentRoot "/web/vhosts/y"
ServerName www.Y.com
ErrorLog "/var/log/httpd/y.err"
CustomLog "/var/log/httpd/y.access" common
<Directory "/web/vhosts/y">
options FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.html index.htm
</Directory>
</VirtualHost>
5、檢查配置文件
httpd -t
Syntax OK
6、修改hosts
vi /etc/hosts
172.16.31.6 httpd-qas.sh-ctmc.com httpd-qas www.X.com www.Y.com
7、重啟服務(wù)
systemctl restart httpd
8、測(cè)試結(jié)果
[root@httpd-qas vhosts]# curl http://www.X.com
ServerName www.X.com
[root@httpd-qas vhosts]# curl http://www.Y.com
ServerName www.Y.com
root@httpd-qas vhosts]# cd /var/log/httpd/
[root@httpd-qas httpd]# ll -tr
總用量 24
-rw-r--r-- 1 root root 4345 8月 14 15:47 access_log
-rw-r--r-- 1 root root 320 8月 14 15:51 x.err
-rw-r--r-- 1 root root 0 8月 14 15:57 y.err
-rw-r--r-- 1 root root 3708 8月 14 15:57 error_log
-rw-r--r-- 1 root root 358 8月 14 16:09 x.access
-rw-r--r-- 1 root root 138 8月 14 16:09 y.access