Ansible结构化输出,仅获得一个特定的输出

时间:2019-06-06 16:40:40

标签: ansible

我有这个输出:

ospf_struct: - Neighbor: 1.1.1.2 Neighbor_is_up: 1w2d interface_address: 1.1.1.2 via_interface: GigabitEthernet0/1

您能帮我创建一个仅存储“ via_inteface:GigabitEthernet0 / 1”的变量吗?

我的剧本看起来像这样:

---

- name: "REPORT -"
  hosts: ios
  connection: network_cli
  become: yes
  become_method: enable
  gather_facts: no
  vars:
   OSPF_command: show ip ospf nei det

  tasks:

    - name: "Run command"
      ios_command:
        commands: "{{ OSPF_command }}"
      register: ospf

    - name: "Run Template"
      set_fact:
        ospf_struct: "{{ ospf.stdout[0] | parse_cli_textfsm('/etc/ansible/playbooks/test.template') }}"

    - name: Display outuput
      debug:
         var: ospf_struct

    #- name Print only one-variable with the value "GigabitEthernet0/1"
       #How could i get this happen.

谢谢!

/穆罕默德

我试图制作一个不错的json“ ospf_struct:"{{ ospf.stdout[0] | parse_cli_textfsm('/etc/ansible/playbooks/test.template') | to_nice_json }}"

并尝试调试,但也无法正常工作

debug:
   var: ospf_struct.via_interface

1 个答案:

答案 0 :(得分:0)

现在,它与味精一起使用,而不是var: debug: msg: "{{ ospf_struct.0.via_interface }}"谢谢#Alassane的支持–