# Ansible copy file on remote machines on different location.

**URL:** <https://forum.ansible.com/t/ansible-copy-file-on-remote-machines-on-different-location/23873>\
**Category:** Ansible Project\
**Created:** [March 3, 2017, 9:33am UTC](https://forum.ansible.com/t/ansible-copy-file-on-remote-machines-on-different-location/23873 "2017-03-03T09:33:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![niraj\_pandey](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@niraj\_pandey](https://forum.ansible.com/u/niraj_pandey)\
**Post date:** [March 3, 2017, 9:33am UTC](https://forum.ansible.com/t/ansible-copy-file-on-remote-machines-on-different-location/23873/1 "2017-03-03T09:33:03Z")

</div>

> down vote[favorite](http://stackoverflow.com/questions/42433905/ansible-copy-file#)  
>   
>   
> |   
>   
>   
> How can I copy a file from machine A to machine B and machine C on different location. ie:  
>   
>   
>   
> On machine A I have file abc and I want to copy it on the /tmp area of machine B and /opt area of machine C.  
>   
>   
>   
>   
>   
>   
>   
>   
> I tried this so far:  
>   
>   
>   
>   
>   
>   
>   
>   
> **My inventory file:**  
>   
>   
>   
>   
>   
>   
>   
>   
> [webserver]  
>   
>   
>   
> machine-B dst=/tmp  
>   
>   
>   
> machine-C dst=/opt  
>   
>   
>   
>   
>   
>   
>   
>   
> **Playbook:**  
>   
>   
>   
>   
>   
>   
>   
>   
> - hosts: webserver  
>   
>   
>   
> tasks:  
>   
>   
>   
> - name: Transfer file from ServerA to ServerB  
>   
>   
>   
> synchronize:  
>   
>   
>   
> src: /home/niraj/ansible\_testing  
>   
>   
>   
> dest: “{{ hostvars[item][‘dst’] }}”  
>   
>   
>   
> with\_items: “{{ groups[‘webserver’] }}”  
>   
>   
>   
> #dest: /home/niraj  
>   
>   
>   
> #mode: pull  
>   
>   
>   
>   
>   
>   
>   
> delegate\_to: machine-A  
>   
>   
> |

---

<div class="post-metadata">

**Author:** ![niraj\_pandey](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@niraj\_pandey](https://forum.ansible.com/u/niraj_pandey)\
**Post date:** [March 3, 2017, 9:54am UTC](https://forum.ansible.com/t/ansible-copy-file-on-remote-machines-on-different-location/23873/2 "2017-03-03T09:54:45Z")

</div>

I tried this but it copying the file on both the location on both the machines.

---

<div class="post-metadata">

**Author:** ![niraj\_pandey](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@niraj\_pandey](https://forum.ansible.com/u/niraj_pandey)\
**Post date:** [March 10, 2017, 7:04am UTC](https://forum.ansible.com/t/ansible-copy-file-on-remote-machines-on-different-location/23873/3 "2017-03-10T07:04:53Z")

</div>

Can someone please help me on this.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 10, 2017, 3:24pm UTC](https://forum.ansible.com/t/ansible-copy-file-on-remote-machines-on-different-location/23873/4 "2017-03-10T15:24:57Z")

</div>

This should do it.

- hosts: webserver  
&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Transfer file from ServerA to ServerB  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;synchronize:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: /home/niraj/ansible\_testing  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ dst }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: machine-A

---

<div class="post-metadata">

**Author:** ![niraj\_pandey](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@niraj\_pandey](https://forum.ansible.com/u/niraj_pandey)\
**Post date:** [March 22, 2017, 8:31am UTC](https://forum.ansible.com/t/ansible-copy-file-on-remote-machines-on-different-location/23873/5 "2017-03-22T08:31:46Z")

</div>

Many thanks !!
