Ansible,将列表分发到其他列表

Ansible, distribute a list to other list

我正在尝试使用 ansible 来做到这一点:

我有多个“水果”,想分给多个孩子:

  - vars:
      kids:
      - John
      - Shara
      - David
      fruits:
      - Banana
      - Mango
      - Orange
      - Peach
      - Pineapple
      - Watermelon
      - Avocado
      - Cherries

想要的结果,像这样:

John:
- Banana
- Peach
- Avocado


Shara:
- Mango
- Pineapple
- Cherries

David:
- Orange
- Watermelon

我尝试过使用 zip、zip_longest、list,但没有办法。

ansible.builtin.debug:
  msg: "{{ item | zip(['a','b','c','d','e','f']) | list }}"
loop:
  - John
  - Shara
  - David

例如

    - set_fact:
        _dict: "{{ dict(kids|zip(_values)) }}"
      vars:
        _batch: "{{ fruits|length|int / kids|length|int }}"
        _values: "{{ fruits|batch(_batch|float|round)|list }}"

给予

  _dict:
    David:
    - Avocado
    - Cherries
    John:
    - Banana
    - Mango
    - Orange
    Shara:
    - Peach
    - Pineapple
    - Watermelon

问:"有超过 4 个孩子"

答:例如

- hosts: localhost
  gather_facts: false
  vars:
    kids:
    - John
    - Shara
    - David
    - Alice
    - Bob
    fruits:
    - Banana
    - Mango
    - Orange
    - Peach
    - Pineapple
    - Watermelon
    - Avocado
    - Cherries
    - Apple
  tasks:
    - set_fact:
        _dict: "{{ dict(kids|zip(_values)) }}"
      vars:
        _batch: "{{ fruits|length|int / kids|length|int }}"
        _values: "{{ fruits|batch(_batch|float|round)|list }}"
    - debug:
        var: _dict

给予

  _dict:
    Alice:
    - Avocado
    - Cherries
    Bob:
    - Apple
    David:
    - Pineapple
    - Watermelon
    John:
    - Banana
    - Mango
    Shara:
    - Orange
    - Peach