debugging 无法从Junos输出中使用(-)访问JSON元素- Ansible 2.8

b4qexyjb  于 2023-10-24  发布在  其他
关注(0)|答案(2)|浏览(142)

我需要帮助,试图从junos_command输出中访问一个包含(-)的JSON元素。我似乎不知道如何通过bgp-information。
我使用junos_command模块。我使用juniper_junos_command模块也遇到了同样的问题。
使用Ansible 2.8版本
Playbook:

- 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']

输出(部分):

TASK [Debug output] ***************************************************************************************************************************************************************************************
ok: [NEP6HDRJ31] => 
  configs.stdout[0]:
    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

TASK [Debug output] ***************************************************************************************************************************************************************************************
ok: [NEP6HDRJ31] => 
  configs.stdout[0]['bgp-information']['bgp-peer']: VARIABLE IS NOT DEFINED!
aemubtdh

aemubtdh1#

你可以看到bgp-information是一个列表变量(下一行以破折号开始).所以你的最后一个任务应该是:

- 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']是否工作。

mhd8tkvw

mhd8tkvw2#

我使用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

相关问题