使用Ansible的uri模块可以发送HTTP请求并解析响应头中的值。以下是一个示例代码,演示如何使用uri模块解析REST API响应头中的值:
- name: Send HTTP GET request
uri:
url: "http://api.example.com/rest/endpoint"
method: GET
return_content: yes
headers:
Accept: "application/json"
register: response
- name: Parse response headers
set_fact:
x-custom-header-value: "{{ response.headers['x-custom-header'] }}"
- name: Print parsed header value
debug:
var: x-custom-header-value
在上面的示例中,我们使用uri模块发送一个HTTP GET请求到http://api.example.com/rest/endpoint,并设置Accept头为application/json。return_content: yes选项告诉uri模块返回响应内容。
接下来,我们使用set_fact模块将x-custom-header的值存储在x-custom-header-value变量中。可以根据实际的响应头字段名称进行修改。
最后,我们使用debug模块打印出解析的头字段值。
请根据实际情况修改URL和其他请求参数,并根据需要解析其他响应头字段。