Ansible Split JSON Return

601 views
Skip to first unread message

David Foley

unread,
Mar 5, 2020, 9:52:52 AM3/5/20
to Ansible Project
Have the Following Playbook Which Returns the following

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


--
- hosts: localhost
  gather_facts: false
  tasks:
  - name: Getting Mac Address 
    uri: url="http://.com?field=mac-address" return_content=yes
    register: mdb
  - debug:
      msg: "{{ mdb.json.split(':') }}"

What i would like to Return is just 

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

But Split doesn't seem to work 

Cade Lambert

unread,
Mar 5, 2020, 10:28:54 AM3/5/20
to Ansible Project
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 }}"?

David Foley

unread,
Mar 5, 2020, 10:34:40 AM3/5/20
to Ansible Project
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"}


Cade Lambert

unread,
Mar 5, 2020, 10:39:34 AM3/5/20
to Ansible Project
What's your plabook look like? You'll probably have to loop through your JSON results as it looks like it's a list.

David Foley

unread,
Mar 5, 2020, 10:45:03 AM3/5/20
to Ansible Project
-
- hosts: localhost
  gather_facts: false
  tasks:
  - name: Getting Mac Address 
    uri: 
      url: http://.com 
      return_content: yes

Cade Lambert

unread,
Mar 5, 2020, 10:55:36 AM3/5/20
to Ansible Project
Ok, try this then:

- debug:
      msg: "{{ item.mac-address }}"
  loop: "{{ mdb.json }}"

Kai Stian Olstad

unread,
Mar 5, 2020, 3:48:12 PM3/5/20
to ansible...@googlegroups.com
On 05.03.2020 15:52, David Foley wrote:
> 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.


--
Kai Stian Olstad

David Foley

unread,
Mar 6, 2020, 8:38:03 AM3/6/20
to Ansible Project
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.  
Reply all
Reply to author
Forward
0 new messages