- debug:
msg: "{{ result.stdout }}"
ok: [localhost] => {
"msg": {
"access_token": "xxxxxxxxxx",
"expires_in": 43200,
"token_type": "xxxxxx"
}
}
For example, given the file
cat /tmp/test.json
{
"access_token": "xxxxxxxxxx",
"expires_in": 43200,
"token_type": "xxxxxx"
}
The value of *result.stdout* is *AnsibleUnsafeText*. What you see
depends on the callback. The below play
cat pb.yml
- hosts: all
tasks:
- command: cat /tmp/test.json
register: result
- debug:
var: result.stdout
gives JSON with the default callback
ANSIBLE_STDOUT_CALLBACK=default ansible-playbook pb.yml
...
ok: [localhost] => {
"result.stdout": {
"access_token": "xxxxxxxxxx",
"expires_in": 43200,
"token_type": "xxxxxx"
}
}
The same task gives YAML with the yaml callback
ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook pb2.yml
...
ok: [localhost] =>
result.stdout:
access_token: xxxxxxxxxx
expires_in: 43200
token_type: xxxxxx
You can test the type of the attribute *stdout*
- debug:
var: result.stdout|type_debug
gives
result.stdout|type_debug: AnsibleUnsafeText
filter the access token alone
Convert the text to dictionary and get the *access_token* as well.
It's up to you where you put the vars
vars:
test_data: "{{ result.stdout|from_json }}"
access_token: "{{ test_data.access_token }}"
Example of a complete playbook for testing
cat pb.yml
- hosts: all
vars:
test_data: "{{ result.stdout|from_json }}"
access_token: "{{ test_data.access_token }}"
tasks:
- command: cat /tmp/test.json
register: result
- debug:
var: result.stdout
- debug:
var: result.stdout|type_debug
- debug:
var: test_data|type_debug
- debug:
var: test_data
- debug:
var: access_token