特定格式的 Ansible 调试消息

Ansible debug msg in a specific format

我有下面的脚本,它执行 ping。

    - name: Running WAN connectivty checks on WAN-01A
      cisco.ios.ios_ping:
         vrf: '{{item.vrf}}'
         dest: '{{item.destination}}'
         source: '{{item.source}}'
         count: '{{item.count}}'
         size: '{{item.size}}'
      loop: "{{ wan01avar }}"
      when: inventory_hostname ==  'WAN-01A'
      register: wan01a
      tags:
        - never
        - WAN

    - name: WAN connectivty results on WAN-01A
      debug: msg="{{ wan01a.results | json_query(jmesquery)}}"
      vars:
         jmesquery: "[*].{Source: item.source, Destination: item.destination,packetloss: packet_loss}"
      when: inventory_hostname ==  'WAN-01A'
      tags:
        - never
        - WAN


该脚本使用以下变量:

wan01avar:
  - vrf: 'default'
    source: 'Bundle-Ether400.682'
    destination: '10.27.251.194'
    count: '10'
    size: '1500'


  - vrf: 'CLOUD_DCI'
    source: 'Bundle-Ether400.682'
    destination: '10.27.251.194'
    count: '9'
    size: '1400'

我正在使用 json_query (jmesquery) 并打印以下输出:

TASK [wan : Output from WAN-01A] *******************************************
ok: [WAN-01A] => {
    "msg": [
        {
            "Destination": "10.27.251.194",
            "Source": "Bundle-Ether400.682",
            "packetloss": "0%"
        },
        {
            "Destination": "10.27.251.194",
            "Source": "Bundle-Ether400.682",
            "packetloss": "0%"
        }
    ]
}

我想要实现的是以下输出:

TASK [wan : Output from WAN-01A] *******************************************
ok: [WAN-01A] => {
    "msg": [
        {
"Destination": "10.27.251.194", "Source": "Bundle-Ether400.682", "packetloss": "0%"
"Destination": "10.27.251.194","Source": "Bundle-Ether400.682","packetloss": "0%"
        }
    ]
}

如何实现上面的输出?每个部分一行不错

我创建了这本简单的剧本,可以帮助您获得所需的东西:

---
- name: Format message
  hosts: localhost
  vars:
    wan01a:
      - vrf: 'default'
        source: 'Bundle-Ether400.682'
        destination: '10.27.251.194'
        count: '10'
        size: '1500'
      - vrf: 'CLOUD_DCI'
        source: 'Bundle-Ether400.682'
        destination: '10.27.251.194'
        count: '9'
        size: '1400'
  tasks:
    - debug:
        msg: "{{ wan01a }}"

    - debug:
        msg: "Destination: {{ item['destination'] }}, Source: {{ item['source'] }}, count: {{ item['count'] }}"
      with_items: "{{ wan01a }}"

    - debug:
        msg: >
          {%- for item in wan01a %}
          Destination: {{ item['destination'] }}, Source: {{ item['source'] }}, count: {{ item['count'] }}
          {%- endfor %}

我用三种不同的方式打印了消息。最后一个最接近你想要达到的效果。