gpt4 book ai didi

ansible - 双循环 Ansible

转载 作者:行者123 更新时间:2023-12-02 08:57:41 25 4
gpt4 key购买 nike

我有一个这样的对象

objs:
- { key1: value1, key2: [value2, value3] }
- { key1: value4, key2: [value5, value6] }

我想创建以下文件

value1/value2
value1/value3
value4/value5
value4/value6

但我不知道如何使用 with_items

进行双循环

最佳答案

看看 Ansible's docs for loops 中的 with_subelements .

  1. 您需要创建目录:
  2. 迭代objs并创建文件:

这是一个例子:

---

- hosts: localhost
gather_facts: no
vars:
objs:
- { key1: value1, key2: [ value2, value3] }
- { key1: value4, key2: [ value5, value6] }
tasks:
- name: create directories
file: path="{{ item.key1 }}" state=directory
with_items:
objs

- name: create files
file: path="{{ item.0.key1 }}/{{ item.1 }}" state=touch
with_subelements:
- objs
- key2

输出非常不言自明,第二个循环按照您需要的方式迭代值:

PLAY [localhost] ************************************************************** 

TASK: [create files] **********************************************************
changed: [localhost] => (item={'key2': ['value2', 'value3'], 'key1': 'value1'})
changed: [localhost] => (item={'key2': ['value5', 'value6'], 'key1': 'value4'})

TASK: [create files] **********************************************************
changed: [localhost] => (item=({'key1': 'value1'}, 'value2'))
changed: [localhost] => (item=({'key1': 'value1'}, 'value3'))
changed: [localhost] => (item=({'key1': 'value4'}, 'value5'))
changed: [localhost] => (item=({'key1': 'value4'}, 'value6'))

PLAY RECAP ********************************************************************
localhost : ok=2 changed=2 unreachable=0 failed=0

关于ansible - 双循环 Ansible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31566568/

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