Dheeraj
Dheeraj

Reputation: 347

Ansible extract value from JSON output and use it in another tasks

Using the below task I want to extract the authresponse signature value:

- uri:
   url: "http://10.25.155.x/axapi/v3/auth"
   method: POST
   validate_certs: no
   follow_redirects: all
   body: { "credentials": { "username": "admin","password": "a10" } } 
   return_content: yes
   body_format: json
  register: "a10"

Output:

"json": {
    "authresponse": {
        "description": "the signature should be set in Authorization header for following request.", 
        "signature": "6123befcb272ae2d0a9b92f1842dba"
    }
},

I have to pass the signature value to the below task in Authorization:

- name: access list
  uri:
    url: "http://10.25.155.x/axapi/v3/access-list/extended/"
    method: POST
    headers:
       Content-Type: "application/json"
       Authorization: A10 6123befcb272ae2d0a9b92f1842dba
    body: { "extended": { "extd": 102, "rules": [ { "extd-seq-num": 15,"extd-action": "permit","tcp": 1,"src-any": 1,"dst-host": "172.69.4.4","dst-eq" : 443 } ] } }
    validate_certs: no
    follow_redirects: all
  with_items: "{{a10.results}}"

Upvotes: 1

Views: 2386

Answers (1)

techraf
techraf

Reputation: 68629

All you need is to refer to the specific key from the data structure you registered in the first task. The JSON is already mapped into an Ansible (Python) object:

Authorization: "A10 {{ A10.json.authresponse.signature }}"

Upvotes: 2

Related Questions