Can';t从Junos输出使用(-)访问JSON元素-Ansible 2.8

Can';t从Junos输出使用(-)访问JSON元素-Ansible 2.8,json,debugging,ansible,Json,Debugging,Ansible,我需要帮助才能从junos_命令输出中访问包含(-)的JSON元素。我似乎不知道如何通过bgp信息 我使用的是junos_命令模块。我在使用juniper_junos_命令模块时也遇到了同样的问题 我使用的是Ansible 2.8版 从剧本: - name: Show BGP neighbor JSON output junos_command: commands: - show bgp neighbor 174.68.232.1

我需要帮助才能从junos_命令输出中访问包含(-)的JSON元素。我似乎不知道如何通过bgp信息

我使用的是junos_命令模块。我在使用juniper_junos_命令模块时也遇到了同样的问题

我使用的是Ansible 2.8版

从剧本:

    - name: Show BGP neighbor JSON output
      junos_command:  
        commands:
          - show bgp neighbor 174.68.232.1
        display: json  
      register: configs

    - name: Debug output
      debug:
        var: configs.stdout[0]

    - name: Debug output
      debug:
        var: configs.stdout[0]['bgp-information']['bgp-peer']
调试输出(部分):


您可以看到
bgp信息
是一个列表变量(下一行以破折号开始)。因此,您的最后一项任务应该是:

- name: Debug output
  debug:
    var: configs.stdout[0]['bgp-information'][0]['bgp-peer']
希望能有帮助

更新

我获取了您提供的输出,并在变量中定义了它,下面是用于测试答案的剧本:

---
- hosts: localhost
  gather_facts: false
  vars:
    configs_stdout:
    - bgp-information:
      - attributes:
          xmlns: http://xml.juniper.net/junos/16.1R4/junos-routing
        bgp-peer:
        - active-holdtime:
          - data: '90'
          attributes:
            junos:style: detail
          bgp-bfd:
          - bfd-configuration-state:
            - data: disabled
            bfd-operational-state:
            - data: down
          bgp-option-information:
          - address-families:
            - data: inet-unicast inet-multicast inet-vpn-unicast inet-vpn-multicast inet-labeled-unicast inet6-labeled-unicast inet-mvpn
            attributes:
              xmlns: http://xml.juniper.net/junos/16.1R4/junos-routing
            authentication-configured:
            - data:
              - null
            bgp-options:
            - data: Preference LocalAddress AdvertiseInactive AuthKey LogUpDown AddressFamily Multipath Rib-group Refresh
            bgp-options-extended:
            - {}
            bgp-options2:
            - {}
            export-policy:
            - data: iBGP-POLICY
            holdtime:
            - data: '90'
            import-policy:
            - data: iBGP-IN
            local-address:
            - data: 174.68.232.31

  tasks:
  - name: Debug output
    debug:
      var: configs_stdout

  - name: Debug output
    debug:
      var: configs_stdout[0]['bgp-information'][0]['bgp-peer']

如果运行PB,您将验证
var:configs.stdout[0]['bgp-information'][0]['bgp-peer']
是否有效。

我使用regex\u replace找到了一个解决方案

    - name: Show BGP neighbor JSON output
      juniper_junos_command:  
        provider: "{{ credentials }}"
        commands:
          - show bgp neighbor 174.68.232.1
        display: json  
      register: configs

    - name: set fact
      set_fact:
        bgp_neighbor: "{{ configs.parsed_output | regex_replace('-', '_') }}"

    - name: Debug output
      debug:
        var: bgp_neighbor.bgp_information[0].bgp_peer[0].peer_state[0].data

    - name: Debug output
      debug:
        var: bgp_neighbor.bgp_information[0].bgp_peer[0].description[0].data 

    - name: Debug output
      debug:
        var: bgp_neighbor.bgp_information[0].bgp_peer[0].peer_id[0].data     

    - name: BGP Status
      debug:
        msg: |
            "peer name: {{ bgp_neighbor.bgp_information[0].bgp_peer[0].description[0].data }}
             peer address: {{ bgp_neighbor.bgp_information[0].bgp_peer[0].peer_id[0].data }}
             peer state: {{ bgp_neighbor.bgp_information[0].bgp_peer[0].peer_state[0].data }}"
