可以,将一个列表分发给其他列表



我正试图用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_lengest、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

Q:">有4个以上的孩子">

A: 例如

- 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

相关内容

  • 没有找到相关文章

最新更新