ansible synchronize作用跟rsync很相似,具体参数如下:
参数内容,均来自官网,如果有出入,以官网为主
参数 | 是否必须 | 默认 | 选项 | 说明 |
archive | no | yes |
yes no |
rsync镜像递归标志,启用递归,链接,权限,时间,属主,属组标志和 -D |
checksum | no | no |
yes no |
跳过基本的校验 |
compress | no | yes |
yes no |
在传输过程中压缩文件数据。在大多数情况下,请保持启用,除非它导致问题。 |
copy_links | no | no |
yes no |
将符号链接复制为它们所指向的项(引用对象),而不是符号链接。 |
delete | no | no |
yes no |
删除src路径中不存在的dest中的文件(传输后,不在之前)。此选项需要recursive = yes。 |
dest | yes |
要同步源的目标主机上的路径;该路径可以是绝对路径或相对路径。 |
||
dest_port | no | ansible_ssh_port的值设定,remote_port配置设置,如果ssh客户端没有设置的话 |
目标主机上ssh的端口号。在ansible 2.0之前,ansible_ssh_port库存变量优先于此值。 |
|
dirs | no | no |
yes no |
传输目录而不进行递归 |
existing_only | no | no |
yes no |
跳过在接收器上创建一个新文件。 |
group | no | 递归选项的值 |
yes no |
保留组 |
links | no | 递归选项的值 |
yes no |
将符号链接复制为符号链接。 |
mode | no | push |
yes no |
指定同步的方向。在推模式下,localhost或delegate是源;在拉模式下,远程主机在上下文中是源。 |
owner | no | 递归选项的值 |
yes no |
保留属主 |
partial | no | 告诉rsync保留一些文件,这应该使以后的文件传输的其余部分。 | ||
perms | no | 递归选项的值 |
yes no |
保留权限 |
recursive | no | 递归选项的值 |
yes no |
递归到目录。 |
rsync_opts | no | 通过传入数组来指定其他rsync选项。 | ||
rsync_path | no | 指定要在远程主机上运行的rsync命令。请参见rsync手册页上的--rsync-path。 | ||
rsync_timeout | no | 以秒为单位指定rsync命令的--timeout。 | ||
set_remote_user | no | True | put user @为远程路径。如果您有自定义ssh配置为与库存用户不匹配的主机定义远程用户,则应将此参数设置为“no”。 | |
src | yes | 将同步到目标的源主机上的路径;路径可以是绝对路径或相对路径。 | ||
times | no | 递归选项的值 |
yes no |
保存修改时间 |
use_ssh_args | no | no |
yes no |
使用ansible.cfg中指定的ssh_args |
verify_host | no | 验证目标主机密钥。 |
1.同步目录下文件:
源文件:
# ll total 4 lrwxrwxrwx 1 root root 8 Dec 28 17:39 fk -> /data/fm -rw-r--r-- 1 root root 4096 May 31 2016 p.txt
同步目录下文件,而不是目录:
# ansible webserver -m synchronize -a "src=/root/dog/ dest=/tmp/" 172.16.110.48 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh ‘ssh -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/dog/\" \"172.16.110.48:/tmp/\"", "msg": ".d..t...... ./\n", "rc": 0, "stdout_lines": [ ".d..t...... ./" ] } 172.16.110.47 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh ‘ssh -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/dog/\" \"172.16.110.47:/tmp/\"", "msg": ".d..t...... ./\n", "rc": 0, "stdout_lines": [ ".d..t...... ./" ] }
同步了所有文件
# ll total 8 drwxr-xr-x 2 root root 29 Dec 28 17:39 dog lrwxrwxrwx 1 root root 8 Dec 28 17:39 fk -> /data/fm //无效链接,源不存在
2.同步整个目录:
# ansible webserver -m synchronize -a "src=/root/dog dest=/tmp/" 172.16.110.48 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh ‘ssh -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/dog\" \"172.16.110.48:/tmp/\"", "msg": "cd+++++++++ dog/\ncL+++++++++ dog/fk -> /data/fm\n<f+++++++++ dog/p.txt\n", "rc": 0, "stdout_lines": [ "cd+++++++++ dog/", "cL+++++++++ dog/fk -> /data/fm", "<f+++++++++ dog/p.txt" ] } 172.16.110.47 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh ‘ssh -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/dog\" \"172.16.110.47:/tmp/\"", "msg": "cd+++++++++ dog/\ncL+++++++++ dog/fk -> /data/fm\n<f+++++++++ dog/p.txt\n", "rc": 0, "stdout_lines": [ "cd+++++++++ dog/", "cL+++++++++ dog/fk -> /data/fm", "<f+++++++++ dog/p.txt" ] }
同步了整个目录
# ll total 8 drwxr-xr-x 2 root root 29 Dec 28 17:39 dog
3.同步时候保持属主,属组,权限,链接,同时传输的时候使用压缩
同步前文件详情:
# ll dog/ total 8 -rw-r--r-- 1 ckl ckl 9 Dec 29 13:05 em_f lrwxrwxrwx 1 root root 8 Dec 28 17:39 fk -> /data/fm -rwx------ 1 root root 4096 May 31 2016 p.txt
同步:
# ansible webserver -m synchronize -a "compress=yes group=yes links=yes src=/root/dog/ dest=/tmp/sync_dir" 172.16.110.47 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh ‘ssh -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/dog/\" \"172.16.110.47:/tmp/sync_dir\"", "msg": ".d..t...... ./\n<f+++++++++ em_f\ncL+++++++++ fk -> /data/fm\n<f+++++++++ p.txt\n", "rc": 0, "stdout_lines": [ ".d..t...... ./", "<f+++++++++ em_f", "cL+++++++++ fk -> /data/fm", "<f+++++++++ p.txt" ] } 172.16.110.48 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh ‘ssh -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/dog/\" \"172.16.110.48:/tmp/sync_dir\"", "msg": ".d..t...... ./\n<f+++++++++ em_f\ncL+++++++++ fk -> /data/fm\n<f+++++++++ p.txt\n", "rc": 0, "stdout_lines": [ ".d..t...... ./", "<f+++++++++ em_f", "cL+++++++++ fk -> /data/fm", "<f+++++++++ p.txt" ] }
结果:(权限及属性保持不变)
# ll total 8 -rw-r--r-- 1 ckl ckl 9 Dec 29 13:05 em_f lrwxrwxrwx 1 root root 8 Dec 28 17:39 fk -> /data/fm //无效链接,源不存在 -rwx------ 1 root root 4096 May 31 2016 p.txt
4.删除目标上的文件:
删除前文件:
# ll total 8 -rw-r--r-- 1 ckl ckl 9 Dec 29 13:05 em_f lrwxrwxrwx 1 root root 8 Dec 28 17:39 fk -> /data/fm -rwx------ 1 root root 4096 May 31 2016 p.txt
删除:
# ansible webserver -m synchronize -a "src=/root/dele/ dest=/tmp/sync_dir delete=yes" 172.16.110.47 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh ‘ssh -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/dele/\" \"172.16.110.47:/tmp/sync_dir\"", "msg": "*deleting dele/\n*deleting p.txt\n*deleting fk\n*deleting em_f\n", "rc": 0, "stdout_lines": [ "*deleting dele/", "*deleting p.txt", "*deleting fk", "*deleting em_f" ] } 172.16.110.48 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --delete-after --archive --rsh ‘ssh -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/dele/\" \"172.16.110.48:/tmp/sync_dir\"", "msg": "*deleting dele/\n*deleting p.txt\n*deleting fk\n*deleting em_f\n", "rc": 0, "stdout_lines": [ "*deleting dele/", "*deleting p.txt", "*deleting fk", "*deleting em_f" ] }
查看目标文件:
# ll /tmp/sync_dir/ total 0
5.同步链接时,同步源文件
# ll total 8 -rw-r--r-- 1 ckl ckl 9 Dec 29 13:05 em_f lrwxrwxrwx 1 root root 8 Dec 28 17:39 fk -> /data/fm -rwx------ 1 root root 4096 May 31 2016 p.txt drwxr-xr-x 2 root root 20 Dec 29 13:15 wu [[email protected] dog]# ll * -rw-r--r-- 1 ckl ckl 9 Dec 29 13:05 em_f lrwxrwxrwx 1 root root 8 Dec 28 17:39 fk -> /data/fm -rwx------ 1 root root 4096 May 31 2016 p.txt wu: total 4 -rw-r--r-- 1 root root 14 Dec 29 13:15 kk.txt
同步:
# ansible webserver -m synchronize -a "compress=yes group=yes copy_links=yes dirs=yes src=/root/dog/ dest=/tmp/sync_dir" 172.16.110.48 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --copy-links --dirs --rsh ‘ssh -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/dog/\" \"172.16.110.48:/tmp/sync_dir\"", "msg": ".d..t...... ./\n<f+++++++++ em_f\n<f+++++++++ fk\n<f+++++++++ p.txt\ncd+++++++++ wu/\n<f+++++++++ wu/kk.txt\n", "rc": 0, "stdout_lines": [ ".d..t...... ./", "<f+++++++++ em_f", "<f+++++++++ fk", "<f+++++++++ p.txt", "cd+++++++++ wu/", "<f+++++++++ wu/kk.txt" ] } 172.16.110.47 | SUCCESS => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --copy-links --dirs --rsh ‘ssh -S none -o StrictHostKeyChecking=no‘ --out-format=‘<<CHANGED>>%i %n%L‘ \"/root/dog/\" \"172.16.110.47:/tmp/sync_dir\"", "msg": ".d..t...... ./\n<f+++++++++ em_f\n<f+++++++++ fk\n<f+++++++++ p.txt\ncd+++++++++ wu/\n<f+++++++++ wu/kk.txt\n", "rc": 0, "stdout_lines": [ ".d..t...... ./", "<f+++++++++ em_f", "<f+++++++++ fk", "<f+++++++++ p.txt", "cd+++++++++ wu/", "<f+++++++++ wu/kk.txt" ] }
查看结果:
# ll total 12 -rw-r--r-- 1 ckl ckl 9 Dec 29 13:05 em_f -rw-r--r-- 1 root root 7 Dec 28 16:49 fk //不再是链接 -rwx------ 1 root root 4096 May 31 2016 p.txt drwxr-xr-x 2 root root 20 Dec 29 13:15 wu
时间: 2024-11-03 21:30:22