# copy restricted files from server A to B

**URL:** <https://forum.ansible.com/t/copy-restricted-files-from-server-a-to-b/32975>\
**Category:** Ansible Project\
**Created:** [May 19, 2020, 6:29pm UTC](https://forum.ansible.com/t/copy-restricted-files-from-server-a-to-b/32975 "2020-05-19T18:29:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laci](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@Laci](https://forum.ansible.com/u/Laci)\
**Post date:** [May 19, 2020, 6:29pm UTC](https://forum.ansible.com/t/copy-restricted-files-from-server-a-to-b/32975/1 "2020-05-19T18:29:49Z")

</div>

I have some files with 0400/-r-------- permissions on server A that I need to copy to server B and preserve permissions.

For example:  
serverA:/etc/ssl\_cert.key → serverB:/etc/ssl\_cert.key

How would you do it?

---

<div class="post-metadata">

**Author:** ![Laci](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@Laci](https://forum.ansible.com/u/Laci)\
**Post date:** [May 19, 2020, 8:38pm UTC](https://forum.ansible.com/t/copy-restricted-files-from-server-a-to-b/32975/2 "2020-05-19T20:38:22Z")

</div>

Another detail is that serverA can’t talk to serverB or vice versa, only the Ansible server can talk to both of them

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 19, 2020, 8:49pm UTC](https://forum.ansible.com/t/copy-restricted-files-from-server-a-to-b/32975/3 "2020-05-19T20:49:31Z")

</div>

Have a look at the various examples on the synchronize module page:  
[https://docs.ansible.com/ansible/latest/modules/synchronize\_module.html](https://docs.ansible.com/ansible/latest/modules/synchronize_module.html).  
You can use a combination of delegate\_to and push/pull mode.

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [May 19, 2020, 8:49pm UTC](https://forum.ansible.com/t/copy-restricted-files-from-server-a-to-b/32975/4 "2020-05-19T20:49:51Z")

</div>

Use copy (to copy on the second server), fetch (to get the file on the controller), and stat (to get rights) modules

Regards,

JYL

---

<div class="post-metadata">

**Author:** ![Laci](https://avatars.discourse-cdn.com/v4/letter/l/c67d28/32.png) [@Laci](https://forum.ansible.com/u/Laci)\
**Post date:** [May 20, 2020, 9:25pm UTC](https://forum.ansible.com/t/copy-restricted-files-from-server-a-to-b/32975/5 "2020-05-20T21:25:27Z")

</div>

Thank you guys!  
It worked, this is the playbook for anyone having the same issues.  
At the end I added a test to remove files from buffer

_serverA% ls -l_  
_-rw-------. 1 root 6 May 20 16:11 ezaz.txt_

- hosts: serverA  
gather\_facts: no  
tasks:

- name: fetch test file  
fetch: src=/tmp/ezaz.txt dest=/tmp/buffer/ flat=yes fail\_on\_missing=yes

- hosts: serverB  
gather\_facts: no  
tasks:

- name: copy from dckr to dckr3  
copy: src=/tmp/buffer/ezaz.txt dest=/tmp/final\_ezaz.txt remote\_src=no owner=root group=root mode=0400 backup=yes force=yes

- hosts: ansible  
gather\_facts: no  
tasks:

- name: Delete certificates from /tmp  
file: path=/tmp/buffer/ezaz.txt state=absent

_serverB% ls -l_  
_-r--------. 1 root root 6 May 20 16:30 final\_ezaz.txt_
