1、使用ansible的playbook实现自动化安装httpd
1. 安装
先安装epel源,配置好国内镜像源然后执行 yum install ansible -y
2. 编辑主机组
vim /etc/ansible/hosts
最下面添加:
[test] #方括号填写主机组名,例如db、server、nginx等
10.0.0.7 #主机ip
10.0.0.10
10.0.0.13
3.编写playbook脚本
vim install_httpd.yml
—
- name: install package
hosts: test
tasks:
- name: install httpd
yum:
name:
- httpd
state: present
4.检查语法:ansible-playbook -C install_httpd.yml
5.不报错执行: ansible-playbook install_httpd.yml
2、建立httpd服务器,要求提供两个基于名称的虚拟主机:
(1)www.X.com,页面文件目录为/web/vhosts/x;错误日志为
/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
在主控端操作:
1、先复制配置文件到当前目录修改
cp /etc/httpd/conf/httpd.conf httpd.conf -av
2、创建基于名称虚拟主机配置文件
vim /etc/httpd/conf.d/vhost.conf
3、修改主配置文件,添加刚刚创建的虚拟主机的配置
vim /etc/httpd/conf/httpd.conf
<Directory "/web/vhosts/x">
AllowOverride None
Options None
Require all granted
</Directory>
<Directory "/web/vhosts/y">
AllowOverride None
Options None
Require all granted
</Directory>
4、配置文件中的目录以及文件创建(脚本创建)
#!/bin/bash
useradd -s /sbin/nologin nginx
sed -i 's/User apache/User nginx/' /etc/httpd/conf/httpd.conf
sed -i 's/Group apache/Group nginx/' /etc/httpd/conf/httpd.conf
mkdir /web/vhosts/{x,y} -pv
mkdir /var/logs/httpd/{x,y} -pv
echo "www.x.com" > /web/vhosts/x/index.html
echo "www.y.com" > /web/vhosts/y/index.html
chown -R nginx.nginx /web/*
5、copy配置文件
ansible test -m copy -a 'src=/root/httpd.conf dest=/etc/httpd/conf/ backup=yes'
ansible test -m copy -a 'src=/root/httpd.conf dest=/etc/httpd/conf.d/ backup=yes'
6、重启服务
ansible test -m shell -a 'systemctl restart httpd'
7、访问刚刚的域名
(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名