使用playbook实现一键部署rsync

awoyaoc 2020-06-14

环境

主机名wanIPlanIP服务角色
m0110.0.0.61172.16.1.61Ansible控制端
backup10.0.0.41172.16.1.41rsync服务端被控端
web0110.0.0.7172.16.1.7rsync客户端被控端
web0210.0.0.8172.16.1.8rsync客户端被控端
nfs10.0.0.31172.16.131rsync客户端被控端

流程分析

1.安装ansible
2.优化ansible
3.推送公钥
4.开启防火墙
5.开启80 443 873 nfs等端口和服务白名单
6.关闭selinux
7.创建同一的用户

1.web backup nfs 安装rsync
2.拷贝rsync配置文件
3.创建服务端backup的备份目录
4.copy密码文件
5.把客户端密码加入环境全局变量文件
6.启动rsync,并加入开机自启动

配置主机清单

mkdir /root/ansible/rsync -p && vim /root/ansible/rsync/hosts
[web_group]
web01 ansible_ssh_host=172.16.1.7 asible_ssh_user=root ansible_ssh_port=22
web02 ansible_ssh_host=172.16.1.8 asible_ssh_user=root ansible_ssh_port=22

[nfs_group]
nfs ansible_ssh_host=172.16.1.31 asible_ssh_user=root ansible_ssh_port=22

[backup_group]
backup ansible_ssh_host=172.16.1.41 asible_ssh_user=root ansible_ssh_port=22

rsync配置文件

vim /root/ansible/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 = backup			 
secrets file = /etc/rsync.passwd	  
log file = /var/log/rsyncd.log		  						 
[backup]								
comment = welcome to oldboyedu backup!	   
path = /backup

yml

vim /root/ansible/rsync/rsync.yml

- hosts: all 
  tasks: 
    - name: Install Rsync Server 
      yum: 
        name: rsync 
        state: present 
 
    - name: selicent pass 
      copy: 
        content: "export RSYNC_PASSWORD=123" 
        dest: /etc/profile.d/rsync.pass 
        owner: root 
        group: root 
        mode: 0600 
      when: ansible_hostname is match "web*" 
       
    - name: sourse 
      shell: "source /etc/profile.d/rsync.pass" 
      when: ansible_hostname is match "web*" 
 
    - name: selicent pass 
      copy: 
        content: "export RSYNC_PASSWORD=123" 
        dest: /etc/profile.d/rsync.pass 
        owner: root 
        group: root 
        mode: 0600 
      when: ansible_hostname is match "nfs*"  
       
    - name: sourse 
      shell: "source /etc/profile.d/rsync.pass" 
      when: ansible_hostname is match "nfs*"  
 
    - name: Configure Rsync Conf 
      copy: 
        src: /root/ansible/rsync/rsyncd.conf 
        dest: /etc/rsyncd.conf 
        owner: root 
        group: root 
        mode: 0644 
      when: ansible_hostname is match "backup*" 

    - name: Create Backup Dir 
      file: 
        path: /backup 
        recurse: yes 
        owner: www 
        group: www 
        mode: 0755 
        state: directory 
      when: ansible_hostname is match "backup*" 

    - name: Create PASS File 
      copy: 
        content: backup:123 
        dest: /etc/rsync.passwd 
        owner: root 
        group: root 
        mode: 0600 
      when: ansible_hostname is match "backup*" 

- hosts: all 
  tasks: 
    - name: Start Rsync Server 
      service: 
        name: rsyncd 
        state: started 
        enabled: true

执行

1.执行base.yml
[ ~]# ansible-playbook ansible/base.yml 

2.执行rsync.yml
[ ~]# ansible-playbook ansible/rsync/rsync.yml -i /root/ansible/rsync/hosts

相关推荐