Ansible解析JSON输出

时间:2018-04-07 05:54:37

标签: ansible ansible-2.x ansible-inventory

我试图解析Ansible输出打印值

- name: Creating a new instance
  os_server:
  state: present
  auth:
  auth_url: "{{ auth_url }}"
  username: "{{ username }}"
  password: "{{ password }}"
  project_name: "{{ project_name }}"
name: "{{ item.hostname }}"
image: "{{ item.image }}"
nics: "{{ nics }}"
with_items: "{{ servers }}"
register: "os"

输出:

"server": {
    "OS-DCF:diskConfig": "MANUAL", 
    "OS-EXT-AZ:availability_zone": "zoneA", 
    "OS-EXT-STS:power_state": 1, 
    "OS-EXT-STS:task_state": null, 
    "OS-EXT-STS:vm_state": "active", 
    "OS-SRV-USG:launched_at": "2018-04-01T18:53:16.000000", 
    "OS-SRV-USG:terminated_at": null, 
    "accessIPv4": "10.190.230.23", 
    "accessIPv6": "", 
    "addresses": {
        "provider_corenet_bif_757": [
            {
                "OS-EXT-IPS-MAC:mac_addr": "fa:1:3:3:5e:6a", 
                "OS-EXT-IPS:type": "fixed", 
                "addr": "10.19.23.23", 
                "version": 4
            }
        ], 
        "provider_nmnet_bif_912": [
            {
                "OS-EXT-IPS-MAC:mac_addr": "fa:1:3:39:b:57", 
                "OS-EXT-IPS:type": "fixed", 
                "addr": "10.25.13.64", 
                "version": 4
            }
        ]

      server.addresses.provider_nmnet_bif_912.addr

    },

我想解析addr“10.25.13.64”。

我试过了{{item.server.addresses.provider_nmnet_bif_912.addr}}和{{os.server.addresses.provider_nmnet_bif_912.addr}}两个都没有用。

需要帮助!!!

1 个答案:

答案 0 :(得分:1)

终于明白了:

  

“{{item.server.addresses.provider_nmnet_bif_912 [0] .addr}}”