maa*_*yke 24 linux copy ansible
我想使用 Ansible 将文件从远程目录复制到本地目录,但 fetch 模块只允许我复制一个文件。我有许多服务器需要文件(每个服务器的目录相同),但我现在不知道如何使用 Ansible 执行此操作。
有任何想法吗?
Dun*_*ock 38
您应该使用同步模块来执行此操作。这使用了rsync的强大功能。它将复制任何深度的文件和目录结构,防弹且高效 - 仅复制已更改的实际字节:
- name: Fetch stuff from the remote and save to local
synchronize: src={{ item }} dest=/tmp/ mode=pull
with_items:
- "folder/one"
- "folder/two"
Run Code Online (Sandbox Code Playgroud)
关键是mode参数:
指定同步的方向。在推送模式下,本地主机或委托是源;在拉模式下,上下文中的远程主机是源。
Kęs*_*tis 28
您可能需要注册远程内容而不是循环遍历它,这样的事情应该可以工作:
- shell: (cd /remote; find . -maxdepth 1 -type f) | cut -d'/' -f2
register: files_to_copy
- fetch: src=/remote/{{ item }} dest=/local/
with_items: "{{ files_to_copy.stdout_lines }}"
Run Code Online (Sandbox Code Playgroud)
/remote应该使用远程服务器上的目录路径和/local/主服务器上的目录更改何处
小智 5
我没有足够的代表发表评论,否则我会添加它。
我使用了 K?stutis 发布的内容。我不得不稍作修改
- shell: (cd /remote; find . -maxdepth 1 -type f) | cut -d'/' -f2
register: files_to_copy
- fetch: src=/remote/{{ item }} dest=/local/
with_items: "{{ files_to_copy.stdout_lines }}"
Run Code Online (Sandbox Code Playgroud)
with_items 是我必须改变的区域。否则无法找到文件。
| 归档时间: |
|
| 查看次数: |
79384 次 |
| 最近记录: |