Is there any way to get parsed data from stdout of each task on playbook?

Hi,

I am a newbie in this wonderful tool Ansible.
I made a playbook script to test it.

But I found that sometimes I need to parse the stdout of each task.

For instance

  • name : run and get the result in JSON format on stdout
    shell : “/usr/local/something”
    register : result

Let’s see result.stdout

{u’name’:u’test1’, u’ipaddress’:‘xxx.xxx.xxx.xx’, u’pass’:‘asdfasfd’}

Can I extract these three elements(name, ipaddress, pass) from stdout and use it on another task?

I think lot of users experienced this issue because the return values(stdout) of tools are not simple sometimes

Even if I have a plan to make another Module I think it’s tough because I should pass the three elements.
(Unfortunately I am not good at Python yet)

Thanks in advance!

-Justin

You should be able to use that json by doing something like:

  • debug: msg=“{{ (result.stdout|from_json).ipaddress }}”

If you wanted to try to turn the whole thing into an accessible data type, you could probably use set_fact:

  • set_fact:
    some_new_var: “{{ result.stdout|from_json }}”

Sort of. set_fact doesn’t have anything to coerce string inputs and will result in you storing a string.

However, you can use {{ result.stdout | from_json }} anywhere you want the datastructure.

Hi,

This was helpful.

Meanwhile, I am trying to extract “PRETTY NAME” in a loop from below output. What should I use?

Can you look into this ?

You can use some thing like this.

  • set_fact:
    Name: (result.stdout | from_json).name
    Ipaddress: similar to the above
(attachments)

Thank you for the time.

I tried the Ipaddess parttern for my case. Also, tried the case suggested by you. But did not receive positive results.

Below is the snip of my code and the output respectively.

U can use system-release instead of os-release if you are just trying find the os name and version from all hosts.

This should return something like this.

(attachments)