2017-06-29 97 views
0

我遇到了一個奇怪的問題與可靠,我敢肯定它只是由於我缺乏經驗,因爲我相對較新的ansible(我一直只與它合作幾個星期)Ansible json_query添加額外的字符到字典值

因此,簡而言之,我正在嘗試使用命令模塊來運行AWS CLI命令,以列出用戶的AWS訪問密鑰,然後從該用戶中刪除它們。我使用CLI而不是IAM模塊的原因是因爲我相信IAM模塊在刪除訪問密鑰方面存在一個缺陷。即使當我指定狀態更新和訪問鍵去除和訪問鍵狀態時,刪除它仍然不會刪除訪問鍵,或者當我將訪問鍵狀態設置爲非活動狀態時使它們不活動。

的第一個任務列出了給定用戶的訪問密鑰和寄存器的輸出:

- name: List the access keys (if any) of the user(s) we just created 
    vars: 
    use_key: "{{ enable_access_keys }}" 
    command: "aws iam list-access-keys --user-name {{ item }}" 
    with_items: 
    - "{{ iam_user_name_list }}" 
    when: not use_key 
    register: list_key_output 

^請記住,iam_user_name_list僅包含在目前1個用戶,這就是爲什麼我訪問結果我的方式。我知道這需要在未來改變。

由於從list_key_output標準輸出看起來像這樣

"stdout": "{\n \"AccessKeyMetadata\": [\n  {\n   \"UserName\": \"other-guy\", \n   \"Status\": \"Active\", \n   \"CreateDate\": \"2017-06-29T18:45:04Z\", \n   \"AccessKeyId\": \"removed\"\n  }\n ]\n}", 

我調試味精的標準輸出,並且註冊一個變量測試給它沒有斜線和換行符正確的JSON格式,所以我可以使用json_query得到鍵從標準輸出。我正在使用json查詢,因爲無論出於何種原因,AccessKeyId都不會被識別爲AccessKeyMetadata字典的關鍵字。

- name: list keys stdout 
    debug: 
    msg: "{{ list_key_output.results[0].stdout }}" 
    register: test 

- name: test variable output 
    debug: 
    msg: "{{ test.msg.AccessKeyMetadata | json_query('[].AccessKeyId') }}" 

在這一點上,我成功地從標準輸出

ok: [127.0.0.1] => { 
    "changed": false, 
    "msg": [ 
     "correct access key here" 
    ] 
} 

越來越快捷鍵現在,我喂訪問鍵刪除CLI命令,像這樣

- name: Remove any access keys our new console user might have 
    vars: 
    use_key: "{{ enable_access_keys }}" 
    command: "aws iam delete-access-key --access-key {{ test.msg.AccessKeyMetadata | json_query('[].AccessKeyId') }} --user-name other-guy" 
    when: not use_key 
    register: delete_key_output 

這任務由於提供了無效的訪問密鑰而失敗。

fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": ["aws", "iam", "delete-access-key", "--access-key", "[u********************]", "--user-name", "other-guy"], "delta": "0:00:00.388902", "end": "2017-06-29 18:59:13.308230", "failed": true, "rc": 255, "start": "2017-06-29 18:59:12.919328", "stderr": "\nAn error occurred (ValidationError) when calling the DeleteAccessKey operation: The specified value for accessKeyId is invalid. It must contain only alphanumeric characters.", "stderr_lines": ["", "An error occurred (ValidationError) when calling the DeleteAccessKey operation: The specified value for accessKeyId is invalid. It must contain only alphanumeric characters."], "stdout": "", "stdout_lines": []} 

正如你可以看到,當我通過訪問密鑰的命令,[u被預置到所述訪問密鑰的前部和]被附加到它的背面。

這是怎麼發生的?我怎樣才能實現我的目標沒有3個字符添加到訪問鍵使其無效?我不明白爲什麼會發生這種情況,因爲當我調試訪問密鑰的方式與我提供給命令相同的方式時,它只顯示沒有[u在前面和後面]的訪問密鑰。

對不起,很長的文章,但我覺得我真的不得不描述的情況能夠得到幫助。預先感謝任何答案!

回答

0

要回答你的問題相關:

ok: [127.0.0.1] => { 
    "changed": false, 
    "msg": [ 
     "correct access key here" 
    ] 
} 

注意在msg[] - 這意味着你打印一份列表,包含一個元素 - correct access key here字符串。

當您嘗試將列表轉換爲字符串時,您會得到它的Python解釋[u'correct access key here']

你需要得到一個列表的第一個元素:

{{ test.msg.AccessKeyMetadata | json_query('[].AccessKeyId') | first }} 

附:但從我的角度來看,你的方式是錯誤的。嘗試解決您的問題與IAM模塊。

+0

謝謝。你是對的,我可以告訴它,我正在走的方式變得非常複雜。事實證明,我的問題與iam模塊是,即使文檔說刪除是一個選項,你只能做access_key_state活動/不活動。另外,出於好奇,整個列表的過濾器是什麼?所有? – astrade

+0

@astrade抱歉,不確定您的意思是「過濾整個列表」。 –