# synchronize module and localhost

**URL:** <https://forum.ansible.com/t/synchronize-module-and-localhost/22285>\
**Category:** Ansible Project\
**Created:** [August 14, 2016, 3:39pm UTC](https://forum.ansible.com/t/synchronize-module-and-localhost/22285 "2016-08-14T15:39:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [August 14, 2016, 3:39pm UTC](https://forum.ansible.com/t/synchronize-module-and-localhost/22285/1 "2016-08-14T15:39:54Z")

</div>

I want to use the synchronize module to rsync a directory from one place  
on the control host to another place on the control host. My playbook  
looks like this:

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;become: True  
&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: False  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- local\_action: copy src=/tmp/foo.txt dest=/tmp/wip/{{inventory\_hostname}}.txt  
&nbsp;&nbsp;&nbsp;&nbsp;- local\_action: synchronize src=/tmp/wip/ dest=/tmp/real

Based on [http://docs.ansible.com/ansible/synchronize\_module.html](http://docs.ansible.com/ansible/synchronize_module.html), which says

&nbsp;&nbsp;# Synchronization of two paths both on the control machine  
&nbsp;&nbsp;local\_action: synchronize src=some/relative/path dest=/some/absolute/path

The first task creates a bunch of files in /tmp/wip, with the hostnames  
of my hosts in the filename. I expected the second task to use the  
synchronize module to copy /tmp/wip to /tmp/real on the control machine,  
but instead it copies it to each of the hosts. (run\_once: True on the  
second task causes it to only copy to one of the hosts, which limits the  
damage when testing. :^)

I've tried a couple of things, none of which work. Most surprisingly,  
changing the second task to

&nbsp;&nbsp;- synchronize: src=/tmp/wip/ dest=/tmp/real

does exactly the same thing, and some further experimentation suggests  
that delegate\_to / local\_action are only ever affecting the src host, and  
never the dest host.

How do you use the synchronize module to copy files from one play to  
another on localhost? Is there a way to change the dest host to localhost?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [August 14, 2016, 8:32pm UTC](https://forum.ansible.com/t/synchronize-module-and-localhost/22285/2 "2016-08-14T20:32:47Z")

</div>

> How do you use the synchronize module to copy files from one play to  
> another on localhost? Is there a way to change the dest host to localhost?

"from one \*directory\* to another on localhost", I meant.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [August 16, 2016, 7:46pm UTC](https://forum.ansible.com/t/synchronize-module-and-localhost/22285/3 "2016-08-16T19:46:02Z")

</div>

> How do you use the synchronize module to copy files from one play to  
> another on localhost? Is there a way to change the dest host to localhost?

On further reflection, I sort of think there isn't. In any invocation of  
the module, you're going to have a source host and a destination host, and  
you can use delegate\_to to change one of them, but there's no obvious way  
to tell it \*which\* one of them (so it's always the source host).

It'd be nice if you could just tell the module what to use as the source  
host and the destination host; the current behavior where it defaults to  
the control host and the remote host is probably good for many use cases,  
but there are also cases where you want to do entirely different things.

Should I make a feature request ticket for that?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [August 18, 2016, 2:17pm UTC](https://forum.ansible.com/t/synchronize-module-and-localhost/22285/4 "2016-08-18T14:17:56Z")

</div>

synchronize already does things very differently than any other ansible module. I’m afraid this would be too much. To do what you want, you’ll need to put a second play into your playbook like this:

- hosts: all  
become: True  
gather\_facts: False  
tasks:

- local\_action: copy src=/tmp/foo.txt dest=/tmp/wip/{{inventory\_hostname}}.txt

- hosts: localhost  
become: True  
gather\_facts: False  
tasks:

- local\_action: synchronize src=/tmp/wip/ dest=/tmp/real

-Toshio

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [August 19, 2016, 3:58pm UTC](https://forum.ansible.com/t/synchronize-module-and-localhost/22285/5 "2016-08-19T15:58:17Z")

</div>

> How do you use the synchronize module to copy files from one play to  
> another on localhost? Is there a way to change the dest host to localhost?

> synchronize already does things very differently than any other  
> ansible module. I'm afraid this would be too much. To do what you  
> want, you'll need to put a second play into your playbook like this:

Yeah, our problem is that we're doing this in the middle of a role, so all  
we have is delegate\_to.

Maybe the next time someone rewrites the synchronize module, it could take  
optional parameters to override the default source and dest hosts, or  
something like that. :^)

(Meanwhile, we can just use the command module, and take rsync out of the  
list of things that the command module emits warnings about.)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
