Foudel
Foudel

Reputation: 277

How to get a specific value of Ansible's output?

I have the following playbook:

---
# 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

Could you please tell me how to get the value of the access_token from the below output:

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\"}"
    ]
}

Upvotes: 0

Views: 545

Answers (1)

techraf
techraf

Reputation: 68649

You have a JSON string in the token_details.stdout. You can use from_json filter to convert it to an object and then refer to the key in the following way:

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

Notice the stdout instead of stdout_lines.

And I can't see anything that would prevent you from using the uri module instead of calling curl.

Upvotes: 1

Related Questions