On Sat, 14 Oct 2023 01:01:28 -0700 (PDT)
Veera <sveem...@gmail.com> wrote:

> - debug:
>         msg: "{{ result.stdout }}"
> 
> ok: [localhost] => {
>     "msg": {
>         "access_token": "xxxxxxxxxx",
>         "expires_in": 43200,
>         "token_type": "xxxxxx"
>     }
> }

For example, given the file

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

shell> cat pb.yml
- hosts: all
  tasks:
    - command: cat /tmp/test.json
      register: result
    - debug:
        var: result.stdout

gives JSON with the default callback

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

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

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


-- 
Vladimir Botka

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20231014172804.3f085226%40gmail.com.

Attachment: pgpJQrJn3h_61.pgp
Description: OpenPGP digital signature

Reply via email to