如何获得Ansible输出的特定值?

时间:2018-01-12 14:10:38

标签: ansible

我有以下剧本:

---
# file: access_token/tasks

- name: Verify if tenantname is provided
  fail: msg="Please provide the name of the tenant."
  when: tenantname is undefined

- name: Verify if tenantsecret is provided
  fail: msg="Please provide the application secret of the tenant."
  when: tenantsecret is undefined

- name: Send OAuth details
  shell: |
    curl -X POST \
    '{{mlp_uaa}}' \
    -H 'content-type: application/x-www-form-urlencoded' \
    -d 'client_id={{tenantname}}&client_secret={{tenantsecret}}' 
  register: token_details

- debug: var=token_details.stdout_lines

您能告诉我如何从以下输出中获取access_token的值:

TASK [access_token : debug] ****************************************************
ok: [127.0.0.1] => {
    "token_details.stdout_lines": [
        "{\"access_token\":\"eyJhbGciOiJSUjzI1NiIsImtpZCI6ImxlZ2FjeS10b2tlbi1rZXkiLCJ0eXAiOiJKV1QifQ.eyJqdGkiOiIxYmRmYjA5NmM2NDA0NWRmODMxYzZhZmVhNjJjMWY2NiIsInN1YiI6InRlc3RjbGllbnRhY2NlcHRhbmNlIiwiYXV0aG9yaXRpZXMiOlsidGVzdGNsaWVudGFjY2VwdGFuY2UiXSwic2NvcGUiOlsidGVzdGNsfsdfsaWVudGFjY2VwdGFuY2UiXSwiY2xpZW50X2lkIjoidGVzdGNsaWVudGFjY2VwdGFuY2UiLCJjaWQiOiJ0ZXN0fdsdY2xpZW50YWNjZXB0YW5jZSIsImF6cCI6InRlc3RjbGllbnRhY2NlcHRhbmNlIiwiZ3JhbnRfdHlwZSI6ImNsaWVudF9jcmVkZW50aWFscyIsInJldl9zaWciOiI1ZDI2NzE4MyIsImlhdCI6MTUxNTc2NTkzMywiZXhwIjoxNTE1NzY5NTMzLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwODAvdWFhL29hdXRoL3Rva2VuIiwiemlkIjoidWFhIiwiYXVkIjpbInRlc3RjbGllbnRhY2NlcHRhbmNlIl19.lfbpIhNtNR-SprHjhPodqNH5Nwc8d9rfblK-UBTNHyGbYMeoabR5A2w49iVdi-pVK5mu8G-1cj25onTR1xASgVQTCoEUu5T4IiEZ6MaNwOGfAKSaX1UweeXOS74FHspD5c-JiOgYSBsfj8WTaGiU6IRpG3tO43BsrNNCxsMcsuFZ0fRlGkZLdmyyxqJxaNd49t744LN2ncFc8Yh7M5vZjLa20rHcRHiAr17_AWzFcEezW4HN4sSpsyJ2EJs1gHc04LCOwS3CnWGya6vGBcXCdcoLN8SZLEwhbRZtR5xb39DVQFXzyFhk0OpImae-wHUzwmpIBcQKY0IYxcc6i3yAXw\",\"token_type\":\"bearer\",\"expires_in\":3599,\"scope\":\"testclientacceptance\",\"jti\":\"1bdfb096c64045df831c6afea62c1f66\"}"
    ]
}

1 个答案:

答案 0 :(得分:1)

token_details.stdout中有一个JSON字符串。您可以使用from_json过滤器将其转换为对象,然后按以下方式引用该键:

- debug:
    var: (token_details.stdout | from_json).access_token

请注意stdout而不是stdout_lines

而且我看不到任何会阻止您使用uri module而不是调用curl的内容。