gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:58:05 25 4
gpt4 key购买 nike

我正在尝试用 ansible 做到这一点:
我有多个“水果”,想分发给多个 child :

  - 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个 child
答:例如
- 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

关于list - Ansible,将列表分发到其他列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66597135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com