Copy multiple files with Ansible

前端 未结 11 1055
执念已碎
执念已碎 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:50

    Use the following source code for copy multiple files on your client machine.


     - 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
    

    Note:

    If you are passing multiple paths by using variable then

    src: "/root/{{ item }}"

    If you are passing path by using a variable for different items then

    src: "/root/{{ item.source_path }}"

    0 讨论(0)
  • 2021-01-29 22:51

    You can use the with_fileglob loop for this:

    - copy:
        src: "{{ item }}"
        dest: /etc/fooapp/
        owner: root
        mode: 600
      with_fileglob:
        - /playbooks/files/fooapp/*
    
    0 讨论(0)
  • 2021-01-29 22:52

    You can loop through variable with list of directories:

    - name: Copy files from several directories
      copy:
        src: "{{ item }}"
        dest: "/etc/fooapp/"
        owner: root
        mode: "0600"
      loop: "{{ files }}"
      vars:
        files:
          - "dir1/"
          - "dir2/"
    
    0 讨论(0)
  • 2021-01-29 22:58
    - 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 }}"
    
    0 讨论(0)
  • 2021-01-29 23:06

    copy module is a wrong tool for copying many files and/or directory structure, use synchronize module instead which uses rsync as backend. Mind you, it requires rsync installed on both controller and target host. It's really powerful, check ansible documentation.

    Example - copy files from build directory (with subdirectories) of controller to /var/www/html directory on target host:

    synchronize:
      src: ./my-static-web-page/build/
      dest: /var/www/html
      rsync_opts:
        - "--chmod=D2755,F644" # copy from windows - force permissions
    
    0 讨论(0)
提交回复
热议问题