0

Remote Server's "/home" enter image description here

Remote Server User 1. bitnami 2. take02 3. take03 4. take04

But local Host are only ubuntu users.

I would like to copy the "home" directory of REMOTE HOST as ansible, keeping the OWNER information.

This is my playbook:

---
- hosts: discovery_bitnami
  gather_facts: no
  become: yes

  tasks:
    - name: "Creates directory"
      local_action: >
        file path=/tmp/{{ inventory_hostname }}/home/ state=directory

    - name: "remote-to-local sync test"
      become_method: sudo
      synchronize:
        mode: pull
        src: /home/
        dest: /tmp/{{ inventory_hostname }}/home
        rsync_path: "sudo rsync"

Playbook result is:

PLAY [discovery_bitnami] *******************************************************

TASK [Creates directory] *******************************************************
ok: [discovery_bitnami -> localhost]

TASK [remote-to-local sync test] ***********************************************
fatal: [discovery_bitnami]: FAILED! => {"changed": false, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/ubuntu/.ssh/red_LightsailDefaultPrivateKey.pem -S none -o StrictHostKeyChecking=no -o Port=22' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"[email protected]:/home/\" \"/tmp/discovery_bitnami/home\"", "failed": true, "msg": "rsync: failed to set times on \"/tmp/discovery_bitnami/home/.\": Operation not permitted (1)\nrsync: recv_generator: mkdir \"/tmp/discovery_bitnami/home/bitnami\" failed: Permission denied (13)\n*** Skipping any contents from this failed directory ***\nrsync: recv_generator: mkdir \"/tmp/discovery_bitnami/home/take02\" failed: Permission denied (13)\n*** Skipping any contents from this failed directory ***\nrsync: recv_generator: mkdir \"/tmp/discovery_bitnami/home/take03\" failed: Permission denied (13)\n*** Skipping any contents from this failed directory ***\nrsync: recv_generator: mkdir \"/tmp/discovery_bitnami/home/take04\" failed: Permission denied (13)\n*** Skipping any contents from this failed directory ***\nrsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1655) [generator=3.1.1]\n", "rc": 23}
    to retry, use: --limit @/home/ubuntu/work/esc_discovery/ansible_test/ansible_sync_test.retry

PLAY RECAP *********************************************************************
discovery_bitnami          : ok=1    changed=0    unreachable=0    failed=1 

But, failed "cmd" works fine run with sudo on the console.

$ sudo /usr/bin/rsync --delay-updates -F --compress --archive --rsh 'ssh -i /home/ubuntu/.ssh/red_PrivateKey.pem -S none -o StrictHostKeyChecking=no -o Port=22' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' [email protected]:/home/ /tmp/discovery_bitnami/home

How do I run "task" with sudo?

ps. remove become: yes then all permission is "ubuntu" enter image description here

1 Answer 1

4

I guess you are out of options for the synchronize module. It runs locally without sudo and it's hardcoded.

On the other hand, in the first task you create a directory under /tmp as root, so the permissions are limited to the root user. As a result you get "permissions denied" error.

Either:

  • refactor the code so that you don't need root permissions for the local destination (or add become: no for the task "Creates directory"), as you use archive option which implies permissions preservation, this might not be an option;

or:

  • create your own version of the synchronize module and add sudo to the front of the cmd variable;

or:

  • use the command module with sudo /usr/bin/rsync as the call.

Mind that synchronize module is a non-standard one, there were changes in the past regarding the accounts used, and requests for the changes.


On top of everything, the current documentation for the module is pretty confusing. On one hand it states strongly:

The user and permissions for the synchronize dest are those of the remote_user on the destination host or the become_user if become=yes is active.

But in another place it only hints that the source and destination meaning is reversed when using pull mode:

In pull mode the remote host in context is the source.

So for the case from this question, the following passage is relevant, even though it incorrectly states the "src":

The user and permissions for the synchronize src are those of the user running the Ansible task on the local host (or the remote_user for a delegate_to host when delegate_to is used).

2
  • Thank you!! but, I would like to copy the "home" directory of REMOTE HOST as ansible, keeping the OWNER information. Remote Server User 1. bitnami 2. take02 3. take03 4. take04 But local Host are only ubuntu users. Mar 8, 2017 at 5:16
  • Yes, that's exactly what the question explained. Do you expect me to respond to your comment?
    – techraf
    Mar 8, 2017 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.