ansible部署网站
作者:互联网
作业:
1.部署rsync
2.部署nfs
3.部署httpd,将zuoye代码部署进去,挂载上传作业的目录
环境准备
主机名 | WanIP | LanIP | 角色 | 部署应用 |
---|---|---|---|---|
web01 | 10.0.0.7 | 172.16.1.7 | nfs客户端、rsync服务端 | httpd、php、nfs、rsync |
web02 | 10.0.0.8 | 172.16.1.8 | nfs客户端、rsync客户端 | httpd、php、nfs、rsync |
nfs | 10.0.0.31 | 172.16.1.31 | nfs服务端 | nfs |
添加主机池
[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
[nfs]
nfs ansible_ssh_host=10.0.0.31
# 将私钥发送给主机池里的服务器
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.7
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.8
[root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.31
部署rsync
1.先准备rsync配置文件
[root@backup ~]# vim /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup
## 服务端
2.编写脚本
vim rsync.sh
# 安装rsync
ansible web01 -m yum -a 'name=rsync'
# 修改rsync配置文件
ansible web01 -m copy -a 'src=/etc/rsyncd.conf dest=/etc/'
# 创建密码文件并授权为600
ansible web01 -m copy -a 'content="rsync_backup:123456" dest=/etc/rsync.passwd mode=600'
# 创建www用户
ansible web_group -m group -a 'name=www gid=666'
ansible web_group -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=no'
# 创建备份目录
ansible web01 -m file -a 'path=/backup owner=www group=www state=directory'
# 启动服务
ansible web01 -m service -a 'name=rsyncd state=started enabled=true enabled=yes'
## 部署rsync(客户端)
# 安装
ansible web02 -m yum -a 'name=rsync'
# 创建密码文件
ansible web02 -m copy -a 'content="123456" dest=/etc/rsync.passwd mode=600'
3.执行脚本
[root@m01 ~]# sh rsync.sh
4.推数据测试是否成功
部署httpd
1.先安装apache
yum install -y httpd
2.修改apache的启动用户
66 User www
67 Group www
## 部署web网站
1.编写脚本
vim web.sh
# 安装apache和php
ansible web_group -m yum -a 'name=httpd,php state=present'
# 将修改好的httpd配置文件发送给其他机器
ansible web_group -m copy -a 'src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/ backup=yes'
# 将压缩包放入站点目录中
ansible web_group -m copy -a 'src=/root/kaoshi_modify.zip dest=/var/www/html/'
# 解压压缩包
ansible web_group -m command -a 'unzip /var/www/html/kaoshi_modify.zip -d /var/www/html/'
# 授权站点目录
ansible web_group -m command -a 'chown -R www.www /var/www/html'
# 启动apache
ansible web_group -m service -a 'name=httpd state=started enabled=yes'
2.执行脚本
sh web.sh
3.浏览器访问上传
部署nfs
1.编写脚本
vim nfs.sh
# 安装nfs
ansible nfs -m yum -a 'name=nfs-utils'
# 编写nfs配置文件
ansible nfs -m copy -a 'dest=/etc/exports content="/data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)"'
# 创建www用户
ansible nfs -m group -a 'name=www gid=666'
ansible nfs -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=no'
# 创建/data目录
ansible nfs -m file -a 'path=/data owner=www group=www state=directory'
# 启动nfs并加入开机自启
ansible nfs -m service -a 'name=nfs-utils state=started enabled=yes'
# 挂载目录
ansible web_group -m mount -a 'path=/var/www/html/user_data/ src=172.16.1.31:/data fstype=nfs state=mounted'
2.查看挂载目录
[root@m01 ~]# ansible web_group -m command -a 'df -h'
[WARNING]: Found both group and host with same name: nfs
web02 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.5G 18G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.8M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 497M 120M 378M 25% /boot
tmpfs 98M 0 98M 0% /run/user/0
172.16.1.31:/data 19G 1.4G 18G 8% /var/www/html/user_data
web01 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.5G 18G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 14M 473M 3% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 497M 120M 378M 25% /boot
tmpfs 98M 0 98M 0% /run/user/0
172.16.1.31:/data 19G 1.4G 18G 8% /var/www/html/user_data
标签:web,www,group,部署,rsync,网站,ansible,nfs 来源: https://www.cnblogs.com/zlyj/p/16417273.html