# synchronize between 2 files on remote host uses private\_key

**URL:** <https://forum.ansible.com/t/synchronize-between-2-files-on-remote-host-uses-private-key/17466>\
**Category:** Ansible Project\
**Created:** [February 7, 2015, 4:05pm UTC](https://forum.ansible.com/t/synchronize-between-2-files-on-remote-host-uses-private-key/17466 "2015-02-07T16:05:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Spiegle](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@Michael\_Spiegle](https://forum.ansible.com/u/Michael_Spiegle)\
**Post date:** [February 7, 2015, 4:05pm UTC](https://forum.ansible.com/t/synchronize-between-2-files-on-remote-host-uses-private-key/17466/1 "2015-02-07T16:05:48Z")

</div>

I have a simple task to copy a file from one path to another on a remote host. I need a private key to SSH into the remote host, but the synchronize module automatically uses my private key in the rsync command too which seems unnecessary:

`

ansible-playbook --private-key=keys/mykey.pem playbook.yml --tags=‘main,role-timezone-set-localtime’ -vvvv

… some ansible output …  
TASK: [timezone | set /etc/localtime to UTC] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
(redacted output)

failed: [remote\_machine → remote\_machine] =\> {“cmd”: “rsync --delay-updates -FF --compress --checksum --archive --rsh ‘ssh -i keys/mykey.pem -S none -o StrictHostKeyChecking=no’ --out-format=‘\<\>%i %n%L’ "user@1.2.3.4:/usr/share/zoneinfo/UTC" "/etc/localtime"”, “failed”: true, “rc”: 255}

msg: Warning: Identity file keys/mykey.pem not accessible: No such file or directory.  
ssh: connect to host 1.2.3.4 port 22: Connection timed out  
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]  
rsync error: unexplained error (code 255) at io.c(605) [Receiver=3.0.9]

`

In the above, you can see that the rsync process running on the remote machine is trying to use an ssh private key to log into itself, but that key only exists on my laptop where I’m running Ansible from. Here’s what my task looks like (I tried nulling the private\_key to no avail):

`

- name: set /etc/localtime to {{timezone}}

synchronize:

src: “{{tzfile}}”

dest: /etc/localtime

mode: pull

times: yes

checksum: yes

private\_key: “”

delegate\_to: “{{inventory\_hostname}}”

tags: role-timezone-set-localtime

`

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [February 10, 2015, 1:36am UTC](https://forum.ansible.com/t/synchronize-between-2-files-on-remote-host-uses-private-key/17466/2 "2015-02-10T01:36:58Z")

</div>

Hi Michael, have you tried setting the key option via the rsync\_opts and/or rsync\_path parameters to synchronize module?

---

<div class="post-metadata">

**Author:** ![Michael\_Spiegle](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@Michael\_Spiegle](https://forum.ansible.com/u/Michael_Spiegle)\
**Post date:** [February 10, 2015, 1:45am UTC](https://forum.ansible.com/t/synchronize-between-2-files-on-remote-host-uses-private-key/17466/3 "2015-02-10T01:45:43Z")

</div>

I looked at the module code and it seemed like it would automatically add the key if it was being used. I ended up running rsync directly using the command module and it seems to be working now.

Looking at the rsync\_opts argument, it seems like it will only append to the existing arguments, not change any existing arguments. Did this work for you?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [February 10, 2015, 1:53am UTC](https://forum.ansible.com/t/synchronize-between-2-files-on-remote-host-uses-private-key/17466/4 "2015-02-10T01:53:58Z")

</div>

Actually, looking through the code I believe you’re correct. Setting the option for -i again _might_ work (assuming the rsync command will override the setting with the second value, instead of throwing an error), however I think using the rsync command directly is what you want anyway, as you said the rsync is for a remote → remote copy. The synchronize module will only push or pull the file from or to the remote host and the Ansible controller normally - there is no option to do a completely remote rsync. I’m kind of surprised that using the delegate\_to option makes it kind of work this way at all.

---

<div class="post-metadata">

**Author:** ![jang](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@jang](https://forum.ansible.com/u/jang)\
**Post date:** [March 2, 2015, 10:41am UTC](https://forum.ansible.com/t/synchronize-between-2-files-on-remote-host-uses-private-key/17466/5 "2015-03-02T10:41:42Z")

</div>

Actually, I've just tripped over this. I was attempting to use synchronize  
to transfer files from the controller to a destination host. The  
synchronize task was invoked with a remote\_user: root, but run as a normal  
user on the controller.

What happens, it seems, is that ansible first attempts to make an ssh  
connection to root@localhost, to ship over the ansible module that'll  
perform the sync; then that runs - and fails, because the root user  
doesn't have the credentials available to make the conneciton to the  
destination machine.

I could understand this being reasonable behaviour to support delegate\_to,  
but is there a way to make synchronize simply invoke rsync directly in the  
common case (where the soruce is the ansible controller)?

(At the moment I have to use rsync via local\_action, which feels pretty  
ugly.)

Cheers,  
jan
