gpt4 book ai didi

json - Ansible URI 模块发布表单 urlencoded JSON 数据

转载 作者:行者123 更新时间:2023-12-04 12:56:46 26 4
gpt4 key购买 nike

我正在尝试使用 ansible URI 模块访问 API。如果我设置 tasks[0].uri.body.json_data: <literal string>但是,当我尝试将变量注入(inject) tasks[0].uri.body.json_data: <string with variable value injected> 时,它起作用了,它显示以下错误
json format error .
我只是想知道在这种情况下如何注入(inject)变量值。
先感谢您。
下面的示例代码

$ ansible-playbook test.yaml --extra-var id=1
---
# test.yaml
- name: Get person
hosts: localhost
vars:
person_id: '{{ id }}'
tasks:
- name: Get person based on ID
uri:
url: https://example.com/api
method: POST
body_format: form-urlencoded
validate_certs: no
# return_content: yes
body:
auth_user: test
auth_pwd: test
json_data: '{"operation": "core/get", "class": "Person", "key": "{{person_id}}", "output_fields": "*"}' # NOT working (replaced "key": "1" with "key": "{{person_id}}")
# json_data: '{"operation": "core/get", "class": "Person", "key": "1", "output_fields": "*"}' # WORKING
register: login
- debug:
var: person_id
- debug:
var: login.json

最佳答案

在 YAML 中创建数据然后过滤更容易 to_json .例如

shell> cat pb.yml
- hosts: localhost
vars:
person_id: "{{ id }}"
json_data:
operation: "core/get"
class: "Person"
key: "{{ person_id }}"
output_fields: "*"
tasks:
- debug:
msg: "{{ json_data|to_json }}"

shell> ansible-playbook pb.yml --extra-var id=1
...
msg: '{"operation": "core/get", "class": "Person", "key": "1", "output_fields": "*"}'

关于json - Ansible URI 模块发布表单 urlencoded JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65840216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com