一键部署nfs、rsync、sersync
项目代码:
链接:https://pan.baidu.com/s/13I0BBAYsdK-KmPekZ5VpdA
提取码:u2tw
--来自百度网盘超级会员V6的分享
目录结构
[root@m01 /ansible/roles]# tree -F
.
├── fenfa.sh #分发秘钥脚本
├── group_vars/ #主机组变量
│ └── all/
│ └── main.yml
├── hosts #hosts文件
├── nfs-client/
│ ├── files/
│ ├── handlers/
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
├── nfs-server/
│ ├── files/
│ ├── handlers/
│ │ └── main.yml
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ └── exports.j2
├── rsync-client/
│ ├── files/
│ ├── handlers/
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ ├── back-conf.j2
│ └── rsync.j2
├── rsync-server/
│ ├── files/
│ ├── handlers/
│ │ └── main.yml
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ └── rsyncd.j2
├── sersync-client/
│ ├── files/
│ ├── handlers/
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
├── sersync-server/
│ ├── files/
│ │ └── sersync2.5.4_64bit_binary_stable_final.tar.gz
│ ├── handlers/
│ ├── tasks/
│ │ └── main.yml
│ └── templates/
│ └── confxml.j2*
└── top.yml #启动文件
fenfa.sh文件内容
[root@m01 /ansible/roles]# cat fenfa.sh
#!/bin/bash
#author: wh
#version: v2
#desc: 一键创建秘钥对 分发秘钥对
#1.vars
pass=1 #服务器的密码
ips="172.16.1.7 172.16.1.31 172.16.1.41"
. /etc/init.d/functions
#1.4 判断是否联网或是否可以使用yum
#1.5 加入判断sshpass命令是否存在,如果不存在则安装
#2.创建秘钥对
if [ -f ~/.ssh/id_rsa ] ;then
echo "已经创建过秘钥对"
else
echo "正在创建秘钥对...."
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' &>/dev/null
if [ $? -eq 0 ];then
action "密钥创建成功" /bin/true
else
action "密钥创建失败" /bin/false
fi
fi
#3.通过循环发送公钥
for ip in $ips
do
sshpass -p${pass} ssh-copy-id -i ~/.ssh/id_rsa.pub -oStrictHostKeyChecking=no $ip &>/dev/null
if [ $? -eq 0 ];then
action "$ip 公钥分发 成功" /bin/true
else
action "$ip 公钥分发 失败" /bin/false
fi
done
hosts文件内容
[root@m01 /ansible/roles]# cat hosts
[web]
172.16.1.7
[nfs]
172.16.1.31
[backup]
172.16.1.41
启动文件top.yml文件内容
[root@m01 /ansible/roles]# cat top.yml
- hosts: nfs
roles:
- role: nfs-server
- role: rsync-client
- role: sersync-server
- hosts: backup
roles:
- role: rsync-server
- role: rsync-client
- role: sersync-client
- hosts: web
roles:
- role: rsync-client
- role: nfs-client
主机组变量文件内容
[root@m01 /ansible/roles]# cat group_vars/all/main.yml
#nfs的用户
nfs_user: nfsnobody
#nfs的共享的挂载目录
nfs_dir: /data
#nfs配置的共享目录
nfs_server_dir: "172.16.1.31:/data"
#web挂载nfs的目录
web_nfs_dir: /upload
#rsync用户
rsync_user: rsync
#rsync认证用户
rsync_auth_user: rsync_backup
#rsync服务端ip
rsync_server_ip: 172.16.1.41
#rsync备份配置文件的模板
rsync_module_name: backup
#rsync的备份共享目录
rsync_dir: /backup
#rsync密码文件
rsync_client_pass_dir: /etc/rsync.client
#rsync的密码
rsync_auth_password: 1
#sersync的nfs实时同步模块
sersync_module_name: nfsbackup
#sersync的nfs实时同步目录
sersync_dir: /nfsbackup
nfs客户端文件内容
[root@m01 /ansible/roles]# cat nfs-client/tasks/main.yml
- name: 安装nfs-utils
yum:
name: nfs-utils
state: present
- name: 挂载目录
mount:
src: "{{ nfs_server_dir }}"
path: "{{ web_nfs_dir }}"
fstype: nfs
state: mounted
nfs服务端文件内容
[root@m01 /ansible/roles]# cat nfs-server/tasks/main.yml
- name: 安装rpcbind,nfs-utils
yum:
name: "{{ item }}"
state: present
loop:
- rpcbind
- nfs-utils
- name: 创建共享目录,修改属主属组
file:
path: "{{ nfs