0

I've a task in ansible where I've to extract value of token from a json object, which is as follows:

{
"token.stdout_lines": [
    "{",
    "\t\"id\": \"08320829d85c7000\",",
    "\t\"description\": \"\",",
    "\t\"token\": \"zMiyCw7X6u_IjBpTbD1Nvt4eGk-dxBXWWOqRCgWh_KiYtp7AjD5mML5mBIEtApncBSXwU3QqexT_4VVmEv0WeA==\",",
    "\t\"status\": \"active\",",
    "\t\"userName\": \"telegraf\",",
    "\t\"userID\": \"0831cb0c68dc7000\",",
    "\t\"permissions\": [",
    "\t\t\"read:orgs/ea37b04111f50748/buckets\",",
    "\t\t\"write:orgs/ea37b04111f50748/buckets\"",
    "\t]",
    "}"
]
}

In order to retrieve the value of the token, I followed these steps:

- set_fact:
myvalue: "{{ token.stdout_lines | regex_search('regexp')}}"
  vars:
regexp: 'token([^"]+)":([^"]+)"([^.{\\}$"]+)'
- debug:
var: myvalue

The output I get is a null value in myvalue.

Can anyone point to where I am making a mistake?

Many thanks

Mamoona A
  • 3
  • 1

2 Answers2

0

Not quite sure if regexp is the best answer in this case.

Why not do just something like

- set_fact:
    myvalue: "{{ token.stdout_lines | from_json }}"
- debug:
    var: myvalue.token

See more on Ansible's website.

boky
  • 815
  • 5
  • 10
0

I would try to remove unnecessary groups. Regex: token[^"]+":[^"]+"([^.{\\}$"]+)

Example

Amerousful
  • 2,292
  • 1
  • 12
  • 26