Ansible如何创建字典键列表

Ansible how to create list of dictionary keys

我可能遗漏了一些简单的东西。我有字典 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 填充 {{ years }},此任务的 3 年 值。

我看过很多例子,但我看过的所有东西都太复杂了,都是关于如何创建字典的,但没有帮助。

谢谢!

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

    - 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