rsync数据同步与nfs的ansible部署
特别说明
创建一个专门写剧本的目录
mkdir /etc/ansible/ansible-playbook
创建一个脚本的执行目录
mkdir /server/scripts -p
1. rsync的部署
1.1 主机清单的配置
vim /etc/ansible/hosts
[rsync_server]
172.16.1.41
[rsync_client]
#172.16.1.7
172.16.1.31
1.2 配置文件的准备
具体的步骤:rsync备份配置
创建配置文件目录
mkdir /etc/ansible/file
rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = "backup dir by yq"
path =/backup
服务器端的rsync.password
rsync_backup:123456
目录结构:
1.3 剧本的编写
剧本的步骤如1.2的备份配置一样。点击查看详细
rsync.yaml
- hosts: rsync_server
tasks:
- name: 01-yum-rsync
yum: name=rsync state=installed
- name: 02-push-conf-file and password
copy: src=/etc/ansible/file/{{item.src}} dest={{item.dest}} mode={{item.mode}}
with_items:
- {src: 'rsyncd.conf',dest: '/etc/',mode: '644'}
- {src: 'rsync.password',dest: '/etc/',mode: '600'}
notify: re-rsyncd
- name: 03-create-user
user: name=rsync create_home=no shell=/sbin/nologin
- name: 04-touch- directory
file: path=/backup state=directory owner=rsync group=rsync
- name: 06-start
service: name=rsyncd state=started enabled=yes
- name: 07-check
shell: ' netstat -lntup|grep 873'
register: result
- name: 08-show
debug: msg={{result.stdout_lines}}
handlers:
- name: re-rsyncd
service: name=rsyncd state=restarted
- hosts: rsync_client
tasks:
- name: 01-yum-rsync
yum: name=rsync state=installed
- name: 02-create-password-file
copy: content=123456 dest=/etc/rsync.password mode=600
- name: 03-test
file: path=/yang/yq02_nfs.txt state=touch
when: (ansible_hostname == "nfs01")
- name: 04-check-rsync
shell: rsync -avz /yang/yq02_nfs.txt rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
1.4 剧本的运行之前准备
运行剧本之前,要进行在管理主机上,配置ssh免密登录:
免密登录的脚本:可点击查看此文字:ssh具体配置
注意:我使用的ip地址:172.16.1.31和172.16.1.41
根据自己的ip地址不同进行更改脚本
也可以搜一下怎么配置centos的ip地址
分发:
fenfa.sh
#!/bin/bash
for ip in 31 41
do
echo "====================start fenfa 172.16.1.$ip===================="
sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.$ip "-o StrictHostKeyChecking=no" &>/dev/null
echo "=================host 172.16.1.$ip success!!!!=================="
echo "==================== end ===================="
done
运行脚本:
sh fenfa.sh
检查是否成功:
运行下边的脚本
sh check.sh
#!/bin/bash
for ip in 31 41
do
echo "====================check 172.16.1.$ip===================="
ssh 172.16.1.$ip hostname
echo "==================== end ===================="
done
1.5 运行剧本
检查剧本:
ansible-playbook --syntax-check rsync.yaml
本地运行检查:
ansible-playbook -C rsync.yaml
运行剧本:
ansible-playbook rsync.yaml
2. nfs的剧本
nfs的部署流程:点击蓝色字体查看nfs的部署流程
2.1 创建文件目录
目录结构:
创建目录:
mkdir /etc/ansible/nfs-file/{nfs-server,nfs-client} -p
2.2 配置文件准备
vim /etc/ansible/nfs-file/nfs-server/exports
/data 172.16..1.0/24(rw,sync)
2.3 主机清单的配置
vim /etc/ansible/hosts
添加
[nfs:children]
nfs_server
nfs_client
[nfs_server]
172.16.1.31
[nfs_client]
172.16.1.7
2.4 剧本编写
cd /etc/ansible/ansible-playbook/nfs.yaml
- hosts: nfs
tasks:
- name: 01-yum-rpc-nfs
yum:
name: ['nfs-utils','rpcbind']
state: installed
- hosts: nfs_server
vars:
Conf_dir: /etc/ansible/nfs-file/nfs-server/exports
tasks:
- name: 02-copy-file
copy: src={{Conf_dir}} dest=/etc
- name: 03-createdata-dir
file: path=/data state=directory owner=nfsnobody group=nfsnobody
notify: re-boot
- name: 04-boot
service: name={{item}} state=started enabled=yes
with_items:
- rpcbind
- nfs
handlers:
- name: re-boot
service: name=nfs state=restarted
- hosts: nfs_client
vars:
Data_dir: /data
tasks:
- name: 01-mount
mount: src=172.16.1.31:{{Data_dir}} path=/mnt state=mounted fstype=nfs
- name: 02-shell
shell: df -h|grep data
register: result
- name: 03-show
debug: msg={{result.stdout_lines}}
2.5 剧本的运行
ansible-playbook nfs.yaml