2017-03-08 34 views
0

遠程服務器的 「/家」 enter image description hereAnsible同步模塊權限問題

遠程服務器用戶 1. bitnami 2. take02 3. take03 4. take04

但本地主機只Ubuntu用戶。

我想複製REMOTE HOST的「home」目錄作爲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' \"[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 

但是, 失敗 「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' [email protected]:/home/ /tmp/discovery_bitnami/home 

如何使用sudo運行「任務」?

ps。刪除become: yes然後所有權限是「ubuntu」 enter image description here

回答

2

我想你已經沒有synchronize模塊的選項。它在本地運行,沒有sudoit's hardcoded

另一方面,在第一個任務中,您以root用戶身份在/tmp下創建目錄,因此權限僅限於root用戶。因此,您會收到「權限被拒絕」錯誤。

或者:

  • 重構代碼,這樣你就不需要爲本地目的root權限(或添加become: no的任務"Creates directory"),你用這意味着權限保存存檔選項,這可能不是一個選擇;

或:

  • 創建自己的synchronize模塊的版本,並添加sudocmd變量前面;

或:

  • 使用command模塊sudo /usr/bin/rsync作爲呼叫。

記住synchronize模塊是一個非標準的一個,有changes in the past關於所使用的賬戶,並requests for the changes


最重要的是,模塊的當前文檔相當混亂。一方面,它強烈地指出:

的使用者和同步DEST權限是那些remote_user在目標主機上或become_user如果become=yes是積極的。

但在另一個地方,它只是暗示,源和目的意義是使用pull模式時逆轉:

在拉模式上下文中的遠程主機源。

所以從這個問題的情況下,下面這段話是相關的,儘管它錯誤地指出「SRC」:

的同步SRC的用戶和權限都是用戶運行本地主機上的Ansible任務(或使用delegate_to時的delegate_to主機的remote_user)。

+0

謝謝! 但是, 我想將REMOTE HOST的「home」目錄複製爲以保留OWNER信息。 遠程服務器用戶 1. bitnami 2. take02 3. take03 4. take04 但本地主機只有Ubuntu用戶。 –

+0

是的,這正是問題所解釋的。你希望我回應你的評論嗎? – techraf

+0

謝謝你......你的評論!!! –