[英]How to retrieve values from json response of a CURL request when using ansible command module?
I am using Ansible to test some APIs.我正在使用 Ansible 来测试一些 API。 Instead of using get_url or uri module of ansible, i am using Curl requests with command/shell module.
我没有使用 ansible 的 get_url 或 uri 模块,而是使用带有命令/shell 模块的 Curl 请求。
I would need to extract a particular value from the json response of a curl api call, so that i can use that value in next curl request.我需要从 curl api 调用的 json 响应中提取特定值,以便我可以在下一个 curl 请求中使用该值。
For example,例如,
- name: Run a curl request to login and get jwt token
command: curl -k -X POST -H "Content-Type:application/json" --data '{"user":"{{username}}","password":"{{password}}"}' https://localhost:8001/api/v1/login
register: login_response
- name: Print the jwt response
debug:
var: login_response.stdout
- name: Run a curl request to get service token
command: curl -k -X GET -H "Accept:application/json" -H "Authorization:Bearer {{login_response.stdout.jwt}}" https://localhost:8001/api/v1/servicetoken/issue
register: service_token
- name: Print service token
debug:
var: service_token.stdout
The above ansible playbook will fail at third task because it cannot find a variable like:上面的 ansible playbook 将在第三个任务中失败,因为它找不到像这样的变量:
login_response.stdout.jwt
Normally the variable in second task, ie, login_response.stdout will print something like this:通常第二个任务中的变量,即 login_response.stdout 将打印如下内容:
ok: [localhost] => {
"login_response.stdout": {
"jwt": "eyJhbGciOiJIUzUxMiIsImtpZCI6ImFiNmU0ZDg2LWE4YzgtNDU4OS04MmRiLWIxZTg1YzQwNDNlZiJ9.eyJleHAiOjE2MDU2MTIxODAuNjA5MTI1NCwiaWF0IjoxNjA1NjExODgwLjYwOTEyMC42MDkxMjU0fQ.ZC4a3H3j03ZmzDkjGj11cvxSls2qXZmVOGuIvKp8LHVpYOUyEJlWJJOTArHxKhxne3DsuqWoGpslR6KxuUOBFg",
"roles": [
{
"name": "Admin"
}
]
}
}
What i want is to extract that jwt token from the above response and use it in third task.我想要的是从上述响应中提取该 jwt 令牌并在第三个任务中使用它。
How can i achieve this?我怎样才能做到这一点?
I found the answer!我找到了答案!
This can be achieved using from_json filter in order to make ansible able to access the json fields.这可以使用from_json过滤器来实现,以使 ansible 能够访问 json 字段。
Now the working playbook should be like:现在工作手册应该是这样的:
- name: Run a curl request to login and get jwt token
command: curl -k -X POST -H "Content-Type:application/json" --data '{"user":"{{username}}","password":"{{password}}"}' https://localhost:8001/api/v1/login
register: login_response
- name: Print the jwt token
debug:
var: (login_response.stdout|from_json).jwt
- name: Run a curl request to get service token
command: curl -k -X GET -H "Accept:application/json" -H "Authorization:Bearer {{(login_response.stdout|from_json).jwt}}" https://localhost:8001/api/v1/servicetoken/issue
register: service_token
- name: Print service token
debug:
var: (service_token.stdout|from_json).token
I refered here to get the idea: https://medium.com/@justinhennessy/parsing-json-with-ansible-bcbb8d4b6a54我在这里提到了这个想法: https : //medium.com/@justinhennessy/parsing-json-with-ansible-bcbb8d4b6a54
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.