将每个循环的输出存储到列表中

问题描述 投票:0回答:1

我想将每次查看的输出存储到一个列表中。我有一个这样的任务:

- name: Get ping data
  set_fact:
    packets_received: "{{ item | regex_search('(\\d*) packets received', '\\1'))[0] }}"
    packet_loss: "{{ item | regex_search('(\\d*)% packet loss', '\\1'))[0] }}"
    latency: "{{ item | regex_search('(\\d*\\.\\d*)\\/(\\d*\\.\\d*)\\/(\\d*\\.\\d*) ms', '\\1', '\\2', '\\3')) }}"

  loop: 
    "{{ping_output['results']}}"

我想将这3个变量存储到一个新列表中,例如 [ {packets_received: value, packet_loss: value, Latency: value}, {packets_received: value, packet_loss: value, Latency: value} ]

我在执行此操作时遇到困难,因为使用组合过滤器只会替换列表中的项目,因为它具有相同的键名称。

ansible
1个回答
0
投票

例如,让我们收集三台主机的 ping 结果

  rhosts: [test_11, test_12, test_13]
    - command: ping -c 3 {{ item }}
      register: ping_output
      loop: "{{ rhosts }}"

在控制器上安装jc并解析输出

  pings:  "{{ ping_output.results |
              map(attribute='stdout') |
              map('community.general.jc', 'ping') }}"

给出(删节)

 pings:
    -   data_bytes: 56
        destination: test_11
        destination_ip: 10.1.0.61
        duplicates: 0
        packet_loss_percent: 0.0
        packets_received: 3
        packets_transmitted: 3
        pattern: null
        responses:
        -   bytes: 64
            duplicate: false
            icmp_seq: 1
            response_ip: 10.1.0.61
            time_ms: 2.56
            timestamp: null
            ttl: 64
            type: reply
        -   bytes: 64
            ...

现在得到结果应该是微不足道的。有很多选择:

  1. 最简单的是community.general.keep_keys。宣布目标
  target: [packets_received, packet_loss_percent, time_ms]

然后选择按键

  result: "{{ pings | community.general.keep_keys(target=target) }}"

给予

  result:
    -   packet_loss_percent: 0.0
        packets_received: 3
        time_ms: 2003.0
    -   packet_loss_percent: 0.0
        packets_received: 3
        time_ms: 2001.0
    -   packet_loss_percent: 0.0
        packets_received: 3
        time_ms: 2001.0
  1. 如果您必须使用钥匙
  target: [packets_received, packet_loss, latency]

使用 json_querycommunity.general.dict

  result: "{{ pings |
              json_query('[].[packets_received, packet_loss_percent, time_ms]') |
              map('zip', target) |
              map('map', 'reverse') |
              map('community.general.dict') }}"

给你想要的东西

  result:
    -   latency: 2003.0
        packet_loss: 0.0
        packets_received: 3
    -   latency: 2001.0
        packet_loss: 0.0
        packets_received: 3
    -   latency: 2001.0
        packet_loss: 0.0
        packets_received: 3

用于测试的完整剧本示例

- hosts: localhost

  vars:

    rhosts: [test_11, test_12, test_13]
    pings:  "{{ ping_output.results |
                map(attribute='stdout') |
                map('community.general.jc', 'ping') }}"
    target: [packets_received, packet_loss_percent, time_ms]
    result: "{{ pings | community.general.keep_keys(target=target) }}"

    targe2: [packets_received, packet_loss, latency]
    resul2: "{{ pings |
                json_query('[].[packets_received, packet_loss_percent, time_ms]') |
                map('zip', targe2) |
                map('map', 'reverse') |
                map('community.general.dict') }}"

  tasks:

    - command: ping -c 3 {{ item }}
      register: ping_output
      loop: "{{ rhosts }}"

    - debug:
        var: pings | to_nice_yaml

    - debug:
        var: result | to_nice_yaml

    - debug:
        var: resul2 | to_nice_yaml
© www.soinside.com 2019 - 2024. All rights reserved.