# Speeding up a task

**URL:** <https://forum.ansible.com/t/speeding-up-a-task/36924>\
**Category:** Ansible Project\
**Created:** [July 30, 2022, 10:44am UTC](https://forum.ansible.com/t/speeding-up-a-task/36924 "2022-07-30T10:44:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HobsonI](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hobsoni/32/4960_2.png) [@HobsonI](https://forum.ansible.com/u/HobsonI)\
**Post date:** [July 30, 2022, 10:44am UTC](https://forum.ansible.com/t/speeding-up-a-task/36924/1 "2022-07-30T10:44:12Z")

</div>

Hi Guys,

I need advice on speeding up this task:

- name: "restore files to {{ location }}"  
&nbsp;&nbsp;&nbsp;ansible.builtin.copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "/home/ian/backup/hobson42/var/www/ianhobson.com/"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ location }}"

It copies 5022 files, totalling 109.2Mb, and even though both machines have NVME SSDs and the network is quiet,  
the task takes over 30 minutes! Note - some of the files are owned by root, and have "600" permissions, which need to be preserved.

How do I go about this in a secure way?

I was thinking of setting up passwordless keyfile access for root, and then using command: sudo rsync, delegated to localhost, and tearing root access down again afterwards. How would I do this using Ansible?

Ideas and advice welcome.

Ian

---

<div class="post-metadata">

**Author:** ![skratbag](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/skratbag/32/4836_2.png) [@skratbag](https://forum.ansible.com/u/skratbag)\
**Post date:** [July 30, 2022, 10:49am UTC](https://forum.ansible.com/t/speeding-up-a-task/36924/2 "2022-07-30T10:49:01Z")

</div>

Hi Ian,

Look at [https://docs.ansible.com/ansible/latest/collections/ansible/posix/synchronize\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/posix/synchronize_module.html)

This incorporates raync and may well do what you need to do substantially faster.

Cheers

When in trouble, or in doubt  
Run in circles, scream and shout

---

<div class="post-metadata">

**Author:** ![Michael\_Stroder](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Michael\_Stroder](https://forum.ansible.com/u/Michael_Stroder)\
**Post date:** [July 30, 2022, 10:51am UTC](https://forum.ansible.com/t/speeding-up-a-task/36924/3 "2022-07-30T10:51:45Z")

</div>

I'd recommend to avoid copying thousands of files.

Ideas to try:  
- Trigger rsync [1] if most of the files are static  
- Pack the files into a single tar.gz and transfer and extract that [2][3]

Ciao, Michael.

[1] [https://docs.ansible.com/ansible/latest/collections/ansible/posix/synchronize\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/posix/synchronize_module.html)

[2] [https://docs.ansible.com/ansible/latest/collections/community/general/archive\_module.html](https://docs.ansible.com/ansible/latest/collections/community/general/archive_module.html)

[3] [https://docs.ansible.com/ansible/latest/collections/ansible/builtin/unarchive\_module.html#ansible-collections-ansible-builtin-unarchive-module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/unarchive_module.html#ansible-collections-ansible-builtin-unarchive-module)

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 30, 2022, 10:55am UTC](https://forum.ansible.com/t/speeding-up-a-task/36924/4 "2022-07-30T10:55:26Z")

</div>

Use module \*ansible.posix.synchronize\* if you can install \*rsync\*  
both on local and remote host. See  
[https://docs.ansible.com/ansible/latest/collections/ansible/posix/synchronize\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/posix/synchronize_module.html)

---

<div class="post-metadata">

**Author:** ![HobsonI](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hobsoni/32/4960_2.png) [@HobsonI](https://forum.ansible.com/u/HobsonI)\
**Post date:** [July 31, 2022, 4:06am UTC](https://forum.ansible.com/t/speeding-up-a-task/36924/5 "2022-07-31T04:06:06Z")

</div>

Hi Michael,

Your idea triggered the best solution I have found. Thanks.

This is my solution - for people who discover this later.

io.hcs holds the backup we are trying to restore.  
sinope.hcs is where we are restoring to.  
callisto.hcs is where ansible is run.

There is a (read only) samba share on io.hcs that is permanently open on callisto.hcs, but can't be used to copy the files over, because the permissions get altered. But it can be used to copy the archive over. tar maintains ownership and permissions as default behaviour.

Therefore the tasks are:

&nbsp;&nbsp;&nbsp;- name: get IO to tar up the website  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;community.general.archive:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "/home/ian/BackupFiles/hobson42/var/www/ianhobson.com/"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /home/ian/BackupFiles/ianhobson.com.gz # will appear in share as /home/ian/backup/ianhobson.com.gz  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: io.hcs

&nbsp;&nbsp;&nbsp;- name: create {{ location }} directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ location }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: directory

&nbsp;&nbsp;&nbsp;- name: move archive over to target, and extract it.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.unarchive:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: /home/ian/backup/ianhobson.com.gz  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ location }}"

&nbsp;&nbsp;&nbsp;- name: remove archive  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /home/ian/BackupFiles/ianhobson.com.gz  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: io.hcs

Result is a speed up of between 60 and 100 times!  
The files are restored with the correct permissions and ownership. I think it is necessary that all users are set up on all machines with the same user numbers in /etc/passwd.

Regards  
Ian

---

<div class="post-metadata">

**Author:** ![HobsonI](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hobsoni/32/4960_2.png) [@HobsonI](https://forum.ansible.com/u/HobsonI)\
**Post date:** [July 31, 2022, 4:59am UTC](https://forum.ansible.com/t/speeding-up-a-task/36924/6 "2022-07-31T04:59:22Z")

</div>

Hi Michael,

Your idea triggered the best solution I have found. Thanks.

This is my solution - for people who discover this later.

io.hcs holds the backup we are trying to restore.  
sinope.hcs is where we are restoring to.  
callisto.hcs is where ansible is run.

There is a (read only) samba share on io.hcs that is permanently open on callisto.hcs, but can't be used to copy the files over, because the permissions get altered. But it can be used to copy the archive over. tar maintains ownership and permissions as default behaviour.

Therefore the tasks are:

&nbsp;&nbsp;&nbsp;- name: get IO to tar up the website  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;community.general.archive:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "/home/ian/BackupFiles/hobson42/var/www/ianhobson.com/"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /home/ian/BackupFiles/ianhobson.com.gz # will appear in share as /home/ian/backup/ianhobson.com.gz  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: io.hcs

&nbsp;&nbsp;&nbsp;- name: create {{ location }} directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ location }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: directory

&nbsp;&nbsp;&nbsp;- name: move archive over to target, and extract it.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.unarchive:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: /home/ian/backup/ianhobson.com.gz  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ location }}"

&nbsp;&nbsp;&nbsp;- name: remove archive  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /home/ian/BackupFiles/ianhobson.com.gz  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: io.hcs

Result is a speed up of between 60 and 100 times!  
The files are restored with the correct permissions and ownership. I think it is necessary that all users are set up on all machines with the same user numbers in /etc/passwd.

Regards  
Ian
