Search code examples
ansibleremote-serveransible-2.x

Using Ansible copy module for remote to remote file transfer (same remote host)


I'm trying to move all files under a specific remote directory to another remote directory - on the same remote host - using Ansible's copy module.

The directory, and files, do exist on the remote host and I thought I'd use the Ansible copy module with remote_src: yes in order to achieve this.

However, so far I have been running into unforeseen issues with this approach - any help is appreciated, thank you!!

Task of concern

- name: copy remote to remote (same host)
  copy:
    src: "{{ item }}"
    dest: "{{ dir_base_path }}/go/to/my/nested/path"
    remote_src: yes
    owner: "{{ owner }}"
    group: "{{ group }}"
    mode: preserve
  with_fileglob:
    - "{{ dir_base_path }}/stay/at/parent_dir/*"
  when: status.changed and dir.stat.exists

Remote Directory structure

--> parent path
   -- all-the-files-I-need
   --`nested_directory
      -- need-to-copy-files here

Error Observed

TASK [playbook: copy remote to remote (same host)] ****************************************************************************************
 [WARNING]: Unable to find 'base_path/stay/at/parent_dir/' in expected paths (use -vvvvv to see paths)

Version Information

ansible --version
ansible 2.7.10
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0]

Solution

  • As per documentation for fileglob - Matching is against local system files on the Ansible controller. To iterate a list of files on a remote node, use the find module. Refer: https://docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html

    You can first use find command to find the files and then store using register and then copy those files.