首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ansible Extract JSON标签

Ansible Extract JSON标签
EN

Stack Overflow用户
提问于 2018-07-31 20:17:09
回答 1查看 211关注 0票数 1

我正在尝试使用Infoblox API,它是响应。我需要从响应中提取标记值,这似乎是JSON格式,但我找不到这样做的方法。

以下是我的攻略:

代码语言:javascript
复制
- 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变量如下所示:

代码语言:javascript
复制
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
        }
    ]
}

我想使用来自_infoblox_results.json_ref数据,但我无法用regex_replace提取它(它会返回完整的_infoblox_results.json):

代码语言:javascript
复制
- name: Get Record ID
  set_fact:
    _rcdid: "{{ _infoblox_results.json | regex_replace('record:srv.*\\/default,', '\\1') }}"

- debug:
    var: _rcdid
  when: _infoblox_results.json != []

json_query也不是(它不会有任何回落):

代码语言:javascript
复制
- name: Get Record ID
  set_fact:
    _rcdid: "{{ _infoblox_results.json | json_query('_ref') }}"

- debug:
    var: _rcdid
  when: _infoblox_results.json != []

有谁能给我指个方向吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-31 20:25:05

内存中已经有一个对象,所以只需引用它的值:_infoblox_results.json[0]._ref包含字符串record:srv/ZG5zLmJpbmRfc3J2JC5fZGVmYXVsdC5jb20udmNpbnQuZXcxL19ub2RlX2V4cG9ydGVyLzAvMC85MTAwL3Zhcm5pc2g3MDJ0c3QuZXcxLnZjaW50LmNvbQ:_node_exporter.domain.com/default

这样,您就可以拆分字符串并选择第二个元素:

代码语言:javascript
复制
- name: Get Record ID
  set_fact:
    _rcdid: "{{ _infoblox_results.json[0]._ref.split('/')[1] }}"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51613205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档