Ansible Copy File Locally

Ansible Copy File Locally

We can copy files locally using ansible copy module. But Ansible copy modules works in the way like it will copy the files from source machine to destination machine nothing but localhost(ansible machine) to target serverS in the inventory. But here we need to copy the files in locally from one path to another path. To achieve this we use ansible delegate_to module. Using ansible delegate_to module we can execute any task in local machine itself.

Ansible Copy Files Locally Example

root@ip-10-0-0-20:~# cat copy.yml 
---
- hosts: all
  gather_facts: no
 
  tasks:
  - name: ansible copy file locally.
    copy:
     src: /root/devops.txt
     dest: /root/abcd/devops/
    delegate_to: localhost

If you mention delegate_to: localhost the task will be executed in locally, So in this way we can copy the files in locally.

Note:  in the above playbook you can mention localhost OR 127.0.0.1  both refers to ansible local machine.

Log Output:

root@ip-10-0-0-20:~# ansible-playbook copy.yml 

PLAY [all] *********************************************************************

TASK [ansible copy file locally.] **********************************************
changed: [192.168.100.1 -> localhost]

PLAY RECAP *********************************************************************
192.168.100.1              : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

root@ip-10-0-0-20:~#

 

Leave a Reply

Your email address will not be published. Required fields are marked *