Answers:
您可能需要注册远程内容,然后遍历它,类似这样的方法应该起作用:
- 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 }}"
这里/remote
应该目录路径您的远程服务器上,并改变/local/
对你的主目录
您应该使用同步模块执行此操作。这使用了rsync的强大功能。它将复制任何深度的文件和目录结构,防弹高效,仅复制已更改的实际字节:
- name: Fetch stuff from the remote and save to local
synchronize: src={{ item }} dest=/tmp/ mode=pull
with_items:
- "folder/one"
- "folder/two"
关键是mode
参数:
指定同步的方向。在推模式下,本地主机或委托是源;在拉模式下,上下文中的远程主机是源。
synchronise
模块比ansible复制文件的其他方法更加可靠和可扩展。
好吧,如果您使用的是最新的ansible版本(例如2.2.1.0),我认为我们需要对此商品加引号
- name: use find to get the files list which you want to copy/fetch
find:
paths: /etc/
patterns: ".*passwd$"
use_regex: True
register: file_2_fetch
- name: use fetch to get the files
fetch:
src: "{{ item.path }}"
dest: /tmp/
flat: yes
with_items: "{{ file_2_fetch.files }}"
- hosts: srv-test
tasks:
- find: paths="/var/tmp/collect" recurse=no patterns="*.tar"
register: file_to_copy
- fetch: src={{ item }} dest=/tmp
with_items: files_to_copy.stdout_lines
我用这个:1.将目录从远程主机拉到特定主机
- name: Gather hosts stats from other hosts
shell: " scp -r {{results_root_dir_src}} root@{{groups['profiling_server'][0]}}:{{results_root_dir_dest}}/abc/"
when: "'profiling_server' not in group_names"
#It will not run on the node where the directories need to be copied.
- name: Gather from host to local
delegate_to: 127.0.0.1
run_once: true
become: false
shell: "scp -r root@{{groups['profiling_server'][0]}}:{{results_root_dir}} ./results_local_location "
库存
[nodes]
server1
server2
server3
[profiling_server]
server1