gpt4 book ai didi

scp - 为什么使用 Ansible 复制目录这么慢?

转载 作者:行者123 更新时间:2023-12-01 21:50:26 26 4
gpt4 key购买 nike

我正在使用 Ansible 将目录(900 个文件,136MBytes)从一台主机复制到另一台主机:

---
- name: copy a directory
copy: src={{some_directory}} dest={{remote_directory}}

这个操作花费了令人难以置信的 17 分钟,而一个简单的 scp -r <src> <dest>只需 7 秒。

我已经尝试了加速模式,根据ansible docs ,但无济于事。

can be anywhere from 2-6x faster than SSH with ControlPersist enabled, and 10x faster than paramiko.

最佳答案

TLDR:使用同步而不是复制

这是我正在使用的复制命令:

- copy: src=testdata dest=/tmp/testdata/

作为猜测,我认为同步操作很慢。 files module documentation也暗示着这一点:

The "copy" module recursively copy facility does not scale to lots (>hundreds) of files. For alternative, see synchronize module, which is a wrapper around rsync.

深入研究源代码显示 each file is processed with SHA1 。那是implemented using hashlib.sha1 。本地测试意味着 900 个文件只需 10 秒(恰好占用 400mb 空间)。

那么,下一个途径。副本由 module_utils/basic.py's atomic_move method 处理。我不确定加速模式是否有帮助(它是 mostly-deprecated feature ),但我尝试过 pipelining ,将其放入本地 ansible.cfg 中:

[ssh_connection]
pipelining=True

似乎没有帮助;我的样本运行了 24 分钟。显然有一个循环检查文件、上传文件、修复权限,然后开始处理下一个文件。即使 ssh 连接保持打开状态,也会有很多命令。从字里行间看,它有点有意义——我认为“文件传输”不能在管道中完成。

因此,按照提示使用synchronize命令:

- synchronize: src=testdata dest=/tmp/testdata/

即使使用 pipeline=False,也花费了 18 秒。显然,synchronize 命令是这种情况下的最佳选择。

请记住,synchronize 使用 rsync,它默认为修改时间和文件大小。如果您想要或需要校验和,请将 checksum=True 添加到命令中。即使启用了校验和,时间也没有真正改变——仍然是 15-18 秒。我通过使用 -vvvv 运行 ansible-playbook 验证了校验和选项已打开,可以在此处看到:

ok: [testhost] => {"changed": false, "cmd": "rsync --delay-updates -FF --compress --checksum --archive --rsh 'ssh  -o StrictHostKeyChecking=no' --out-format='<<CHANGED>>%i %n%L' \"testdata\" \"user@testhost:/tmp/testdata/\"", "msg": "", "rc": 0, "stdout_lines": []}

关于scp - 为什么使用 Ansible 复制目录这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27985334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com