Ansible 从 JSON 输出中提取值并将其用于其他任务

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

使用以下任务我想提取 authresponse 签名值:

- 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"

输出:

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

我必须将签名值传递给 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}}"

您只需要从您在第一个任务中注册的数据结构中引用特定键即可。 JSON 已经映射到 Ansible (Python) 对象中:

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