Ansible同步模块权限问题

时间:2017-03-08 02:36:06

标签: ansible

远程服务器" / home" enter image description here

远程服务器用户  bitnami  2. take02  3. take03  4. take04

但本地主机只是ubuntu用户。

我想复制" home" REMOTE HOST目录为ansible, 保留OWNER信息。

这是我的剧本:

---
- 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结果是:

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' \"bitnami@54.236.34.197:/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 

但是, 失败&#34; cmd&#34;在控制台上使用sudo运行良好。

$ 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' bitnami@54.236.34.197:/home/ /tmp/discovery_bitnami/home

我如何运行&#34;任务&#34;与sudo?

PS。删除become: yes然后所有权限都是&#34; ubuntu&#34; enter image description here

1 个答案:

答案 0 :(得分:2)

我猜你没有synchronize模块的选项。它在本地运行时没有sudoit's hardcoded

另一方面,在第一个任务中,您以/tmp为根目录创建目录,因此权限仅限于root用户。因此,您将获得&#34; 权限被拒绝&#34;错误。

或者:

  • 重构代码,以便您不需要本地目标的root权限(或为任务become: no添加"Creates directory"),因为您使用的存档选项意味着权限保留,可能不是一个选择;

或:

  • 创建您自己的synchronize模块版本,并将sudo添加到cmd变量的前面;

或:

  • 使用command模块与sudo /usr/bin/rsync作为通话。

请注意synchronize模块是非标准模块,有changes in the past项使用的帐户,requests for the changes

除此之外,该模块的当前文档非常混乱。一方面,它强烈指出:

  

同步目标的用户和权限是目标主机上remote_user的用户和权限,如果become_user处于活动状态,则为become=yes

但在另一个地方,它只暗示使用pull模式时源和目的意义相反:

  

在拉模式下,上下文中的远程主机是源。

因此对于这个问题的情况,以下段落是相关的,即使它错误地陈述了&#34; src&#34;:

  

同步src的用户和权限是在本地主机上运行Ansible任务的用户的权限(或者当使用delegate_to时用于delegate_to主机的remote_user)。