一、使用 ansible 的 playbook 实现自动化安装 httpd
1、实验环境(均用root用户)
控制服务器:10.10.10.71
应用服务器:10.10.10.72,10.10.10.73
2、控制服务器配置
- 安装 ansible
yum install -y ansible
- 添加主机信息
echo -e '[websvr]\n10.10.10.72\n10.10.10.73' >> /etc/ansible/hosts
- 生成公钥和私钥
ssh-keygen # 一直按回车
- 将公钥复制到应用服务器
ssh-copy-id root@10.10.10.72 # 输入 yes 和 远程服务器密码
ssh-copy-id root@10.10.10.73
- 准备 httpd.conf 配置文件(此处用 yum 安装方式,实际可以从别的服务器复制)
yum install -y httpd
mkdir -p /root/playbook/files
cp /etc/httpd/conf/httpd.conf /root/playbook/files
- 创建 Playbook 文件
vim /root/playbook/httpd.yml
- playbook 文件内容
---
- hosts: websvr
remote_user: root
tasks:
- name: install httpd
yum: name=httpd
- name: copy config file
copy: src=/root/playbook/files/httpd.conf dest=/etc/httpd/conf/
notify: restart httpd
- name: create index file
shell: echo `hostname -I` > /var/www/html/index.html
- name: service
service: name=httpd state=started enabled=yes
handlers:
- name: restart httpd
service: name=httpd state=restarted
3、执行(控制服务器)
[root@localhost ~]# ansible-playbook /root/playbook/httpd.yml
PLAY [websvr] ***************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
ok: [10.10.10.72]
ok: [10.10.10.73]
TASK [install httpd] ********************************************************************************************************************
changed: [10.10.10.73]
changed: [10.10.10.72]
TASK [copy config file] *****************************************************************************************************************
ok: [10.10.10.72]
ok: [10.10.10.73]
TASK [create index file] ****************************************************************************************************************
changed: [10.10.10.72]
changed: [10.10.10.73]
TASK [service] **************************************************************************************************************************
changed: [10.10.10.73]
changed: [10.10.10.72]
PLAY RECAP ******************************************************************************************************************************
10.10.10.72 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.10.10.73 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4、验证(在任一服务器执行)
[root@localhost ~]# curl 10.10.10.72
10.10.10.72
[root@localhost ~]# curl 10.10.10.73
10.10.10.73
二、建立httpd服务器,要求提供两个基于名称的虚拟主机
(1)ww.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/y.err,访问日志为/var/log/httpd/y.access;
(3)为两个虚拟主机建立各自的主页文件 index.html,内容分别为其对应的主机名。
- 安装
yum install -y httpd
- 创建相关目录和文件
mkdir -p /web/vhost/{x,y}
echo www.x.com > /web/vhost/x/index.html
echo www.y.com > /web/vhost/y/index.html
- 修改配置文件
vim /etc/httpd/conf/httpd.conf
# 添加以下内容
<virtualhost *:80>
servername www.x.com
documentroot /web/vhost/x
errorlog /var/log/httpd/x.err
customlog /var/log/httpd/x.access common
<directory "/web/vhost/x">
require all granted
</directory>
</virtualhost>
<virtualhost *:80>
servername www.y.com
documentroot /web/vhost/y
errorlog /var/log/httpd/y.err
customlog /var/log/httpd/y.access common
<directory "/web/vhost/y">
require all granted
</directory>
</virtualhost>
- 重启 httpd 服务
systemctl restart httpd
- 测试
# hosts 文件,添加以内容
echo 127.0.0.1 www.x.com www.y.com >> /etc/hosts
# 用域名访问
[root@localhost ~]# curl www.x.com
www.x.com
[root@localhost ~]# curl www.y.com
www.y.com
# 查看日志文件
[root@localhost ~]# ll /var/log/httpd/
total 16
-rw-r--r-- 1 root root 257 Aug 11 16:37 access_log
-rw-r--r-- 1 root root 3823 Aug 11 17:13 error_log
-rw-r--r-- 1 root root 205 Aug 11 17:14 x.access
-rw-r--r-- 1 root root 0 Aug 11 16:46 x.err
-rw-r--r-- 1 root root 67 Aug 11 17:14 y.access
-rw-r--r-- 1 root root 0 Aug 11 16:46 y.err