使用Ansible复制多个文件

时间:2016-04-18 14:32:46

标签: ansible

如何在任务中通过Ansible将多个文件复制到远程节点?

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

11 个答案:

答案 0 :(得分:101)

您可以使用with_fileglob循环:

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

答案 1 :(得分:84)

- 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

答案 2 :(得分:13)

您可以将with_together用于此目的:

- name: Copy multiple files to multiple directories
  copy: src={{ item.0 }} dest={{ item.1 }}
  with_together:
    - [ 'file1', 'file2', 'file3' ]
    - [ '/dir1/', '/dir2/', '/dir3/' ]

答案 3 :(得分:8)

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

$(".phone").data($.mask.dataName)() 
// will produce "0102030405" while the masks displays 01 02 03 04 05

答案 4 :(得分:7)

{{1}}

答案 5 :(得分:2)

- 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

答案 6 :(得分:1)

或者您可以使用with_items:

- copy:
    src: "{{ item }}"
    dest: /etc/fooapp/
    owner: root
    mode: 600
  with_items:
    - dest_dir

答案 7 :(得分:1)

您可以遍历带有目录列表的变量:

>>> for p in partly_unordered_permutations('abcd', 2):
...     print(p)
... 
('a', 'b', 'c', 'd')
('a', 'b', 'd', 'c')
('a', 'c', 'b', 'd')
('a', 'c', 'd', 'b')
('a', 'd', 'b', 'c')
('a', 'd', 'c', 'b')
('b', 'c', 'a', 'd')
('b', 'c', 'd', 'a')
('b', 'd', 'a', 'c')
('b', 'd', 'c', 'a')
('c', 'd', 'a', 'b')
('c', 'd', 'b', 'a')

答案 8 :(得分:1)

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


 - 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}}”

答案 9 :(得分:0)

copy模块是用于复制许多文件和/或目录结构的错误工具,请改用synchronize模块,而将rsync作为后端。请注意,它需要在控制器和目标主机上都安装rsync。它非常强大,请检查ansible documentation

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

答案 10 :(得分:0)

自Ansible 2.5起,应使用with_*构造are deprecatedloop语法。一个简单的实际示例:

- 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