十一、Ansible中的同步模块(rsync)
Synchronize模块
1
2
3
4
5
6
7
|
This is a wrapper around rsync. Of course you could just use the
command action to call rsync yourself, but you also have to add a
fair number of boilerplate options and host facts. You still may
need to call rsync directly via `command' or `shell' depending on
your use case. The synchronize action is meant to do common things
with `rsync' easily. It does not provide access to the full power of
rsync, but does make most invocations easier to follow.
|
这是一个关于rsync的模块。
参数 | 必填 | 默认值 | 选项 | 说明 |
archive | no | yes | yes no | 显示rsync的存档标志所属信息 |
checksum | no | no | yes no | 校验 |
compress | no | yes | yes no | 在传送过程中压缩文件数据 |
dest | yes | 目标路径,路径可以绝对或相对 | ||
delete | no | no | yes no | 删除srv中不存在的文件 |
copy_links | no | no | yes no | 复制符号链接,是映射的文件复制 |
dest_port | no | 22 | yes no | 目标主机端口号(ansible_ssh_port优先于此值) |
group | no | 存档选项值 | yes no | 保留文件的所属组 |
owner | no | 存档选项值 | yes no | 保留文件的所属者(只有超级用户可以操作) |
perms | no | 存档选项值 | yes no | 保留文件的权限 |
rsync_opts | no | no | 通过此选项指定其他的rsync的选项 | |
rsync_path | no | 指定rsync命令到远程主机上运行 | ||
rsync_timeout | no | 0 | 指定rsync的timeout时间以秒为单位 | |
src | yes | 源地址(路径可以是绝对或相对的) | ||
times | no | 存档选项值 | yes no | 保留文件的修改时间 |
exclude-from | no | no | yes no | 用于定义排除多个文件夹和文件 |
exclude | no | no | yes no | 用于定义排除单独的文件夹和文件 |
实例展示:
1
2
3
4
5
6
|
---
- hosts: web
remote_user: deploy
tasks:
- name: webserver update web server (--exclude-file)
synchronize: src=/home/deploy/webserver/ dest=/data/web/webserver/ rsync_opts=--delete,--exclude-from=/data/ansible/public/excludefile
|
实际使用:比用copy模块速度增加很多
cat stbverify_update.yml
---
- name: handlers adserver
hosts: adservers
user: root
tasks:
- name: copy file
synchronize: src=/etc/ansible/STBVerify dest=/root/ rsync_opts=--delete
notify: stbverify handlers
handlers:
- name: stbverify handlers
shell: /bin/bash /data/sh/stbverify_update.sh