ansile(2)模块之synchronize
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,ansible synchronize作用跟rsync很相似,具体参数如下:参数内容,均来自官网,如果有出入,以官网为主参数是否必须默认选项说明archivenoyesyesnorsync镜像递归标志
千家信息网最后更新 2026年11月03日ansile(2)模块之synchronize
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.同步目录下文件:
源文件:
# lltotal 4lrwxrwxrwx 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='<>%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='<>%i %n%L' \"/root/dog/\" \"172.16.110.47:/tmp/\"", "msg": ".d..t...... ./\n", "rc": 0, "stdout_lines": [ ".d..t...... ./" ]} 同步了所有文件
# lltotal 8drwxr-xr-x 2 root root 29 Dec 28 17:39 doglrwxrwxrwx 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='<>%i %n%L' \"/root/dog\" \"172.16.110.48:/tmp/\"", "msg": "cd+++++++++ dog/\ncL+++++++++ dog/fk -> /data/fm\n /data/fm", " { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dog\" \"172.16.110.47:/tmp/\"", "msg": "cd+++++++++ dog/\ncL+++++++++ dog/fk -> /data/fm\n /data/fm", "同步了整个目录
# lltotal 8drwxr-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_flrwxrwxrwx 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='<>%i %n%L' \"/root/dog/\" \"172.16.110.47:/tmp/sync_dir\"", "msg": ".d..t...... ./\n /data/fm\n /data/fm", " {"changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dog/\" \"172.16.110.48:/tmp/sync_dir\"", "msg": ".d..t...... ./\n /data/fm\n /data/fm", "结果:(权限及属性保持不变)
# lltotal 8-rw-r--r-- 1 ckl ckl 9 Dec 29 13:05 em_flrwxrwxrwx 1 root root 8 Dec 28 17:39 fk -> /data/fm //无效链接,源不存在-rwx------ 1 root root 4096 May 31 2016 p.txt
4.删除目标上的文件:
删除前文件:
# lltotal 8-rw-r--r-- 1 ckl ckl 9 Dec 29 13:05 em_flrwxrwxrwx 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='<>%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='<>%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.同步链接时,同步源文件
# lltotal 8-rw-r--r-- 1 ckl ckl 9 Dec 29 13:05 em_flrwxrwxrwx 1 root root 8 Dec 28 17:39 fk -> /data/fm-rwx------ 1 root root 4096 May 31 2016 p.txtdrwxr-xr-x 2 root root 20 Dec 29 13:15 wu[root@localhost dog]# ll *-rw-r--r-- 1 ckl ckl 9 Dec 29 13:05 em_flrwxrwxrwx 1 root root 8 Dec 28 17:39 fk -> /data/fm-rwx------ 1 root root 4096 May 31 2016 p.txtwu: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='<>%i %n%L' \"/root/dog/\" \"172.16.110.48:/tmp/sync_dir\"", "msg": ".d..t...... ./\n { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --copy-links --dirs --rsh 'ssh -S none -o StrictHostKeyChecking=no' --out-format='<>%i %n%L' \"/root/dog/\" \"172.16.110.47:/tmp/sync_dir\"", "msg": ".d..t...... ./\n查看结果:
# lltotal 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.txtdrwxr-xr-x 2 root root 20 Dec 29 13:15 wu
同步
文件
链接
递归
路径
主机
目录
目标
传输
参数
权限
符号
命令
库存
时候
时间
标志
模式
源文件
用户
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技股
互联网科技战争
金融科技 互联网金融
互联网的科技创新
互联网科技logo
互联网金融 科技创新
中正互联网科技招聘
移动互联网科技公司
崇明区互联网教育科技售后服务
互联网媒体平台算高科技产业
深圳育鹏互联网科技有限公司招聘
互联网科技研发水平
企晟互联网信息科技有限公司
西安小鹰速贷互联网科技有限公司
高科技互联网发展前景如何
互联网公司高科技产品
互联网高科技行业有哪些企业
现代互联网科技公司
互联网金融金融科技
颐高云石互联网科技
华服互联网科技怎么样
为想互联网科技怎么样
九亚互联网科技
苏州互联网科技贷款公司
天津摩尔互联网科技
互联网科技素材视频竖屏
厦门千工互联网科技有限公司
数字互联网科技有限公司
互联网科技品牌
武汉亿视互联网科技有限公司