案例4: 远程批量安装rsync服务,并设置管理端修改配置文件变动时候执行playbook时触发重启服务
管理端:192.168.171.128
[root@localhost ~]# ls
conf rsync_install.yaml web_mount.yaml
[root@localhost ~]# ls conf/
rsyncd.conf
[root@localhost ~]# cat conf/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 = rsync_backup
secrets file = /etc/rsyncd.password
log file = /var/log/rsyncd.log
[data]
path=/data
[root@localhost ~]# cat rsync_install.yaml
#test: 为/etc/ansible/hosts中的主机列表 #task: 执行的任务
#name: 描述信息 #yum: yum模块,安装服务的
#copy: copy模块,远程传递文件的 #file: file模块,远程创建目录的
#service: service模块,远程管理服务的
- hosts: test
tasks:
#安装rsync服务
- name: Install Rsync Server
yum: name=rsync state=installed
#配置rsync服务,cp自定义的配置文件,且设置当该配置文件变更需要触发重启操作
- name: configure rsync server
copy: src=./conf/rsyncd.conf dest=/etc/rsyncd.conf
notify: Restart Rsync Server
#创建rsync虚拟用户和密码文件,用户名:rsync_backup,密码:1
- name: create Virt User
copy: content='rsync_backup:1' dest=/etc/rsyncd.password mode=600
#远程创建用户组和用户
- name: create yonghu zu www
group: name=www gid=666
#远程创建用户, create_home=no:不创建家目录 指定shell不能登录
- name: create yonghu www
user: name=www uid=666 group=www create_home=no shell=/sbin/nologin
#远程创建目录/data作为共享目录
- name: create data mulu
file: path=/data state=directory recurse=yes owner=www group=www mode=755
#远程启动rsync服务
- name: start rsyncserver
service: name=rsyncd state=started enabled=yes
#下面handler是接收notify的触发,执行重启rsync服务
handlers:
- name: Restart Rsync Server
service: name=rsyncd state=restarted
[root@localhost ~]# ansible-playbook rsync_install.yaml #执行远程安装
[root@localhost ~]# yum -y install rsync
[root@localhost ~]# echo 1 > /etc/rsync.pass
[root@localhost ~]# chmod -R 600 /etc/rsync.pass
[root@localhost ~]# echo 111 > a.txt
[root@localhost ~]# rsync -av a.txt rsync_backup@192.168.171.129::data --password-file=/etc/rsync.pass
[root@localhost ~]# rsync -av a.txt rsync_backup@192.168.171.130::data --password-file=/etc/rsync.pass
所有被管理端:192.168.171.129和192.168.171.130
[root@localhost ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2019-10-04 17:16:39 CST; 4min 18s ago
[root@localhost ~]# netstat -anput |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 23117/rsync
[root@localhost ~]# ls /data/
a.txt
[root@localhost ~]# cat /data/a.txt
111