TASK [Debug output] *********************************************************************************************************************************************************************************************
ok: [NEP6HDRJ31] => 
  bgp_neighbor.bgp_information[0].bgp_peer[0].peer_state[0].data: Established
TASK [Debug output] *********************************************************************************************************************************************************************************************
ok: [NEP6HDRJ31] => 
  bgp_neighbor.bgp_information[0].bgp_peer[0].description[0].data: NEP6BPRJ01
TASK [Debug output] *********************************************************************************************************************************************************************************************
ok: [NEP6HDRJ31] => 
  bgp_neighbor.bgp_information[0].bgp_peer[0].peer_id[0].data: 174.68.232.1
TASK [BGP Status] ***********************************************************************************************************************************************************************************************
ok: [NEP6HDRJ31] => 
  msg: |-
    "peer name: NEP6BPRJ01
     peer address: 174.68.232.1
     peer state: Established"

PLAY RECAP ******************************************************************************************************************************************************************************************************
NEP6HDRJ31                 : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 


您好,我刚刚尝试过,但仍在获取配置。标准输出[0]['bgp-information'][0]['bgp-peer']:未定义变量!–还有其他建议吗?请看更新后的答案,我添加了一个PB来测试结果。假设变量是您在问题中粘贴的,那么它应该可以工作..谢谢您的帮助。通过使用regex_replace,我能够从输出中获得所需的内容。
    - name: Show BGP neighbor JSON output
      juniper_junos_command:  
        provider: "{{ credentials }}"
        commands:
          - show bgp neighbor 174.68.232.1
        display: json  
      register: configs

    - name: set fact
      set_fact:
        bgp_neighbor: "{{ configs.parsed_output | regex_replace('-', '_') }}"

    - name: Debug output
      debug:
        var: bgp_neighbor.bgp_information[0].bgp_peer[0].peer_state[0].data

    - name: Debug output
      debug:
        var: bgp_neighbor.bgp_information[0].bgp_peer[0].description[0].data 

    - name: Debug output
      debug:
        var: bgp_neighbor.bgp_information[0].bgp_peer[0].peer_id[0].data     

    - name: BGP Status
      debug:
        msg: |
            "peer name: {{ bgp_neighbor.bgp_information[0].bgp_peer[0].description[0].data }}
             peer address: {{ bgp_neighbor.bgp_information[0].bgp_peer[0].peer_id[0].data }}
             peer state: {{ bgp_neighbor.bgp_information[0].bgp_peer[0].peer_state[0].data }}"
TASK [Debug output] *********************************************************************************************************************************************************************************************
ok: [NEP6HDRJ31] => 
  bgp_neighbor.bgp_information[0].bgp_peer[0].peer_state[0].data: Established
TASK [Debug output] *********************************************************************************************************************************************************************************************
ok: [NEP6HDRJ31] => 
  bgp_neighbor.bgp_information[0].bgp_peer[0].description[0].data: NEP6BPRJ01
TASK [Debug output] *********************************************************************************************************************************************************************************************
ok: [NEP6HDRJ31] => 
  bgp_neighbor.bgp_information[0].bgp_peer[0].peer_id[0].data: 174.68.232.1
TASK [BGP Status] ***********************************************************************************************************************************************************************************************
ok: [NEP6HDRJ31] => 
  msg: |-
    "peer name: NEP6BPRJ01
     peer address: 174.68.232.1
     peer state: Established"

PLAY RECAP ******************************************************************************************************************************************************************************************************
NEP6HDRJ31                 : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0