ad-hoc搭建上传图片网站
环境准备
| 主机名 |
外网IP |
内网IP |
角色 |
| m01 |
10.0.0.61 |
172.16.1.61 |
ansible管理端 |
| backup |
10.0.0.41 |
172.16.1.41 |
ansible被管理端、rsync服务端、nfs服务端 |
| nfs |
10.0.0.31 |
172.16.1.31 |
ansible被管理端、rsync客户端、nfs服务端、sersync |
| web01 |
10.0.0.7 |
172.16.1.7 |
ansible被管理端、部署提交作业代码,挂载上传目录即可 |
| web02 |
10.0.0.8 |
172.16.1.8 |
ansible被管理端、部署提交作业代码,挂载上传目录即可 |
发送公钥
ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.7
ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.8
ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.31
ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.41
编辑主机清单
[web_group]
web01 ansible_ssh_host=172.16.1.7
web02 ansible_ssh_host=172.16.1.8
[nfs_group]
nfs ansible_ssh_host=172.16.1.31
[bakcup_group]
backup ansible_ssh_host=172.16.1.41
[rsync_install_group:children]
nfs_group
bakcup_group
[nfs_install_group:children]
web_group
nfs_group
bakcup_group
准备
1.准备rsync配置文件
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 = Backup to Rsync
path = /backup
2.httpd配置文件
vim /root/httpd.conf
User www
Group www
3.kaoshi.tgz包
编辑ad-hoc
[root@m01 ~]# cat ansible_web.sh
#!/bin/bash
# 基础操作
ansible all -m selinux -a 'state=disabled'
ansible all -m service -a 'name=firewalld state=stopped'
ansible all -m group -a 'name=www gid=666'
ansible all -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=no'
# 安装rsync
ansible rsync_install_group -m yum -a 'name=rsync state=present'
# 推送配置文件
ansible backup -m copy -a 'src=/root/rsync.moban dest=/etc/rsyncd.conf owner=root
group=root mode=0644'
# 创建目录
ansible backup -m file -a 'path=/backup owner=www group=www mode=0755 state=directory'
# 创建密码文件
ansible backup -m copy -a 'content="rsync_backup:123" dest=/etc/rsync.passwd owner=root group=root mode=0600'
# 启动服务
ansible backup -m service -a 'name=rsyncd state=started enabled=yes'
# 安装nfs
ansible nfs_install_group -m yum -a 'name=nfs-utils state=present'
# 编辑配置文件
ansible rsync_install_group -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports'
# 创建共享目录
ansible rsync_install_group -m file -a 'path=/data owner=www group=www mode=0755 state=directory'
# 启动nfs
ansible rsync_install_group -m service -a 'name=nfs-server state=started enabled=yes'
# 在nfs上创建rsync的密码文件
ansible nfs -m copy -a 'content=123 dest=/etc/rsync.passwd owner=root group=rootmode=0600'
# 部署web
ansible web_group -m yum -a 'name=httpd,php state=present'
# 推送配置文件
ansible web_group -m copy -a 'src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf'
# 部署代码
ansible web_group -m unarchive -a 'src=/root/kaoshi.tgz dest=/var/www/html'
# 创建上传目录
ansible web_group -m file -a 'path=/var/www/html/uploads owner=www group=www mode=0755 state=directory'
# 启动httpd服务
ansible web_group -m service -a 'name=httpd state=started enabled=yes'
# 挂载上传目录到nfs
ansible web_group -m mount -a 'path=/var/www/html/uploads src=172.16.1.31:/data fstype=nfs state=mounted'