【问题标题】:Ansible synchronize module permissions issueAnsible 同步模块权限问题
【发布时间】:2017-07-28 11:13:09
【问题描述】:

远程服务器的“/home” enter image description here

远程服务器用户 1.比特纳米 2.take02 3.采取03 4.采取04

但本地主机只是ubuntu用户。

我想将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"

剧本结果是:

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 

但是, 失败的“cmd”可以在控制台上使用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

如何使用 sudo 运行“任务”?

ps。删除 become: yes 然后所有权限都是“ubuntu” enter image description here

【问题讨论】:

    标签: ansible


    【解决方案1】:

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

    另一方面,在第一个任务中,您以 root 身份在/tmp 下创建一个目录,因此权限仅限于 root 用户。因此,您会收到“permissions denied”错误。

    要么:

    • 重构代码,这样您就不需要本地目的地的root权限(或为任务"Creates directory"添加become: no),因为您使用暗示保留权限的存档选项,这可能不是一个选项;

    或:

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

    或:

    • 使用command 模块和sudo /usr/bin/rsync 作为调用。

    注意synchronize 模块是一个非标准模块,有changes in the past 与使用的帐户有关,还有requests for the changes


    最重要的是,该模块的当前文档非常混乱。一方面它强烈声明:

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

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

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

    因此,对于这个问题的案例,以下段落是相关的,即使它错误地说明了“src”:

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

    【讨论】:

    • 谢谢!!但是,我想将 REMOTE HOST 的“主”目录复制为 ansible,保留 OWNER 信息。远程服务器用户 1.bitnami 2.take02 3.take03 4.take04 但是本地Host只有ubuntu用户。
    • 是的,这正是问题所解释的。你希望我回复你的评论吗?
    猜你喜欢
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 2015-12-25
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 2017-11-30
    相关资源
    最近更新 更多