Answers:
您可以with_fileglob
为此使用循环:
- copy:
src: "{{ item }}"
dest: /etc/fooapp/
owner: root
mode: 600
with_fileglob:
- /playbooks/files/fooapp/*
/roles/db/files
但无法通过这种方法使用它。我已经尝试过了,with_fileglob: - /roles/db/file/*
但是不会走好路
- name: Your copy task
copy: src={{ item.src }} dest={{ item.dest }}
with_items:
- { src: 'containerizers', dest: '/etc/mesos/containerizers' }
- { src: 'another_file', dest: '/etc/somewhere' }
- { src: 'dynamic', dest: '{{ var_path }}' }
# more files here
dest
集合作为变量?{ src: 'containerizers', dest: {{ containerizers }} }
。
{ src: '{{ source.var }}', dest: '{{ dest.var }}' }
您可以将with_together用于此目的:
- name: Copy multiple files to multiple directories
copy: src={{ item.0 }} dest={{ item.1 }}
with_together:
- [ 'file1', 'file2', 'file3' ]
- [ '/dir1/', '/dir2/', '/dir3/' ]
如果需要多个位置,则需要多个任务。一个复制任务只能从一个位置(包括多个文件)复制到节点上的另一个位置。
- copy: src=/file1 dest=/destination/file1
- copy: src=/file2 dest=/destination/file2
# copy each file over that matches the given pattern
- copy: src={{ item }} dest=/destination/
with_fileglob:
- /files/*
- hosts: lnx
tasks:
- find: paths="/appl/scripts/inq" recurse=yes patterns="inq.Linux*"
register: file_to_copy
- copy: src={{ item.path }} dest=/usr/local/sbin/
owner: root
mode: 0775
with_items: "{{ files_to_copy.files }}"
find
模块仅适用于ansible 2.x,而不适用于ansible 1.x
stdout_lines
在返回值中提到了,但不适用于find
模块。它只有files
,examined
并matched
作为返回值。希望对别人有帮助
find
似乎只看远程系统,不允许从管理节点获取任何内容。这些答案,使用with_fileglob
,似乎更加恰当:stackoverflow.com/a/42290160/272387,stackoverflow.com/a/36720342/272387。
- name: find inq.Linux*
find: paths="/appl/scripts/inq" recurse=yes patterns="inq.Linux*"
register: find_files
- name: set fact
set_fact:
all_files:
- "{{ find_files.files | map(attribute='path') | list }}"
when: find_files > 0
- name: copy files
copy:
src: "{{ item }}"
dest: /destination/
with_items: "{{ all_files }}"
when: find_files > 0
使用以下源代码在客户端计算机上复制多个文件。
- name: Copy data to the client machine
hosts: hostname
become_method: sudo
become_user: root
become: true
tasks:
# Copy twice as sometimes files get skipped (mostly only one file skipped from a folder if the folder does not exist)
- name: Copy UFO-Server
copy:
src: "source files path"
dest: "destination file path"
owner: root
group: root
mode: 0644
backup: yes
ignore_errors: true
注意:
如果要通过使用变量传递多个路径,则
src:“ / root / {{item}}”
如果通过对不同项目使用变量来传递路径,则
src:“ / root / {{item.source_path}}”
copy
module是复制许多文件和/或目录结构的错误工具,请改用synchronize
module rsync
作为后端。注意,它需要同时rsync
安装在控制器和目标主机上。它非常强大,请查看Ansible文档。
示例-将文件从build
控制器的目录(带有子目录)复制到/var/www/html
目标主机上的目录:
synchronize:
src: ./my-static-web-page/build/
dest: /var/www/html
rsync_opts:
- "--chmod=D2755,F644" # copy from windows - force permissions