# How to copy a folder between two virtual machines?

**URL:** <https://forum.ansible.com/t/how-to-copy-a-folder-between-two-virtual-machines/42695>\
**Category:** Get Help\
**Tags:** yaml, playbook, inventory, debian\
**Created:** [May 15, 2025, 9:50pm UTC](https://forum.ansible.com/t/how-to-copy-a-folder-between-two-virtual-machines/42695 "2025-05-15T21:50:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![virusek391](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@virusek391](https://forum.ansible.com/u/virusek391)\
**Post date:** [May 15, 2025, 9:50pm UTC](https://forum.ansible.com/t/how-to-copy-a-folder-between-two-virtual-machines/42695/1 "2025-05-15T21:50:01Z")

</div>

Hi, I have vritual machine with Ansible. Now i want copy data from directory certyfikaty on svrhost to directory /svr/vaultwarden/cert folder on svrdocker. I don’t know what’s wrong, but below code dosen;t work

```auto
- name: Synchronizacja certyfikatów
  hosts: svrdocker
  become: yes
  tasks:
    - name: Kopiowanie plików z svrhost do svrdocker
      copy:
        src: /certyfikaty/czarnysad.ddns.net/
        dest: /svr/vaultwarden/cert
        remote_src: yes
        force: yes
      delegate_to: svrhost

```

Here is my inwentory file:

```auto
[servers]
svrhost ansible_host=10.0.0.12 ansible_user=Jan ansible_ssh_private_key_file=/root/.ssh/id_rsa
svrdocker ansible_host=10.0.0.16 ansible_user=Jan ansible_ssh_private_key_file=/root/.ssh/id_rsa

```

result:

 ![obraz](https://us1.discourse-cdn.com/ansible/original/2X/6/6db07a2fb1754e5251c47471367d72f6f24f47ee.png)  
The path /svr/vaultwarden/cert is existing on svrdocker. I think that he try use acccess to path /svr/vaultwarden/cert on svrhost.  
Kidn regards  
Lukasz

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [May 16, 2025, 1:22pm UTC](https://forum.ansible.com/t/how-to-copy-a-folder-between-two-virtual-machines/42695/2 "2025-05-16T13:22:45Z")

</div>

Your current playbook is saying "copy `/certyfikaty/czarnysad.ddns.net/` from `svrhost` to `/svr/vaultwarden/cert` on `svrhost`

Assuming you do not want to run ansible from `svrhost`, you could do one of

1. use `shell` and `delegate_to: svrhost` to sftp the file over
2. create some network mount on `svrhost`, then do copy like you have above, where the dest would be your network mounted path
3. use `slurp` or `fetch` to pull the file from svrhost to the ansible host, then use copy without `delegate_to` and `remote_src` to copy the file to `svrdocker`

---

<div class="post-metadata">

**Author:** ![virusek391](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@virusek391](https://forum.ansible.com/u/virusek391)\
**Post date:** [May 16, 2025, 6:47pm UTC](https://forum.ansible.com/t/how-to-copy-a-folder-between-two-virtual-machines/42695/3 "2025-05-16T18:47:32Z")

</div>

Hi, then ansible is running on other virutal machines. I think below pic explains it:

 ![obraz](https://us1.discourse-cdn.com/ansible/original/2X/8/8939f8dc4149a84fa42733ba98092db922cc3d48.png)

---

<div class="post-metadata">

**Author:** ![virusek391](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@virusek391](https://forum.ansible.com/u/virusek391)\
**Post date:** [May 16, 2025, 6:50pm UTC](https://forum.ansible.com/t/how-to-copy-a-folder-between-two-virtual-machines/42695/4 "2025-05-16T18:50:38Z")

</div>

Ok, I understand that I can’t directly upload files from svrhost to svrdocker, I have to first upload them from svrhost to the virtual machine with ansible?

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [May 16, 2025, 7:04pm UTC](https://forum.ansible.com/t/how-to-copy-a-folder-between-two-virtual-machines/42695/5 "2025-05-16T19:04:16Z")

</div>

Yea, that is what the slurp or fetch modules will do

---

<div class="post-metadata">

**Author:** ![virusek391](https://avatars.discourse-cdn.com/v4/letter/v/41988e/32.png) [@virusek391](https://forum.ansible.com/u/virusek391)\
**Post date:** [May 16, 2025, 10:33pm UTC](https://forum.ansible.com/t/how-to-copy-a-folder-between-two-virtual-machines/42695/6 "2025-05-16T22:33:39Z")

</div>

Ok, thx for your help 🙂

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [May 17, 2025, 1:24pm UTC](https://forum.ansible.com/t/how-to-copy-a-folder-between-two-virtual-machines/42695/7 "2025-05-17T13:24:41Z")

</div>

while [synchronize](https://docs.ansible.com/ansible/latest/collections/ansible/posix/synchronize_module.html#examples) (see 2nd and other examples) can do this, it can be tricky and it only works if the machines can ssh to each other, otherwise i recommend to just do `shell: scp -3 ..` + `delegate_to: localhost`, which avoids copying the files to the controller, they are just streamed through.
