# Ansible copy multiple files to multiple destinations ??

**URL:** <https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003>\
**Category:** Ansible Project\
**Created:** [November 12, 2017, 7:43am UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003 "2017-11-12T07:43:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Love](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@Love](https://forum.ansible.com/u/Love)\
**Post date:** [November 12, 2017, 7:43am UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003/1 "2017-11-12T07:43:02Z")

</div>

We need to be able to copy/push multiple files from multiple sources to multiple destinations on destination servers, I’m having trouble with below code, can anyone suggest the right syntax to update code?

I have 3 different files and destination is different for all of them and need this across my inventory, from a syntax standpoint I do not have any issues however, it does not push those files to destination servers.

— # Deploy EP7.0 on desired servers

- hosts: test  
remote\_user: deploy  
become: yes  
become\_method: sudo  
connection: ssh  
gather\_facts: yes

tasks:

- name: check if maven repo exists on destination server  
file:  
path: /opt/dtv/apache-maven-3.5.0/.m2/repository  
state: directory  
owner: root  
group: root  
mode: 0755  
notify: maven repo created

handlers:

- name: maven repo created  
copy:  
src: “{{ item.src }}”  
dest: “{{ item.dest }}”  
mode: “{{ item.mode }}”  
with\_items:

- { src: ‘/home/psk/settings.xml’,dest: ‘/opt/dtv/apache-maven-3.5.0/.m2/repository/’,mode: ‘0755’}

- “{ src: ‘/etc/ansible/roles/common/files/settings.xml’ , dest: ‘/opt/dtv/apache-maven-3.5.0/conf/’ , mode: ‘0755’}”

- “{ src: ‘/etc/ansible/roles/common/files/archetype-catalog.xml’ , dest: ‘/opt/dtv/apache-maven-3.5.0/.m2/repository/’ , mode: ‘0755’}”

- debug: var=result

---

<div class="post-metadata">

**Author:** ![Soniya\_panwar](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Soniya\_panwar](https://forum.ansible.com/u/Soniya_panwar)\
**Post date:** [November 14, 2017, 5:33am UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003/2 "2017-11-14T05:33:09Z")

</div>

Hello,  
Can you please elaborate your tasks more?

1. In first task: you are checking if the file is exist on destination server, and if not exist you will create new directory?
2. In second task: you are saying maven repo created but in this task you are coping your files src to destination.

if you can elaborate what is your scenario and why are you using handler here, that will be a great help.

thanks  
Soniya

---

<div class="post-metadata">

**Author:** ![Love](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@Love](https://forum.ansible.com/u/Love)\
**Post date:** [November 23, 2017, 2:22pm UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003/3 "2017-11-23T14:22:38Z")

</div>

Hello Soniya,

Yes,

First I need to check if the directory exists or NOT & create it, works perfect!!  
Second: After the above directory is created, I need to copy 3 different files from 3 different sources to 2 destinations. In other words many to many copy is NOT working for me.

Please let me know if its still not clear?

---

<div class="post-metadata">

**Author:** ![lidor.ettinger](https://avatars.discourse-cdn.com/v4/letter/l/bbce88/32.png) [@lidor.ettinger](https://forum.ansible.com/u/lidor.ettinger)\
**Post date:** [November 23, 2017, 10:05pm UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003/4 "2017-11-23T22:05:07Z")

</div>

Hi

You can consider something like:

win\_copy:  
src: “{{item.src}}”  
dest: “{{item.dest}}”  
with\_items:

- { src: path\_to\_src , dest: path\_to\_dest}
- { src: path\_to\_src , dest: path\_to\_dest}
- { src: path\_to\_src , dest: path\_to\_dest}

Depending if you want to copy to linux machine or windows so then there are also different modules to use in order to copy bunch of files from the same location source.

---

<div class="post-metadata">

**Author:** ![Love](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@Love](https://forum.ansible.com/u/Love)\
**Post date:** [November 27, 2017, 11:08pm UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003/5 "2017-11-27T23:08:05Z")

</div>

This is a 100% linux/unix environment, so I did not even consider win\_copy, do you think “win\_copy” will work on/for “linux to linux” ? I will definitely give it a try & also need permissions intact at destination, so I would need to insert mode for those files.

---

<div class="post-metadata">

**Author:** ![Soniya\_panwar](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Soniya\_panwar](https://forum.ansible.com/u/Soniya_panwar)\
**Post date:** [November 28, 2017, 4:51am UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003/6 "2017-11-28T04:51:44Z")

</div>

Hello,

You may use below code to perform many to many copy. You may use a when condition to ensure that copy task takes place only when the directory is sucessfully created. In case of directory creation getting failed, the

copy task would not be performed.

Below is the detailed playbook:

1. create the directory
2. check if maven repo directory exists on destination server
3. copy files from source to destination once the creation is successful

— # Deploy EP7.0 on desired servers

- hosts: test  
remote\_user: deploy  
become: yes  
become\_method: sudo  
connection: ssh  
gather\_facts: yes  
tasks:
- name: Create maven if not exist  
file:  
path: /opt/dtv/apache-maven-3.5.0/.m2/repository  
state: directory  
owner: root  
group: root  
mode: 0755
- name: check if maven repo exists on destination server  
stat: path=/opt/dtv/apache-maven-3.5.0/.m2/repository  
register: stat\_result
- debug: var=stat\_result
- name: copy files from source to destination  
copy:  
src: “{{ item.src }}”  
dest: “{{ item.dest }}”  
mode: “{{ item.mode }}”  
with\_items:
- { src: ‘/home/psk/settings.xml’,dest: ‘/opt/dtv/apache-maven-3.5.0/.m2/repository/’,mode: ‘0755’}
- { src: ‘/etc/ansible/roles/common/files/settings.xml’ , dest: ‘/opt/dtv/apache-maven-3.5.0/conf/’ , mode: ‘0644’}
- { src: ‘/etc/ansible/roles/common/files/archetype-catalog.xml’ , dest: ‘/opt/dtv/apache-maven-3.5.0/.m2/repository/’ , mode: ‘0644’}  
when: stat\_result.stat.exists == True

**Output:-**

TASK [maven repo created] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [host1] =\> (item={u’dest’: u’/opt/dtv/apache-maven-3.5.0/.m2/repository/‘, u’src’: u’/home/psk/settings.xml’, u’mode’: u’0755’})  
ok: [host1] =\> (item={u’dest’: u’/opt/dtv/apache-maven-3.5.0/conf/‘, u’src’: u’/etc/ansible/roles/common/files/settings.xml’, u’mode’: u’0644’})  
ok: [host1] =\> (item={u’dest’: u’/opt/dtv/apache-maven-3.5.0/.m2/repository/‘, u’src’: u’/etc/ansible/roles/common/files/archetype-catalog.xml’, u’mode’: u’0644’})

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
host1 : ok=5 changed=0 unreachable=0 failed=0

Thanks  
Soniya

---

<div class="post-metadata">

**Author:** ![Love](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@Love](https://forum.ansible.com/u/Love)\
**Post date:** [December 1, 2017, 1:23pm UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003/7 "2017-12-01T13:23:51Z")

</div>

Hello Soniya,

Thanks for your input, I see code executed successfully without errors but files were not copied to desired destinations.

---

<div class="post-metadata">

**Author:** ![Soniya\_panwar](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Soniya\_panwar](https://forum.ansible.com/u/Soniya_panwar)\
**Post date:** [December 7, 2017, 8:50am UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003/8 "2017-12-07T08:50:25Z")

</div>

Hello Love,

It will be a great help if you can share the screenshot of your playbook’s output as well as the screenshot of source and destination.

The above playbook is working perfectly fine in our environment.

Thanks

Soniya

---

<div class="post-metadata">

**Author:** ![Love](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@Love](https://forum.ansible.com/u/Love)\
**Post date:** [December 7, 2017, 9:54pm UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003/9 "2017-12-07T21:54:58Z")

</div>

Hello Soniya,

Sent reply to only you as verbose output was too huge, please send me alone your email if you haven’t received it so I can send you as an attachment.

---

<div class="post-metadata">

**Author:** ![Soniya\_panwar](https://avatars.discourse-cdn.com/v4/letter/s/22d042/32.png) [@Soniya\_panwar](https://forum.ansible.com/u/Soniya_panwar)\
**Post date:** [December 20, 2017, 11:15am UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003/10 "2017-12-20T11:15:52Z")

</div>

Hello Love,

You cannot implement this scenario using Notify-Handler. You will have to use the WHEN condition (Implementation shown in below playbook).

One of the property of Handlers is that they get fired only if there is a change in state of the task which is triggering the notification (Notify Statement).  
In your use-case there are 2 scenarios:

**Scenario-1:** The destination maven repo does not exist and is created by the File module. In this case there is a change in state of task which triggers the Notification to “maven repo created” handler. Hence the handler will execute and the files would get copied.

**Scenario-2:** The destination maven repo already exists and File module does nothing that results in the change of state of the task. Hence when the notification reaches the Handler it does nothing and files are not copied.

Since this is a valid effect produced by the Handler hence there were no error/alerts thrown during the playbook execution.

**Correct playbook:**

---

<div class="post-metadata">

**Author:** ![Love](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@Love](https://forum.ansible.com/u/Love)\
**Post date:** [January 17, 2018, 4:12pm UTC](https://forum.ansible.com/t/ansible-copy-multiple-files-to-multiple-destinations/26003/11 "2018-01-17T16:12:54Z")

</div>

Hi Soniya,

Cool, I will implement this one and let you know. Thank you very much!!
