Ansible: copy a directory content to another directory

前端 未结 12 1075
青春惊慌失措
青春惊慌失措 2020-12-24 00:25

I am trying to copy the content of dist directory to nginx directory.

- name: copy html file
  copy: src=/home/vagrant/dist/ dest=/usr/share/nginx/html/


        
相关标签:
12条回答
  • 2020-12-24 01:21

    Resolved answer: To copy a directory's content to another directory I use the next:

    - name: copy consul_ui files
      command: cp -r /home/{{ user }}/dist/{{ item }} /usr/share/nginx/html
      with_items:
       - "index.html"
       - "static/"
    

    It copies both items to the other directory. In the example, one of the items is a directory and the other is not. It works perfectly.

    0 讨论(0)
  • 2020-12-24 01:21

    the ansible doc is quite clear https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html for parameter src it says the following:

    Local path to a file to copy to the remote server.
    This can be absolute or relative.
    If path is a directory, it is copied recursively. In this case, if path ends with "/", 
    only inside contents of that directory are copied to destination. Otherwise, if it 
    does not end with "/", the directory itself with all contents is copied. This behavior 
    is similar to the rsync command line tool.
    

    So what you need is skip the / at the end of your src path.

    - name: copy html file
      copy: src=/home/vagrant/dist dest=/usr/share/nginx/html/
    
    0 讨论(0)
  • 2020-12-24 01:27

    To copy a directory's content to another directory you can use ansibles copy module:

    - name: Copy content of directory 'files'
      copy:
        src: files/    # note the '/' <-- !!!
        dest: /tmp/files/
    

    From the docs about the src parameter:

    If (src!) path is a directory, it is copied recursively...
    ... if path ends with "/", only inside contents of that directory are copied to destination.
    ... if it does not end with "/", the directory itself with all contents is copied.

    0 讨论(0)
  • 2020-12-24 01:30

    The simplest solution I've found to copy the contents of a folder without copying the folder itself is to use the following:

    - name: Move directory contents
      command: cp -r /<source_path>/. /<dest_path>/
    

    This resolves @surfer190's follow-up question:

    Hmmm what if you want to copy the entire contents? I noticed that * doesn't work – surfer190 Jul 23 '16 at 7:29

    * is a shell glob, in that it relies on your shell to enumerate all the files within the folder before running cp, while the . directly instructs cp to get the directory contents (see https://askubuntu.com/questions/86822/how-can-i-copy-the-contents-of-a-folder-to-another-folder-in-a-different-directo)

    0 讨论(0)
  • 2020-12-24 01:30

    Ansible remote_src does not support recursive copying.See remote_src description in Ansible copy docs

    To recursively copy the contents of a folder and to make sure the task stays idempotent I usually do it this way:

    - name: get file names to copy
      command: "find /home/vagrant/dist -type f"
      register: files_to_copy
    
    - name: copy files
      copy:
        src: "{{ item }}" 
        dest: "/usr/share/nginx/html"
        owner: nginx
        group: nginx
        remote_src: True
        mode: 0644
      with_items:
       - "{{ files_to_copy.stdout_lines }}"
    

    Downside is that the find command still shows up as 'changed'

    0 讨论(0)
  • 2020-12-24 01:32

    How to copy directory and sub dirs's and files from ansible server to remote host

    - name: copy nmonchart39 directory  to {{ inventory_hostname }}
      copy:
        src: /home/ansib.usr.srv/automation/monitoring/nmonchart39
        dest: /var/nmon/data
    
    
    Where:
    copy entire directory: src: /automation/monitoring/nmonchart39
    copy directory contents src: nmonchart39/
    
    0 讨论(0)
提交回复
热议问题