Synchronize module fails with become
ansible
Solution
`synchronize` module is executed locally on the control machine (not on the target node) and by default it connects to the nodes with the account (SSH credentials) used there (currently logged-in one, or defined in `~/.ssh/config`).
First, you need to override the default behaviour by adding the `use_ssh_args` parameter for the `ansible_ssh_*` values to be considered when connecting:
`use_ssh_args` Use the ssh_args specified in `ansible.cfg`
Second, because the task is run locally, `become` directive is also effective only on the control machine. `ansible_become_*` variables do not apply in this situation.
Instead, you can tell the module to run `sudo rsync` to elevate the permissions on the target node through the `rsync_path` parameter:
- name: Copy and unarchive webapps node.
synchronize:
src: /home/ansible/templates/app/Sprint6/webapps
dest: /opt/msdp/ca/app checksum=yes
use_ssh_args: yes
rsync_path: "sudo -u UserB rsync"
Finally, the above assumes sudoers settings allowing passwordless sudo. If you really need to provide a password in plain text, you'd need another workaround in the like of:
rsync_path: "echo {{ UserA_password }} | sudo -S -u UserB rsync"
Problem
I am trying to use the synchronize module to copy a file from an Ansible node to a remote one. I want these files to exist as `UserB` on the remote nodes but I do not have access to `UserB` directly. Instead `UserA` has sudo privileges to switch to `UserB`. So I'm logging in as `UserA`. My environment file says: ``` ansible_ssh_user=UserA ansible_ssh_pass=<PassUserA> ansible_become_method=sudo ansible_become_user=UserB ansible_become_pass=<PassUserA> ``` My task is: ``` - name: Copy and unarchive webapps node. synchronize: src=/home/ansible/templates/app/Sprint6/webapps dest=/opt/msdp/ca/app checksum=yes become: yes ``` But when I run the playbook, I get an error: ``` fatal: [5.232.57.247]: FAILED! => {"changed": false, "cmd": "/usr/bin/rsync --delay-updates -F --compress --checksum --archive --rsh 'ssh -S none -o StrictHostKeyChecking=no -o ControlMaster=auto -o ControlPersist=60s' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/home/ansible/templates/app/Sprint6/webapps\" \"UserA@5.232.57.247:/opt/msdp/ca/app\"", "failed": true, "msg": "sudo: sorry, you must have a tty to run sudo\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]\n", "rc": 12} ``` On the remote node, only `UserB` can write under: `/opt/msdp/ca/app` Is there a config that I am missing?