1.生成固定的目录结构
[root@ansible roles]# ansible-galaxy init rsync
- Role rsync was created successfully
2.配置tasks
[root@ansible roles]# cat rsync/tasks/main.yml
- name: Install Rsync Server
yum:
name: rsync
state: present
- name: Copy Srsync Configure File
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode }}"
loop:
- { src: rsyncd.conf.j2, dest: /etc/rsyncd.conf,mode: '0644' }
- { src: rsync.passwd.j2, dest: /etc/rsync.passwd,mode: '0600' }
notify: Restart Rsyncd Server
- name: Create Dir "{{ rsync_dir }}"
file:
path: /{{ rsync_dir }}
state: directory
owner: "{{ rs_user }}"
group: "{{ rsg_user }}"
- name: Start Rsync Server
systemd:
name: rsyncd
state: started
enabled: yes
变量配置:
[root@ansible roles]# cat rsync/vars/main.yml
rs_user: www
rsg_user: www
pass: 123456
rsync_dir: /backup
handlers配置:
[root@ansible roles]# cat rsync/handlers/main.yml
- name: Restart Rsyncd Server
systemd:
name: rsyncd
state: restarted
[root@ansible roles]# ll rsync/templates/
total 8
-rw-r--r-- 1 root root 376 Apr 20 11:45 rsyncd.conf.j2
-rw-r--r-- 1 root root 24 Apr 20 11:50 rsync.passwd.j2
配置文件:
[root@ansible roles]# cat rsync/templates/rsyncd.conf.j2
uid = {{ rs_user }}
gid = {{ rsg_user }}
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 = welcome to baimeidashu backup!
path = /backup
[nfs]
path = /data
[root@ansible roles]# cat rsync/templates/rsync.passwd.j2
rsync_backup:{{ pass }}
3.调用role角色
/etc/ansible/roles
[root@ansible roles]# vim site.yml
- hosts: all
roles:
- role: rsync
when: ansible_hostname is match "backup"
运行:
ansible-playbook /etc/ansible/roles/site.yml
欢迎来撩 : 汇总all