Ansible Split JSON Return

Have the Following Playbook Which Returns the following

ok: [localhost] => {
“mdb.json”: [
{
“mac-address”: “xx:xx:xx:xx:xx”
}
]
}

Something like this would work I believe:

“{{ mdb.json.split(‘:’)[1:6] | join(‘:’) }}”

Also, could you not just reference it directly by using “{{ mdb.json.mac-address }}”?

When i do Both

“{{ mdb.json.mac-address }}”

“{{ mdb.json.split(‘:’)[1:6] | join(‘:’) }}”

It returns

fatal: [localhost]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘mac’\n\nThe error appears to be in ‘/srv/Ansible/mdb.yaml’: line 8, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: mdb\n - debug:\n ^ here\n”}

fatal: [localhost]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘split’\n\nThe error appears to be in ‘/srv/Ansible/mdb.yaml’: line 8, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n register: mdb\n - debug:\n ^ here\n”}

What’s your plabook look like? You’ll probably have to loop through your JSON results as it looks like it’s a list.

Ok, try this then:

  • debug:
    msg: “{{ item.mac-address }}”
    loop: “{{ mdb.json }}”

Have the Following Playbook Which Returns the following

ok: [localhost] => {
    "mdb.json": [

The [ is telling us that mdb.json is a list, the fist element in a list i 0.

        {
            "mac-address": "xx:xx:xx:xx:xx"
        }
    ]
}

<snip />

  - debug:
      msg: "{{ mdb.json.split(':') }}"

What i would like to Return is just

"msg": "xx:xx:xx:xx:xx"

That would be {{ mdb.json.0['mac-address'] }}

because of the dash in mac-address you need to use notation and not the dot notation on that one.

Thanks Kai,

that worked i was also able to Split(‘-’)[0] and that only outputted the xxx:xxx:xxx:xx just what i was looking for.