2017-02-14 77 views
0

我試圖使用ansible(版本2.1.2.0)在我們的服務器網絡上創建命名的ssh訪問。從跳箱子我創建一組用戶運行ansible並創建一個私有/公共密鑰對用戶模塊Ansible授權密鑰模塊無法讀取公鑰

- user: 
     name: "{{ item }}" 
     shell: /bin/bash 
     group: usergroup 
     generate_ssh_key: yes 
     ssh_key_comment: "ansible-generated for {{ item }}" 
    with_items: "{{ list_of_usernames }}" 

從那裏,我想整個公鑰複製到authorized_users上的每個文件遠程服務器。我使用的是authorized_key_module獲取和複製用戶生成的公鑰作爲遠程服務器上的authorized_key:

- authorized_key: 
     user: "{{ item }}" 
     state: present 
     key: "{{ lookup('file', '/home/{{ item }}/.ssh/id_rsa.pub') }}" 
    with_items: 
     - "{{ list_of_usernames }}" 

什麼我發現是因爲它是無法訪問文件酒館查找將失敗在用戶的.ssh文件夾中。如果我以root用戶身份運行ansible(對我而言,這不是我願意接受的選項),它將會愉快地運行。

fatal: [<ipAddress>]: FAILED! => {"failed": true, "msg": "{{ lookup('file', '/home/testuser/.ssh/id_rsa.pub') }}: the file_name '/home/testuser/.ssh/id_rsa.pub' does not exist, or is not readable"} 

有沒有更好的方式來做到這一點,而不是以root身份運行ansible?

編輯:對不起,我忘了提,我與運行成爲:是

編輯#2:下面是什麼,我試圖運行,在這種情況下課程的壓縮劇本是」會的authorized_key添加到本地主機,但顯示了我面對錯誤:

--- 
- hosts: all 
    become: yes 
    vars: 
    active_users: ['user1','user2'] 
    tasks: 

    - group: 
     name: "users" 

    - user: 
     name: "{{ item }}" 
     shell: /bin/bash 
     group: users 
     generate_ssh_key: yes 
    with_items: "{{ active_users }}" 

    - authorized_key: 
     user: "{{ item }}" 
     state: present 
     key: "{{ lookup('file', '/home/{{ item }}/.ssh/id_rsa.pub') }}" 
    become: yes 
    become_user: "{{ item }}" 
    with_items: 
     - "{{ active_users }}" 
+0

但是,您將如何創建沒有root權限的新用戶? –

+0

對不起,我在寫這個問題時曾想過寫這個,我的確在使用成爲:是的 – Aeladru

+0

爲什麼你不能只爲下一個任務做'成爲:是'? –

回答

2

好的,問題是查找插件。
它在具有運行ansible-playbookbecome: yes的用戶權限的安全控制主機上執行,不會提升插件的權限。

爲了克服這個問題,user任務捕獲結果,並使用其輸出在進一步的任務:

- user: 
    name: "{{ item }}" 
    shell: /bin/bash 
    group: docker 
    generate_ssh_key: yes 
    ssh_key_comment: "ansible-generated for {{ item }}" 
    with_items: 
    - ansible5 
    - ansible6 
    register: new_users 
    become: yes 

- debug: msg="user {{ item.item }} pubkey {{ item.ssh_public_key }}" 
    with_items: "{{ new_users.results }}" 

雖然你需要委派一些這方面的任務,這個想法是一樣的。

+0

謝謝,我感覺這是關於查找權限的問題,但對於ansible來說是相當新的,所以無法提供這樣的東西。非常感謝 – Aeladru

2

大多數Linux/UNIX機器上只有兩個帳戶訪問/home/testuser/.ssh/id_rsa.pubroottestuser,所以如果你想修改這些文件您需要是roottestuser

您可以使用privilige escalation使用become。你說你不想以root身份運行,這非常好。你沒有提到你正在運行的用戶是否具有sudo訪問權限。如果是這樣,並且使用sudo是罰款你,那麼你可以簡單地做:

- authorized_key: 
     user: "{{ item }}" 
     state: present 
     key: "{{ lookup('file', '/home/{{ item }}/.ssh/id_rsa.pub') }}" 
    with_items: 
     - "{{ list_of_usernames }}" 
    become: yes 

這在默認情況下會使用sudo運行這些命令,作爲根,同時保持由非root用戶運行的剩餘任務。這也是用合理的方式進行運行的首選方式。

如果你不想要這個,並且你想保持你的用戶儘可能沒有root權限,那麼你需要運行命令testuser(和其他你想修改的用戶)。這意味着您仍然需要修補sudoers文件以允許您的用戶轉換成這些用戶中的任何一個(這也可能會導致一些安全問題 - 儘管不如以root身份運行任何內容)你可以這樣做:

- authorized_key: 
     user: "{{ item }}" 
     state: present 
     key: "{{ lookup('file', '/home/{{ item }}/.ssh/id_rsa.pub') }}" 
    with_items: 
     - "{{ list_of_usernames }}" 
    become: yes 
    become_user: "{{ item }}" 

有一些注意事項使用這種方法,所以你可能要嘗試此之前,閱讀完整Privilege Escalation頁ansible的文檔,尤其是在Unpriviliged Users部分。

+0

感謝您的詳細回覆,我會再次瀏覽priviliges文檔以確保我已經確定了它。以root身份執行某些操作似乎有點奇怪,特別是在嘗試設置命名訪問以防止需要以root身份登錄時。 – Aeladru