Ansible提取JSON标记
我正在尝试使用Infoblox API,这是我的回应。我需要从响应中提取标记的值,这似乎是JSON格式的,但我找不到方法 这是我的剧本:Ansible提取JSON标记,json,ansible,Json,Ansible,我正在尝试使用Infoblox API,这是我的回应。我需要从响应中提取标记的值,这似乎是JSON格式的,但我找不到方法 这是我的剧本: - name: "Checking _node_exporter Service Record for {{ inventory_hostname }}" local_action: module: uri url: "{{ infobloxapiurl }}record:srv?name=_node_exporter.domain.com
- name: "Checking _node_exporter Service Record for {{ inventory_hostname }}"
local_action:
module: uri
url: "{{ infobloxapiurl }}record:srv?name=_node_exporter.domain.com&target={{ inventory_hostname }}"
force_basic_auth: yes
user: "{{ infobloxuser }}"
password: "{{ infobloxpassword }}"
validate_certs: no
return_content: yes
register: _infoblox_results
- debug:
var: _infoblox_results.json
_infoblox_results.json变量如下所示:
TASK [prometheus : debug] *******************************************************************************************************************************************************************************************
task path: /ansible/roles/tasks/task.yml:38
ok: [server.domain.com] => {
"_infoblox_results.json": [
{
"_ref": "record:srv/ZG5zLmJpbmRfc3J2JC5fZGVmYXVsdC5jb20udmNpbnQuZXcxL19ub2RlX2V4cG9ydGVyLzAvMC85MTAwL3Zhcm5pc2g3MDJ0c3QuZXcxLnZjaW50LmNvbQ:_node_exporter.domain.com/default",
"name": "_node_exporter.domain.com",
"port": 9100,
"priority": 0,
"target": "server.domain.com",
"view": "default",
"weight": 0
}
]
}
我想使用\u infoblox\u results.json
中的\u ref
数据,但我无法使用regex\u replace
提取它(它会返回完整的\u infoblox\u results.json
):
无论是使用json\u查询
(它都不会返回任何内容):
有人能给我指出正确的方向吗?内存中已经有一个对象,所以只需参考它的值:
\u infoblox\u results.json[0].u ref
包含字符串记录:srv/zg5zlmjpbmrfc3j2jc5fzgvmyxvsdc5jb20udmnpbnquezxcxl19ub2rlx2v4cg9ydgvylzavmc85mtawl3zhcm5pc2g3mdj0c3quzcxlnzjaw50lmnvbq:_node\u exporter.domain.com/default
这样,您可以拆分字符串并选择第二个元素:
- name: Get Record ID
set_fact:
_rcdid: "{{ _infoblox_results.json[0]._ref.split('/')[1] }}"
- name: Get Record ID
set_fact:
_rcdid: "{{ _infoblox_results.json | json_query('_ref') }}"
- debug:
var: _rcdid
when: _infoblox_results.json != []
- name: Get Record ID
set_fact:
_rcdid: "{{ _infoblox_results.json[0]._ref.split('/')[1] }}"