从 dict 列表中删除一个项目并拆分 stdout_lines

Remove an item from a list of dict and splitting stdout_lines

我正在尝试从字典列表中删除一个项目,即“未找到结果”的项目:

    "validar": [
        {
            "LPARNAME": [
                "No results were found."
            ]
        },
        {
            "LPARNAME": [
                "server1",
                "server2",
                "server4"
            ]
        },
        {
            "LPARNAME": [
                "server3",
                "server5",
                "server7"
            ]
        }
    ]
}

我正在尝试使用 rejectattr 但无法正常工作:

  - name: clean
      set_fact:
        validar_: "{{ validar | rejectattr('LPARNAME', 'match', 'No results were found.') | list }}"

我也在尝试拆分 stdout_lines 个:

"output": [
        {
            "item": {
                "Name": "CONT1"
            },
            "stdout_lines": [
                "server1,1.05",
                "server2,0.25"
            ]
        },
        {
            "item": {
                "Name": "CONT2"
            },
            "stdout_lines": [
                "server3,0.05",
                "server4,0.35"
            ]
        },
        {
            "item": {
                "Name": "PRD1"
            },
            "stdout_lines": [
                "server4,1.15"
            ]
        }
    ]
}

我收到一个错误:{"msg": "'list object' 没有属性 'stdout_lines'"}

    - name: list
      set_fact:
        lpar_proc: "{{ lpar_proc | default([]) + [{ 'lpar': item.[0] , 'proc': item[1] }]"
      with_items: [ "{{ output.stdout_lines.split(',')[0]}}", "{{ output.stdout_lines.split(',')[1]" ]

预期输出:

   Name: CONT1
   vm: server1
   proc: 1.05

   Name: CONT1
   vm: server2
   proc: 0.25
... etc

你有一个列表的列表,所以你不能像现在这样使用 rejectattr:

- name: testplaybook jinja2
  hosts: localhost
  gather_facts: no
  vars:
    validar:
    - LPARNAME:
      - No results were found.
    - LPARNAME:
      - server1
      - server2
      - server4
    - LPARNAME:
      - server3
      - server5
      - server7

  tasks:
    - name: clean
      set_fact:
        result: "{{ result | default([]) + [item] }}"
      loop: "{{ validar }}"
      when: '"No results were found." not in item.LPARNAME'
    - name: set in validar
      set_fact:
        validar: "{{ result }}"
    - name: debug
      debug:
        var: validar

结果:

{
    "validar": [
        {
            "LPARNAME": [
                "server1",
                "server2",
                "server4"
            ]
        },
        {
            "LPARNAME": [
                "server3",
                "server5",
                "server7"
            ]
        }
    ]
}

另一个问题:

- name: testplaybook
  hosts: localhost
  gather_facts: no
  vars:
    output:
    - item:
        Name: CONT1
      stdout_lines:
      - server1,1.05
      - server2,0.25
    - item:
        Name: CONT2
      stdout_lines:
      - server3,0.05
      - server4,0.35
    - item:
        Name: PRD1
      stdout_lines:
      - server4,1.15

  tasks:
    - name: m1
      set_fact:
        m1: "{{ m1 | d([]) + _p }}"
      loop: "{{ output }}"
      vars:
        _p: "{{ [item['item']]| product(item.stdout_lines) }}"
    - name: m2
      set_fact:
        result: "{{ result | d([]) + [_server] }}"
      loop: "{{ m1 }}"
      vars:
        _vm: "{{ item.1.split(',')|first }}"
        _proc: "{{ item.1.split(',')|last }}"
        _server: "{{ item.0 | combine({'vm': _vm, 'proc': _proc}) }}"

    - name: debug
      debug:
        var: result

结果:

ok: [localhost] => {
    "result": [
        {
            "Name": "CONT1",
            "proc": "1.05",
            "vm": "server1"
        },
        {
            "Name": "CONT1",
            "proc": "0.25",
            "vm": "server2"
        },
        {
            "Name": "CONT2",
            "proc": "0.05",
            "vm": "server3"
        },
        {
            "Name": "CONT2",
            "proc": "0.35",
            "vm": "server4"
        },
        {
            "Name": "PRD1",
            "proc": "1.15",
            "vm": "server4"
        }
    ]
}
鉴于数据
validar:
  - LPARNAME:
    - No results were found.
  - LPARNAME:
    - server1
    - server2
    - server4
  - LPARNAME:
    - server3
    - server5
    - server7

属性 LPARNAME 的值是一个列表。使用测试 contains 而不是 match。参见 Testing if a list contains a value

validar_: "{{ validar|
              rejectattr('LPARNAME', 'contains', 'No results were found.') }}"

扩展到

validar_:
  - LPARNAME:
    - server1
    - server2
    - server4
  - LPARNAME:
    - server3
    - server5
    - server7

给定数据

output:
  - item:
      Name: CONT1
    stdout_lines:
    - server1,1.05
    - server2,0.25
  - item:
      Name: CONT2
    stdout_lines:
    - server3,0.05
    - server4,0.35
  - item:
      Name: PRD1
    stdout_lines:
    - server4,1.1

迭代with_subelements

    - set_fact:
        lpar_proc: "{{ lpar_proc|default([]) +
                       [item.0.item|
                        combine(dict(['vm', 'proc']|
                                     zip(item.1.split(','))))] }}"
      with_subelements:
        - "{{ output }}"
        - stdout_lines

给予

lpar_proc:
  - {Name: CONT1, proc: '1.05', vm: server1}
  - {Name: CONT1, proc: '0.25', vm: server2}
  - {Name: CONT2, proc: '0.05', vm: server3}
  - {Name: CONT2, proc: '0.35', vm: server4}
  - {Name: PRD1, proc: '1.1', vm: server4}