如何访问JSON数组中的重复值并将每个值设置为ansible中的唯一变量?

时间:2018-08-02 19:22:33

标签: ansible

我正在运行一个Ansible剧本,它以JSON输出我的信息。该数组具有一个循环,该循环具有多个具有不同值的相同键。请参见下面,我对“ intf_id”感兴趣:

"stdout": [
    {
        "TABLE_cdp_neighbor_brief_info": {
            "ROW_cdp_neighbor_brief_info": [
                {
                    "capability": [
                        "switch", 
                        "IGMP_cnd_filtering"
                    ], 
                    "device_id": "osw1J15", 
                    "ifindex": "83886080", 
                    "intf_id": "mgmt0", 
                    "platform_id": "cisco WS-C2960X-48TS-LL", 
                    "port_id": "GigabitEthernet0/45", 
                    "ttl": "160"
                }, 
                {
                    "capability": [
                        "router", 
                        "switch", 
                        "Supports-STP-Dispute"
                    ], 
                    "device_id": "spine01", 
                    "ifindex": "436232192", 
                    "intf_id": "Ethernet1/49", 
                    "platform_id": "N9K-C9508", 
                    "port_id": "Ethernet1/11", 
                    "ttl": "159"
                }, 
                {
                    "capability": [
                        "router", 
                        "switch", 
                        "Supports-STP-Dispute"
                    ], 
                    "device_id": "spine02", 
                    "ifindex": "436232704", 
                    "intf_id": "Ethernet1/50", 
                    "platform_id": "N9K-C9508", 
                    "port_id": "Ethernet1/11", 
                    "ttl": "127"
                }, 
                {
                    "capability": [
                        "router", 
                        "switch", 
                        "IGMP_cnd_filtering", 
                        "Supports-STP-Dispute"
                    ], 
                    "device_id": "leaf1J1402", 
                    "ifindex": "436234240", 
                    "intf_id": "Ethernet1/53", 
                    "platform_id": "N9K-C93180YC-EX", 
                    "port_id": "Ethernet1/53", 
                    "ttl": "175"
                }, 
                {
                    "capability": [
                        "router", 
                        "switch", 
                        "IGMP_cnd_filtering", 
                        "Supports-STP-Dispute"
                    ], 
                    "device_id": "leaf1J1402", 
                    "ifindex": "436234752", 
                    "intf_id": "Ethernet1/54", 
                    "platform_id": "N9K-C93180YC-EX", 
                    "port_id": "Ethernet1/54", 
                    "ttl": "175"
                }
            ]
        }, 
        "neigh_count": "5"
    }
]

当前,我可以使用以下命令访问“ intf_id”的第一个迭代:

- debug: msg="{{ list.stdout[0].TABLE_cdp_neighbor_brief_info.ROW_cdp_neighbor_brief_info[0].intf_id }}"

我觉得自己已经接近了,但是无法弄清楚如何获得“ intf_id”的每次迭代。我可以通过将ROW_cdp_neighbor_brief_info [0]更改为[1]或[2]等来获得后续的密钥。我需要能够访问每个密钥,而又不知道会有多少个密钥。键的每个值还需要随后的任务分别调用。

1)我如何能够调试每个迭代?

2)根据对1的回答,如何为每个值分配一个partialualr变量? (即interface1,interface2,interface3 ...)

2 个答案:

答案 0 :(得分:1)

您可以使用json_query语法的过滤器jmespath通过这种方式遍历各种元素intf_id

---
- hosts: localhost
  gather_facts: no

  tasks:
    - name: populate router data from json
      set_fact:
        data: "{{ lookup('file','so-router-info.json') }}"

    - name: looping over interfaces
      debug:
        var: item
      loop: "{{ data | json_query('TABLE_cdp_neighbor_brief_info.ROW_cdp_neighbor_brief_info[*].intf_id') }}"

它给出了这个输出

…
TASK [looping over interfaces] ********************************************************************************************************
ok: [localhost] => (item=mgmt0) => {
    "item": "mgmt0"
}
ok: [localhost] => (item=Ethernet1/49) => {
    "item": "Ethernet1/49"
}
ok: [localhost] => (item=Ethernet1/50) => {
    "item": "Ethernet1/50"
…

您可以使用网站http://jmespath.org/测试过滤器

答案 1 :(得分:1)

ansible-playbook json_query.yml

tasks:                                                                                                                      
- name:                                                                                                                   
  debug:                                                                                                                  
    msg: "{{ item }}"                                                                                                     
  loop: "{{ stdout | json_query('TABLE_cdp_neighbor_brief_info.ROW_cdp_neighbor_brief_info[*].intf_id') }}"               

(输出摘要)

"msg": "mgmt0"
"msg": "Ethernet1/49"
"msg": "Ethernet1/50"
"msg": "Ethernet1/53"
"msg": "Ethernet1/54"