Deano
Deano

Reputation: 12190

Ansible parse JSON with YAML

I'm trying to assign a variable to match IP address shown in API call I'm making to an online service provider.

Here is the JSON data I'm receiving:

TASK [manager : debug] *********************************************************
ok: [localhost] => {
    "msg": [
        {
            "address": "10.0.3.224",
            "family": "inet",
            "netmask": "24",
            "scope": "global"
        },
        {
            "address": "fe80::216:3eff:feb2:7330",
            "family": "inet6",
            "netmask": "64",
            "scope": "link"
        }
    ]
}

How can I go about parsing the first address output and assign its value to a variable in YAML

this is what I have tried

- debug: msg={{ output.stdout|from_json }} 

but I'm unable to get the IP address.

Upvotes: 9

Views: 12659

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

Try: msg={{ (output.stdout | from_json | first).address }}

Upvotes: 12

Related Questions