首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一种特定格式的可能的调试消息

一种特定格式的可能的调试消息
EN

Stack Overflow用户
提问于 2020-12-05 10:42:38
回答 1查看 90关注 0票数 0

我有以下脚本,它执行ping操作。

代码语言:javascript
复制
    - 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

该脚本使用以下变量:

代码语言:javascript
复制
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)并打印以下输出:

代码语言:javascript
复制
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%"
        }
    ]
}

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

代码语言:javascript
复制
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%"
        }
    ]
}

如何实现上述输出?每一节都有一行

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-05 17:11:10

我已经创建了这个简单的攻略,它可能会帮助你获得你需要的东西:

代码语言:javascript
复制
---
- 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 %}

我用三种不同的方式打印了这条消息。最后一个最接近你想要实现的目标。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65153263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档