Pull value from stdout

Hi

I am for some reason, really struggling to pull an item from stdout and register it.

Inside play:

    • debug:
  1. msg: “{{ output.stdout }}”

Output:

  1. ok: [localhost] => {

  2. “msg”: {

  3. “code”: 200,

  4. “data”: “10.50.3.26”,

  5. “success”: true,

  6. “time”: 0.006

  7. }

  8. }

What I am looking to do is extract the ‘data’ item from the result.

I have tried {{ output.stdout[data] }} amongst other things but I just don’t seem to be able to get it going and Googling doesn’t seem to show what I’m after. I can only with with_items elements.

Can anyone advise?

Thanks

Hi,

I believe you are not registering the output before filtering the key i.e. data in your case. You can try following:

- hosts: localhost
  tasks:
  - name: run Test task
    register: output

  - debug:
      data: “{{ output[’data'] }}"

Hope it helps.

Regards,
Sumit

Hi Sumit,

The result is registered further up in the playbook which is where I’m getting the stdout values from.

But I’d to grab the IP address from the ‘data’ key in the debug msg and register that as a variable somehow to use elsewhere.

Thanks

Hi,

If you have already registered the output you can use “set_facts” module to filter the ipaddress from the output and then use the ipaddress and use in playbook as you want. For e.g.:

  • hosts: localhost
    tasks:
  • name: run Test task
    register: output
  • debug:
    msg: “{{ output[’data’] }}” # this will just output the IP address
  • name: get the data as ip
    set_fact:
    ip: “{{ output[‘data’] }}”
  • debug:
    msg: “{{ ip }}” # this will stdout the ip but can also be used as variable in play as well

Hope this will help.

Regards,
Sumit