0

背景,Ansible運行一個命令了許多當地的循環

我試圖創建一個循環,對散列迭代從qa.yml文件和列表中的每個用戶它試圖找到一個讀文件在本地服務器(公鑰)上,一旦找到文件,它將在遠程機器上創建用戶並將公鑰複製到遠程機器上的authorized_key。

我試圖以迭代的方式實現它,所以爲了更新鍵或添加更多的用戶鍵我需要更改.yml列表並將公鑰文件放在適當的位置。但是我無法獲得local_action +查找工作。

--- 
- hosts: tag_Ansible_CLOUD_QA 

    vars_files: 
    - ../users/qa.yml 
    - ../users/groups.yml 
    remote_user: ec2-user 
    sudo: yes 

    tasks: 

    - name: Create groups 
    group: name="{{ item.key }}" state=present 
    with_dict: "{{ user_groups }}" 

    - name: Create remote users QA 
    user: name="{{ item.key }}" comment="user" group=users groups="qa" 
    with_dict: "{{ qa_users }}" 

    - name: Erase previous authorized keys QA 
    shell: rm -rf /home/"{{ item.key }}"/.ssh/authorized_keys 
    with_dict: "{{ qa_users }}" 

    - name: Add public keys to remote users QA 
    local_action: 
     find: paths="{{'/opt/pubkeys/2016/q2/'}}" patterns="{{ item.key }}" 
     register: result 
    authorized_key: user="{{ item.key }}" key="{{ lookup('file', result) }}" 
    with_dict: "{{ qa_users }}" 

哈希:

qa_users: 
    user1: 
    name: User 1 
    user2: 
    name: User 2 

回答

1

你惡補兩個任務成一個單一的任務項在這最後的任務,以便Ansible是不會這樣的。

適當拆分任務應該工作:

- name: Find keys 
    local_action: find paths="{{'/opt/pubkeys/2016/q2/'}}" patterns="{{ item.key }}" 
    register: result 
    with_dict: "{{ qa_users }}" 

    - name: Add public keys to remote users QA 
    authorized_key: user="{{ item.0.key }}" key="{{ lookup('file', item.1.stdout) }}" 
    with_together: 
     - "{{ qa_users }}" 
     - result 

然後,第二任務循環通過字典和從使用with_together循環,在兩個數據結構,在步驟前進前一個任務的結果。

但是,這看起來不是理想的解決問題的方法。

如果你看看這裏你的任務正在嘗試做的,你可以更簡單地像這樣的東西替代它:

- name: Add public keys to remote users QA 
    authorized_key: user="{{ item.key }}" key="{{ lookup('file', '/opt/pubkeys/2016/q2/' + item.key) }}" 
    with_dict: 
     - "{{ qa_users }}" 

您也可以刪除THID任務,你通過清除下來的用戶先前的鑰匙簡單地使用authorized_keys模塊的exclusive參數:

- name: Add public keys to remote users QA 
    authorized_key: user="{{ item.key }}" key="{{ lookup('file', '/opt/pubkeys/2016/q2/' + item.key) }}" exclusive=yes 
    with_dict: 
     - "{{ qa_users }}" 

此外,它可能是你試圖簡化在一種奇怪的方式對這個問題,但您使用的是數據結構的東西的情況下是不太理想的權利ñ所以我會看看,如果這真的是他們的樣子。

+0

感謝您的反饋意見和建議,我可能會跟隨它,但是我很好奇,爲什麼我堅持跑步,local_action時收到此錯誤: - 名稱:查找密鑰,錯誤:加載YAML腳本時的語法錯誤,credentials/distribute-public-key.yml 注意:錯誤可能實際出現在此位置之前:第41行,第17列 - 名稱:查找密鑰 local_action: ^ –

+0

@DmitryR你可能想現在嘗試我的編輯。沒有發現local_action附近的語法問題。它仍然沒有經過測試,所以可能仍然會失敗。 – ydaetskcoR

-1

謝謝@ydaetskcoR分享正確的方法,以下解決方案給我的一切動態公共密鑰分配,當位於本地計算機上,並置備於遠程EC2計算機文件:

--- 
- hosts: tag_Ansible_CLOUD_QA 

    vars_files: 
    - ../users/groups.yml 
    - ../users/qa.yml 
    remote_user: ec2-user 
    become: yes 
    become_method: sudo 

    tasks: 
    - name: Find user matching key files 
    become_user: jenkins 
    local_action: find paths="{{'/opt/pubkeys/2016/q1/'}}" patterns="{{ '*' + item.key + '*' }}" 
    register: pub_key_files 
    with_dict: "{{ qa_users }}" 

    - name: Create groups 
    group: name="{{ item.key }}" state=present 
    with_dict: "{{ user_groups }}" 

    - name: Allow test users to have passwordless sudo 
    lineinfile: "dest=/etc/sudoers state=present regexp='^%{{ item.key }} ALL=.*ALL.* NOPASSWD: ALL' line='%{{ item.key }} ALL=(ALL) NOPASSWD: ALL'" 
    with_dict: "{{ user_groups }}" 

    - name: Create remote users qa 
    user: name="{{ item.key }}" comment="user" group=users groups="qa" 
    with_dict: "{{ qa_users }}" 

    - name: Add public keys to remote users qa 
    #debug: "msg={{ 'User:' + item.item.key + ' KeyFile:' + item.files.0.path }}" 
    authorized_key: user="{{ item.item.key }}" key="{{ lookup('file', item.files.0.path) }}" exclusive=yes 
    with_items: "{{ pub_key_files.results }}" 

這是命令基於EC2標記線,以獲得動態庫存:

ansible-playbook -i inventory/ec2.py --private-key <path to your key file> --extra-vars '{"QUATER":"q1"}' credentials/distribute-public-key.yml 
+0

你不應該回答問題...你的答案 – confiq

+0

評論@confiq有一個按鈕,回答你的問題,我使用,也可以在直接粘貼,因爲字符的數量是有限的徵求意見,並沒有行分離代碼高亮,我認爲任何人誰找這樣的解決方案將會理解解決這個問題的答案。 –