ansible之synchronize模块

例:将主控方/root/test.yml 目录推送到指定节点的/tmp 目录下

[root@master ~]# ansible all -m synchronize -a 'src=/tmp/test dest=/data/soft/ compress=yes'

root@master.test.com's password: client02 | 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' \"/tmp/test\" \"client02:/data/soft/\"", 
    "msg": "<f+++++++++ test\n", 
    "rc": 0, 
    "stdout_lines": [
        "<f+++++++++ test"
    ]
}
client01 | 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' \"/tmp/test\" \"client01:/data/soft/\"", 
    "msg": "<f+++++++++ test\n", 
    "rc": 0, 
    "stdout_lines": [
        "<f+++++++++ test"
    ]
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

 root@master.test.com’s password: 111111此处输入密码原因是master自身没有做ssh免秘钥处理
master.test.com | 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’ “/tmp/test” “master.test.com:/data/soft/””,
“msg”: “<f+++++++++ test\n”,
“rc”: 0,
“stdout_lines”: [
“<f+++++++++ test”
]
}

[root@client02 ~]# ll /data/soft/test
-rw-r–r-- 1 root root 4 Jul 30 2016 /data/soft/test
}
[root@master tmp]# ll /data/soft/test
-rw-r–r-- 1 root root 4 Jul 30 23:31 /data/soft/test

执行效果:
delete=yes 使两边的内容一样(即以推送方为主)
compress=yes 开启压缩,默认为开启
–exclude=.git 忽略同步.git 结尾的文件
由于模块,默认都是推送push。因此,如果你在使用拉取pull 功能的时候,可以参考如下来实现mode=pull 更改推送模式为拉取模式

例:将client01 节点的/root/stu01 目录拉取到主控节点的/data/soft 目录下

[root@master tmp]# ansible client01 -m synchronize -a 'mode=pull  src=/root/stu01 dest=/data/soft/ compress=yes'
client01 | 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' \"client01:/root/stu01\" \"/data/soft/\"", 
    "msg": ">f+++++++++ stu01\n", 
    "rc": 0, 
    "stdout_lines": [
        ">f+++++++++ stu01"
    ]
}
[root@master tmp]# cat /data/soft/stu01 
213
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

参数解释
dest_port=22 指定目的主机的端口,ansible 配置文件中的
ansible_ssh_port 变量优先级高于该dest_port
变量
rsync_path 指定rsync 命令来在远程服务器上运行。这个参
考rsync 命令的–rsync-path 参数,
–rsync-path=PATH 指定远程服务器上的rsync 命令所在路径信息
rsync_timeout 指定rsync 操作的IP 超时时间,和rsync 命令
的–timeout 参数效果一样

posted @   勤奋的蓝猫  阅读(12)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
点击右上角即可分享
微信分享提示