gpt4 book ai didi

dictionary - Ansible 如何创建字典键列表

转载 作者:行者123 更新时间:2023-12-03 16:49:17 24 4
gpt4 key购买 nike

我可能错过了一些简单的东西。我在 vars.yml 中有字典

deploy_env:
dev:
schemas:
year1:
- main
- custom
year2:
- main
- custom
- security
year3:
- main
- custom

然后在我的 playbook.yml 我有类似的东西
- set_fact:
years: "{{ deploy_env.dev.schemas }}"

- name: Create schemas
shell: "mysql ....params go here... {{ item }}"
with_nested:
- "{{ years }}"

如果模式在 中,则上述工作正常vars.yml 是一个简单的列表,即:
...schemas:
- year1
- year2
- year3

但是,一旦我在每年下添加其他项目(将其设为字典(?),我就会开始收到错误消息: - "{{ years }}"

我基本上想填充 {{年}} 使用此任务的 year1,year2,year3 值。

我看过很多例子,但我看到的一切都太复杂了,而且是关于如何创建没有帮助的词典。

谢谢!

最佳答案

可以创建字典键的列表。例如

    - set_fact:
years: "{{ deploy_env.dev.schemas.keys()|list }}"
- debug:
var: item
loop: "{{ years }}"

    "item": "year1"
"item": "year2"
"item": "year3"

**列表与字典**
引用:

"add additional items under each year (making this a dictionary(?)"


添加项目不会将列表更改为字典。列表中的一项在 YAML 中用破折号“-”引入.在列表的每个项目下添加其他项目使其成为列表列表。
列表示例:
    schemas:
- year1
- year2
- year3
列表列表示例
    schemas:
- year1:
- main
- custom
- year2:
- main
- custom
- security
- year3:
- main
- custom
字典示例:
    schemas:
year1:
- main
- custom
year2:
- main
- custom
- security
year3:
- main
- custom

关于dictionary - Ansible 如何创建字典键列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61095210/

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