第十七周
1、使用ansible的playbook实现自动化安装httpd
三台主机:系统都是centos 7.6,需要先配置ssh的免密登录
ansible:192.168.10.128
apache1:192.168.10.130
apache2:192.168.10.131
-
安装软件包
在ansible主机上,安装ansible和httpd软件包,需要开启epel源
[root@ansible ~]#yum install ansible -y [root@ansible ~]#yum install httpd -y -
编辑ansble需要管理的主机
# 在文件的最后添加下面几行 [root@ansible ~]#vim /etc/ansible/hosts [webservers] 192.168.10.130 192.168.10.131 -
准备httpd配置文件
# 新建一个/data/playbook目录,把相关的文件都放在该目录下 [root@ansible ~]#mkdir /data/playbook # 拷贝httpd的配置文件到/data/playbook,作为自动化安装使用 [root@ansible ~]#cp /etc/httpd/conf/httpd.conf /data/playbook -
编写yaml文件
# 总共执行三个操作,使用yum安装httpd软件包,然后拷贝事先准备好的配置文件到远程主机,最后启动httpd服务并设置为开机启动 [root@ansible playbook]#vim /data/playbook/httpd.yml --- - hosts: webservers remote_user: root tasks: - name: install yum: name=httpd - name: config copy: src=/data/playbook/httpd.conf dest=/etc/httpd/conf/ - name: service service: name=httpd state=started enabled=yes -
执行playbook文件
# 先使用下面的命令做测试,如果没有问题再执行 [root@ansible playbook]#ansible-playbook -C /data/playbook/httpd.yml # 执行httpd.yml文件,对远程主机自动化安装httpd服务 [root@ansible playbook]#ansible-playbook /data/playbook/httpd.yml
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,内容分别为其对应的主机名
-
创建站点目录
[root@apache ~]#mkdir -p /web/vhosts/{x,y} -
编写index.html文件
[root@apache ~]#echo "www.X.com" > /web/vhosts/x/index.html [root@apache ~]#echo "www.Y.com" > /web/vhosts/y/index.html -
编辑配置文件,创建虚拟主机
# 新建/etc/httpd/conf.d/vhosts.conf文件,内容如下 [root@apache ~]#vim /etc/httpd/conf.d/vhost.conf <virtualhost *:80> documentroot /web/vhosts/x servername www.x.com ErrorLog "/var/log/httpd/x.err" CustomLog "/var/log/httpd/x.access" combined <Directory "/web/vhosts/x"> require all granted </Directory> </virtualhost> <virtualhost *:80> documentroot /web/vhosts/y servername www.y.com ErrorLog "/var/log/httpd/www2.err" CustomLog "/var/log/httpd/y.access" combined <Directory "/web/vhosts/y"> require all granted </Directory> </virtualhost> # 重启httpd服务 [root@apache ~]#systemctl restart httpd -
访问测试
# 在客户端的hosts文件中添加解析记录 [root@client ~]# vim /etc/hosts 192.168.10.128 www.X.com www.Y.com # 访问成功 [root@client ~]# curl www.X.com www.X.com [root@client ~]# curl www.Y.com www.Y.com

浙公网安备 33010602011771号