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

时间:2018-01-21 18:13:16

标签: json ansible uri ansible-2.x

使用以下任务,我想提取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}}"

1 个答案:

答案 0 :(得分:1)

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

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