ansible

How do I copy a remote file onto my local machine using Ansible?


I'm using the command module inside my playbook, and it currently looks like this.

- hosts: all

  tasks:   
   - name: Update tar file
     command: sudo scp -r username@hostname:/path/from/destination /path/to/destination

I've omitted the tasks the take place before this task for the purpose of readability, but what happens when I run this playbook is that it stop at this task. It simply doesn't move forward. I'm sure this is because sudo, so It may want the password for that. I'm not sure how to fix that however.


Solution

  • You want to use the fetch module.

    - hosts: host.example.com
      tasks:
        # Copy remote file (host.example.com:/tmp/somefile) into
        # /tmp/fetched/host.example.com/tmp/somefile on local machine
        - ansible.builtin.fetch:
            src: /tmp/somefile
            dest: /tmp/fetched