# Copy multiple files to different locations on remote server

**URL:** <https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892>\
**Category:** Ansible Project\
**Created:** [October 29, 2018, 4:55pm UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892 "2018-10-29T16:55:37Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![anushakeshipeddy4](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anushakeshipeddy4](https://forum.ansible.com/u/anushakeshipeddy4)\
**Post date:** [October 29, 2018, 4:55pm UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/1 "2018-10-29T16:55:37Z")

</div>

Hi,

I am trying to copy multiple files to different locations using with\_items. Could someone please help me with this

**vars: main.yml**

wars:  
file1.war  
file2.war  
file3.war  
file4.war

dest\_path:  
/tmp/{wars}

**tasks- main.yml**

---

<div class="post-metadata">

**Author:** ![Foss\_Geek](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@Foss\_Geek](https://forum.ansible.com/u/Foss_Geek)\
**Post date:** [October 29, 2018, 5:32pm UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/2 "2018-10-29T17:32:39Z")

</div>

Try like this:

# cat site.yml

---

<div class="post-metadata">

**Author:** ![Foss\_Geek](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@Foss\_Geek](https://forum.ansible.com/u/Foss_Geek)\
**Post date:** [October 29, 2018, 6:02pm UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/3 "2018-10-29T18:02:04Z")

</div>

do you want to copy multiple target like this?

# cat site.yml

---

<div class="post-metadata">

**Author:** ![anushakeshipeddy4](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anushakeshipeddy4](https://forum.ansible.com/u/anushakeshipeddy4)\
**Post date:** [October 30, 2018, 11:59am UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/4 "2018-10-30T11:59:59Z")

</div>

Thank you Mohan that worked for me

---

<div class="post-metadata">

**Author:** ![anushakeshipeddy4](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anushakeshipeddy4](https://forum.ansible.com/u/anushakeshipeddy4)\
**Post date:** [November 1, 2018, 4:18am UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/5 "2018-11-01T04:18:06Z")

</div>

How to replace the existing file on remote server …should we use any specific module for that? I tried using force=yes but it didn’t work for me.

---

<div class="post-metadata">

**Author:** ![Foss\_Geek](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@Foss\_Geek](https://forum.ansible.com/u/Foss_Geek)\
**Post date:** [November 1, 2018, 2:37pm UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/6 "2018-11-01T14:37:21Z")

</div>

What is happening when you try to use force: yes?

---

<div class="post-metadata">

**Author:** ![anushakeshipeddy4](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anushakeshipeddy4](https://forum.ansible.com/u/anushakeshipeddy4)\
**Post date:** [November 1, 2018, 2:41pm UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/7 "2018-11-01T14:41:28Z")

</div>

Okay got it. Is there any specific module that we can use to replace a complete file in remote server?

Thank you for your help.

Thanks,  
Anusha

---

<div class="post-metadata">

**Author:** ![Foss\_Geek](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@Foss\_Geek](https://forum.ansible.com/u/Foss_Geek)\
**Post date:** [November 2, 2018, 8:50am UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/8 "2018-11-02T08:50:09Z")

</div>

copy module with force yes automatically replace it. Otherwise you may need to delete it before copy task.

---

<div class="post-metadata">

**Author:** ![anushakeshipeddy4](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anushakeshipeddy4](https://forum.ansible.com/u/anushakeshipeddy4)\
**Post date:** [November 2, 2018, 8:51am UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/9 "2018-11-02T08:51:14Z")

</div>

No change Mohan  
It is not replacing the file

---

<div class="post-metadata">

**Author:** ![Foss\_Geek](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@Foss\_Geek](https://forum.ansible.com/u/Foss_Geek)\
**Post date:** [November 2, 2018, 9:25am UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/10 "2018-11-02T09:25:09Z")

</div>

Try with command or shell module with cp -f.

---

<div class="post-metadata">

**Author:** ![Frank\_Thommen](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@Frank\_Thommen](https://forum.ansible.com/u/Frank_Thommen)\
**Post date:** [November 2, 2018, 10:24am UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/11 "2018-11-02T10:24:47Z")

</div>

This is not the same:

&nbsp;&nbsp;&nbsp;\* copy module copies from the controller (where you run the  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible-playbook command) to the client  
&nbsp;&nbsp;&nbsp;\* cp -f copies a file locally from client to itself

frank

---

<div class="post-metadata">

**Author:** ![Frank\_Thommen](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@Frank\_Thommen](https://forum.ansible.com/u/Frank_Thommen)\
**Post date:** [November 2, 2018, 10:27am UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/12 "2018-11-02T10:27:46Z")

</div>

Then your remote file is most probably identical to the one you are trying to copy 😉

Did you check that your local file (src) is for sure different from the remote one (dest)? Run `ansible-playbook` with '-vvv' to see, which local files ansible selects (this can be confusing when you are using roles) and compare with remote file.

frank

---

<div class="post-metadata">

**Author:** ![Foss\_Geek](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@Foss\_Geek](https://forum.ansible.com/u/Foss_Geek)\
**Post date:** [November 2, 2018, 10:40am UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/13 "2018-11-02T10:40:48Z")

</div>

Oh! yeah. I missed that part.

---

<div class="post-metadata">

**Author:** ![anushakeshipeddy4](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anushakeshipeddy4](https://forum.ansible.com/u/anushakeshipeddy4)\
**Post date:** [November 2, 2018, 11:37am UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/14 "2018-11-02T11:37:23Z")

</div>

I am trying with below playbook and getting errors

---

<div class="post-metadata">

**Author:** ![anushakeshipeddy4](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anushakeshipeddy4](https://forum.ansible.com/u/anushakeshipeddy4)\
**Post date:** [November 5, 2018, 11:40am UTC](https://forum.ansible.com/t/copy-multiple-files-to-different-locations-on-remote-server/28892/15 "2018-11-05T11:40:52Z")

</div>

Could someone please help me in writing shell commands
