Copy multiple files with Ansible

前端 未结 11 1054
执念已碎
执念已碎 2021-01-29 22:29

How can I copy more than a single file into remote nodes by Ansible in a task?

I\'ve tried to duplicate the copy module line in my task to define files but it only copie

相关标签:
11条回答
  • 2021-01-29 22:40

    If you need more than one location, you need more than one task. One copy task can copy only from one location (including multiple files) to another one on the node.

    - 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/*
    
    0 讨论(0)
  • 2021-01-29 22:41

    Since Ansible 2.5 the with_* constructs are deprecated, and loop syntax should be used. A simple practical example:

    - 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
    
    
    0 讨论(0)
  • 2021-01-29 22:42
    - 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
    
    0 讨论(0)
  • 2021-01-29 22:44

    You can use with_together for this purpose:

    - name: Copy multiple files to multiple directories
      copy: src={{ item.0 }} dest={{ item.1 }}
      with_together:
        - [ 'file1', 'file2', 'file3' ]
        - [ '/dir1/', '/dir2/', '/dir3/' ]
    
    0 讨论(0)
  • 2021-01-29 22:44

    Or you can use with_items:

    - copy:
        src: "{{ item }}"
        dest: /etc/fooapp/
        owner: root
        mode: 600
      with_items:
        - dest_dir
    
    0 讨论(0)
  • 2021-01-29 22:47
    - 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
    
    0 讨论(0)
提交回复
热议问题