synchronize module and localhost

186 views
Skip to first unread message

Josh Smift

unread,
Aug 14, 2016, 11:40:21 AM8/14/16
to ansible...@googlegroups.com
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:

- hosts: all
become: True
gather_facts: False
tasks:
- local_action: copy src=/tmp/foo.txt dest=/tmp/wip/{{inventory_hostname}}.txt
- local_action: synchronize src=/tmp/wip/ dest=/tmp/real

Based on http://docs.ansible.com/ansible/synchronize_module.html, which says

# Synchronization of two paths both on the control machine
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

- 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?

-Josh (j...@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.

Josh Smift

unread,
Aug 14, 2016, 4:33:11 PM8/14/16
to ansible...@googlegroups.com
JBS> How do you use the synchronize module to copy files from one play to
JBS> another on localhost? Is there a way to change the dest host to localhost?

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

Josh Smift

unread,
Aug 16, 2016, 3:46:19 PM8/16/16
to ansible...@googlegroups.com
JBS> How do you use the synchronize module to copy files from one play to
JBS> 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?

tkuratomi

unread,
Aug 18, 2016, 10:17:57 AM8/18/16
to Ansible Project, j...@care.com


On Tuesday, August 16, 2016 at 12:46:19 PM UTC-7, Josh Smift wrote:
JBS> How do you use the synchronize module to copy files from one play to
JBS> 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?

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

Josh Smift

unread,
Aug 19, 2016, 11:58:31 AM8/19/16
to Ansible Project
JBS> How do you use the synchronize module to copy files from one play to
JBS> another on localhost? Is there a way to change the dest host to localhost?

TK> synchronize already does things very differently than any other
TK> ansible module. I'm afraid this would be too much. To do what you
TK> 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.)
Reply all
Reply to author
Forward
0 new messages