使用安赛尔复制多个文件

我怎样才能复制多个单一的文件到远程节点的安赛尔在一个任务?

我尝试复制任务中的复制模块行来定义文件,但它只复制第一个文件。

216170 次浏览

您可以使用 with_fileglob循环:

- copy:
src: "\{\{ item }}"
dest: /etc/fooapp/
owner: root
mode: 600
with_fileglob:
- /playbooks/files/fooapp/*

如果需要多个位置,则需要多个任务。一个复制任务只能从一个位置(包括多个文件)复制到节点上的另一个位置。

- 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/*

为此,可以使用 with _ together:

- name: Copy multiple files to multiple directories
copy: src=\{\{ item.0 }} dest=\{\{ item.1 }}
with_together:
- [ 'file1', 'file2', 'file3' ]
- [ '/dir1/', '/dir2/', '/dir3/' ]
- 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 }}"
- 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

或者可以使用 with _ item:

- copy:
src: "\{\{ item }}"
dest: /etc/fooapp/
owner: root
mode: 600
with_items:
- dest_dir
- 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

copy模块是一个复制多个文件和/或目录结构的错误工具,使用 synchronize模块代替使用 rsync作为后端。请注意,它需要在控制器和目标主机上都安装 rsync。它真的很强大,检查 可靠的文件

示例-将控制器的 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

由于可视化2.5,with_*构造 不推荐,并且 loop语法应该被使用。一个简单的实际例子:

- name: Copy CA files
copy:
src: '\{\{item}}'
dest: '/etc/pki/ca-trust/source/anchors'
owner: root
group: root
mode: 0644
loop:
- symantec-private.crt
- verisignclass3g2.crt


您可以使用目录列表循环访问变量:

- name: Copy files from several directories
copy:
src: "\{\{ item }}"
dest: "/etc/fooapp/"
owner: root
mode: "0600"
loop: "\{\{ files }}"
vars:
files:
- "dir1/"
- "dir2/"

使用以下源代码在客户端计算机上复制多个文件。


 - 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

注:

如果使用变量传递多个路径,则

“/root/\{\{ item }}”

如果使用变量为不同的项传递 path,则

Src: “/root/{ item.source _ path }}”

下面是一个复制文件的通用解决方案:

   ...
- name: Find files you want to move
ansible.builtin.find:
paths: /path/to/files/
file_type: file
excludes: "*.txt" # Whatever pattern you want to exclude
register: files_output


- name: Copy the files
ansible.builtin.copy:
src: "\{\{ item.path }}"
dest: /destination/directory/
loop: "\{\{ files_output.files }}"
...

这比使用 with_fileglob更强大,因为你可以使用正则表达式进行匹配:

$ ls /path/to/files
demo.yaml  test.sh  ignore.txt


$ ls /destination/directory
file.h


$ ansible-playbook playbook.yaml
...[some output]...


$ ls /destination/directory
file.h demo.yaml test.sh

从上面的示例中可以看到,由于剧本中的 excludes正则表达式,ignore.txt没有复制到目标目录。像这样忽略文件是不可能的,因为简单地使用 with_fileglob是不可能的。

此外,您可以相对容易地从多个目录移动文件:

   ...
- name: Find files you want to move
ansible.builtin.find:
paths: /path/to/files/
# ... the rest of the task
register: list1


- name: Find more files you want to move
ansible.builtin.find:
paths: /different/path/
# ... the rest of the task
register: list2


- name: Copy the files
ansible.builtin.copy:
src: "\{\{ item.path }}"
dest: /destination/directory/
loop: "\{\{ list1.files + list2.files }}"
...

下面是一个用于在远程主机上复制多个文件的示例“可视脚本”

- name: Copy Multiple Files on remote Hosts
ansible.windows.win_copy:
src: "\{\{ srcPath }}/\{\{ item }}" # Remeber to us \{\{item}}
# as a postfix to source path


dest: "\{\{ destPath }}"
remote_src: yes # if source path is available on remote Host
with_items:
- abc.txt
- abc.properties

使用安赛尔将文件从多个目录复制到多个目录

我发现 Guenhter的答案很有帮助,但是需要改变远程文件的模式。我没有足够的声誉把这个作为一个评论,这将是一个更合适的地方。在这个示例中,我将两个文件从两个目录复制到/tmp 和/tmp/bin,我首先创建这两个目录并修改远程文件模式。

- name: cpldupd
hosts: test
remote_user: root
become: true
vars:
- rpth: /tmp
tasks:
- name: Create '\{\{rpth}}/bin'
file:
path: '\{\{rpth}}/bin'
state: directory


- name: Transfer
copy: src=\{\{ item.src }} dest=\{\{ item.dest }} mode=0775
with_items:
- { src: '../utils/cpldupd', dest: '\{\{rpth}}/cpldupd' }
- { src: '../utils/bin/cpldupd', dest: '\{\{rpth}}/bin/cpldupd' }

  • 主持人: 测试 聚集事实: 错误 成为: 真实的 变量: 路径: “/home/ansibm/playbooks” Remote _ path: “/home/{ anable _ ssh _ user }” Dir: ‘ yml _ files’ 任务:
    • 名称: “为备份文件创建目录” 文件: Path: “\{\{ remote _ path }}/\{\{ dir }}” 目录 所有者: “{ anable _ ssh _ user }” 组: “{ anable _ ssh _ user }” 模式: 0700
    • 名称: “拷贝 yml 文件” 副本: “\{\{ item }” Est: “\{\{ remote _ path }}/\{\{ dir }}” 所有者: “{ anable _ ssh _ user }” 组: “{ anable _ ssh _ user }” 模式: 0644 循环: - “\{\{ path }/ab.html” - “\{\{ path }}/cp.yml”