利用ansible一键化部署rsync服务

本文详细介绍rsync服务端及客户端的部署流程,包括软件安装、配置文件编写、虚拟用户创建、备份目录设置等步骤,并提供了Ansible playbook的yaml文件示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统环境

管理端:外网IP-10.0.0.61 内网IP-172.16.1.61
被控rsync服务端:外网IP-10.0.0.7 内网IP-172.16.1.7
被控rsync客户端:外网IP-10.0.0.51 内网IP-172.16.1.51

[root@m01 file]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@m01 file]# uname -r
3.10.0-862.el7.x86_64

主机清单配置文件

[root@m01 roles]# cat ../hosts 
[rsync_client]
172.16.1.51

[rsync_server]
172.16.1.7

提前准备好的本地配置文件

[root@m01 file]# pwd
/etc/ansible/file
[root@m01 file]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 12 17:14 rsyncd.conf
[root@m01 file]# cat 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 oldboy"
path =/backup

rsync服务部署流程(服务端)

1)安装rsync软件
2)创建并编写配置文件
3)创建rsync虚拟用户,禁止shell登录,不创建家目录。
4)创建备份目录,设置好属主属组信息。
5)创建密码文件。(用户名和密码)
6)启动rsync服务

rsync客户端部署流程

1)安装rsync软件
2)创建密码文件(只有密码)

yaml文件内容呈现

[root@m01 roles]# cat rsync.yaml 
- hosts: rsync_server
  tasks:
    - name: 01-install rsync
      yum: name=rsync state=installed
    - name: 02-create rsyncd.conf
      copy: src=/etc/ansible/file/rsyncd.conf dest=/etc
    - name: 03-create user rsync
      user: name=rsync create_home=no shell=/sbin/nologin
    - name: 04-create /backup directory
      file: path=/backup state=directory owner=rsync group=rsync
    - name: 05-create password file
      copy: content=rsync_backup:oldboy123 dest=/etc/rsync.password mode=600
    - name: 06-start rsync service
      service: name=rsyncd state=started enabled=yes

- hosts: rsync_client
  tasks:
    - name: 01-installl rsync
      yum: name=rsync state=installed
    - name: 02-create password file
      copy: content=oldboy123 dest=/etc/rsync.password mode=600
    - name: 03-create test file
      file: dest=/tmp/test.txt state=touch
    - name: 04-test rsync sending
      shell: rsync -avz /tmp/test.txt rsync_backup@172.16.1.7::backup --password-file=/etc/rsync.password

yaml文件执行顺序

1)首先检查语法格式是否正确

ansible-playbook --syntax-check rsync.yaml

2)其次模拟执行(模拟成功不代表真正执行一定成功)

ansible-playbook -C rsync.yaml

3)最后真正执行

ansible-playbook  rsync.yaml
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值