【问题标题】:Ansible unicode object has no attribute value json parsingAnsible unicode对象没有属性值json解析
【发布时间】:2023-03-03 12:29:01
【问题描述】:

我的任务是

  - name: task name
    shell: openstack floating ip create provider --format json
    register: result

输出将是以下 json 格式

   {
  "router_id": null,
  "status": "DOWN",
  "description": "",
  "created_at": "2017-05-24T10:49:15Z",
  "updated_at": "2017-05-24T10:49:15Z",
  "floating_network_id": "923-cc77237b08e7",
  "headers": "",
  "fixed_ip_address": null,
  "floating_ip_address": "192.*.*.*",
  "revision_number": 1,
  "project_id": "2709ad381fcf41c5bce673c916fded10",
  "port_id": null,
  "id": "c5d187elg-d269-4eae-b6ae-7d258f04983"
}  

我想要做的是,只获取 floating_ip_address 并将其存储到一个变量中,以便我可以在另一个任务中使用它。

我使用下面的代码来做这个,

- set_fact:
     address: "{{ (result.stdout | from_json | selectattr('floating_ip_address') | list | first).floating_ip_address }}"

但我得到一个错误

   "ERROR! 'unicode object' has no attribute 'floating_ip_address'"

只获取ip地址的正确格式是什么?

【问题讨论】:

  • 你确定..我也会添加命令

标签: json ansible openstack


【解决方案1】:

如果floating_ip_address 不是列表,而是您输入的简单键,例如:

- set_fact:
    address: "{{ (result.stdout | from_json)['floating_ip_address'] }}"

【讨论】:

  • 我需要解析示例
  • 我现在不明白你在问什么。
猜你喜欢
  • 2015-03-15
  • 1970-01-01
  • 2013-12-28
  • 2015-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多