1、使用ansible的playbook实现自动化安装httpd
试验准备的一台主控端和三台被控端
主控端:192.168.37.101 被控端:192.168.37.102、192.168.37.105、192.168.37.106(1)、让主控端能免密登录到被控端端从而实现自动化安装
注意:主控端Python版本需要2.6或以上 使用 python --version查看版本ssh-keygen #主控端生成秘钥文件ssh-copy-id #被控端的ip地址(2)、主控端安装ansible
rpm包安装: 依赖EPEL源需先配置yum install ansible -y(3)、修改主控端配置文件添加被控制端主机ip清单并测试
vim /etc/ansible/hosts 添加如下:[servers]192.168.37.102192.168.37.105192.168.37.106ansible servers -m ping #ping测试被监控端连通性
(4)、编写playbook脚本,脚本需是已.yml或.yaml结尾
vim install_httpd.yml--
-
hosts: servers
remote_user: roottasks:
- name: install httpdyum: name=httpd state=present
- name: server startservice: name=httpd state=started enabled=yes
(5)、检查脚本语法并执行查看结果
ansible-playbook install_httpd.yml -C #检查脚本语法及脚本执行过程中有无报错,并不执行(6)、最后到其他三台被控端查看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,内容分别为其对应的主机名两台服务器实现:一台httpd服务端(192.168.37.102),一台客户端(192.168.73.105)验证结果
1、安装httpd服务
yum install httpd -y2、编辑修改相应配置文件
vim /etc/httpd/conf.d/eryuan.conf #新建一个子配置文件配置如下:<Virtualhost *:80>
documentroot /web/vhosts/xservername www.X.com<Directory "/web/vhosts/x">Require all granted</Directory>ErrorLog "/var/log/httpd/x.err"CustomLog "/var/log/httpd/x.access" combined</Virtualhost><Virtualhost *:80>
documentroot /web/vhosts/yservername www.Y.com<Directory "/web/vhosts/y">Require all granted</Directory>ErrorLog "/var/log/httpd/2.err"CustomLog "/var/log/httpd/y.access" combined</Virtualhost><Directory "/var/log/httpd">
Require all granted</Directory>3、建立配置文件中规定的相应目录并添加两台虚拟主机的主页内容
mkdir /web/vhosts/{x,y} -pvecho this is X > /web/vhosts/X/index.htmlecho this is Y > /web/vhosts/Y/index.html4、启动httpd服务并在客户端机器验证结果
systemctl start httpd在客户端主机上编辑本地解析如下:
vim /etc/hosts192.168.37.102 www.X.com www.Y.com验证访问结果:
精彩评论