一、使用ansible的playbook实现自动化安装httpd
1、环境描述
ansible:172.16.31.7 centos7.4
httpd:172.16.31.6 centos7.4
2、安装ansible
ansible服务端
yum install -y ansible
3、打通ansible服务端至客户端ssh免密登陆
ansible服务端
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
检查配置文件内容
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、运行
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客户机状态
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服务器,要求提供两个基于名称的虚拟主机:
(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、创建目录/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、创建日志目录/var/log/httpd
mkdir -p /var/log/httpd
3、开启httpd-vhosts
修改httpd主配置文件
vi /etc/httpd/conf/httpd.conf
#注释掉默认document以及默认directory设置
#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、创建vhosts配置文件
vi /etc/httpd/conf/extra/httpd-vhosts.conf
<VirtualHost www.X.com:80> #主机名www.X.com
DocumentRoot "/web/vhosts/x" #根目录
ServerName www.X.com
ErrorLog "/var/log/httpd/x.err" #错误日志
CustomLog "/var/log/httpd/x.access" common #访问日志
<Directory "/web/vhosts/x">
options FollowSymLinks #禁止目录浏览
AllowOverride All
Require all granted #允许访问目录权限
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、重启服务
systemctl restart httpd
8、测试结果
[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