无法解析 Ansible 中的 dict 和 array 元素

发布于 2025-01-11 18:46:01 字数 604 浏览 0 评论 0原文

我有一个 yaml 文件,我想使用 Ansible 浏览下面 yaml 文件中的每个元素。

有人可以帮我吗?

---
hardwares:
- VM.Standard2.1:
  - Oracle Linux:
    - '7.9'
    - '8'
    CentOS:
    - '8'
    Canonical Ubuntu:
    - '20.04'
- VM.Standard.E4.Flex:
  - Oracle Linux:
    - '7.9'
    - '8'
    CentOS:
    - '8'
    Canonical Ubuntu:
    - '20.04'
- VM.Standard.E3.Flex:
  - Oracle Linux:
    - '7.9'
    - '8'
    CentOS:
    - '8'
    Canonical Ubuntu:
    - '20.04'
- VM.Standard.A1.Flex:
  - Oracle Linux:
    - '7.9'
    - '8'
    Canonical Ubuntu:
    - '20.04'

I have a yaml file and I would like to go through each of the element in the below yaml file using Ansible.

Can someone help me here?

---
hardwares:
- VM.Standard2.1:
  - Oracle Linux:
    - '7.9'
    - '8'
    CentOS:
    - '8'
    Canonical Ubuntu:
    - '20.04'
- VM.Standard.E4.Flex:
  - Oracle Linux:
    - '7.9'
    - '8'
    CentOS:
    - '8'
    Canonical Ubuntu:
    - '20.04'
- VM.Standard.E3.Flex:
  - Oracle Linux:
    - '7.9'
    - '8'
    CentOS:
    - '8'
    Canonical Ubuntu:
    - '20.04'
- VM.Standard.A1.Flex:
  - Oracle Linux:
    - '7.9'
    - '8'
    Canonical Ubuntu:
    - '20.04'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

默嘫て 2025-01-18 18:46:01

结构设计错误。使用字典

    Oracle Linux:
      - '7.9'
      - '8'
    CentOS:
      - '8'
    Canonical Ubuntu:
      - '20.04'

或列表

  - Oracle Linux:
      - '7.9'
      - '8'
  - CentOS:
      - '8'
  - Canonical Ubuntu:
      - '20.04'

让我们在下面的示例中使用字典

    hardwares:
      VM.Standard2.1:
        Oracle Linux:
          - '7.9'
          - '8'
        CentOS:
          - '8'
        Canonical Ubuntu:
          - '20.04'
      VM.Standard.E4.Flex:
        Oracle Linux:
          - '7.9'
          - '8'
        CentOS:
          - '8'
        Canonical Ubuntu:
          - '20.04'
      VM.Standard.E3.Flex:
        Oracle Linux:
          - '7.9'
          - '8'
        CentOS:
          - '8'
        Canonical Ubuntu:
          - '20.04'
      VM.Standard.A1.Flex:
        Oracle Linux:
          - '7.9'
          - '8'
        Canonical Ubuntu:
          - '20.04'

创建一个文件

shell> cat loop_outer_item.yml
- debug:
    msg: "{{ outer_item.key }} {{ item.0.key }} {{ item.1 }}"
  with_subelements:
    - "{{ outer_item.value|dict2items }}"
    - value

并将其包含在循环中

    - include_tasks: loop_outer_item.yml
      loop: "{{ hardwares|dict2items }}"
      loop_control:
        loop_var: outer_item

给出(删节)

  msg: VM.Standard2.1 Oracle Linux 7.9
  msg: VM.Standard2.1 Oracle Linux 8
  msg: VM.Standard2.1 CentOS 8
  msg: VM.Standard2.1 Canonical Ubuntu 20.04
  msg: VM.Standard.E4.Flex Oracle Linux 7.9
  msg: VM.Standard.E4.Flex Oracle Linux 8
  msg: VM.Standard.E4.Flex CentOS 8
  msg: VM.Standard.E4.Flex Canonical Ubuntu 20.04
  msg: VM.Standard.E3.Flex Oracle Linux 7.9
  msg: VM.Standard.E3.Flex Oracle Linux 8
  msg: VM.Standard.E3.Flex CentOS 8
  msg: VM.Standard.E3.Flex Canonical Ubuntu 20.04
  msg: VM.Standard.A1.Flex Oracle Linux 7.9
  msg: VM.Standard.A1.Flex Oracle Linux 8
  msg: VM.Standard.A1.Flex Canonical Ubuntu 20.04

The design of the structure is wrong. Either use a dictionary

    Oracle Linux:
      - '7.9'
      - '8'
    CentOS:
      - '8'
    Canonical Ubuntu:
      - '20.04'

or a list

  - Oracle Linux:
      - '7.9'
      - '8'
  - CentOS:
      - '8'
  - Canonical Ubuntu:
      - '20.04'

Let's use dictionaries in the example below

    hardwares:
      VM.Standard2.1:
        Oracle Linux:
          - '7.9'
          - '8'
        CentOS:
          - '8'
        Canonical Ubuntu:
          - '20.04'
      VM.Standard.E4.Flex:
        Oracle Linux:
          - '7.9'
          - '8'
        CentOS:
          - '8'
        Canonical Ubuntu:
          - '20.04'
      VM.Standard.E3.Flex:
        Oracle Linux:
          - '7.9'
          - '8'
        CentOS:
          - '8'
        Canonical Ubuntu:
          - '20.04'
      VM.Standard.A1.Flex:
        Oracle Linux:
          - '7.9'
          - '8'
        Canonical Ubuntu:
          - '20.04'

Create a file

shell> cat loop_outer_item.yml
- debug:
    msg: "{{ outer_item.key }} {{ item.0.key }} {{ item.1 }}"
  with_subelements:
    - "{{ outer_item.value|dict2items }}"
    - value

and include it in the loop

    - include_tasks: loop_outer_item.yml
      loop: "{{ hardwares|dict2items }}"
      loop_control:
        loop_var: outer_item

gives (abridged)

  msg: VM.Standard2.1 Oracle Linux 7.9
  msg: VM.Standard2.1 Oracle Linux 8
  msg: VM.Standard2.1 CentOS 8
  msg: VM.Standard2.1 Canonical Ubuntu 20.04
  msg: VM.Standard.E4.Flex Oracle Linux 7.9
  msg: VM.Standard.E4.Flex Oracle Linux 8
  msg: VM.Standard.E4.Flex CentOS 8
  msg: VM.Standard.E4.Flex Canonical Ubuntu 20.04
  msg: VM.Standard.E3.Flex Oracle Linux 7.9
  msg: VM.Standard.E3.Flex Oracle Linux 8
  msg: VM.Standard.E3.Flex CentOS 8
  msg: VM.Standard.E3.Flex Canonical Ubuntu 20.04
  msg: VM.Standard.A1.Flex Oracle Linux 7.9
  msg: VM.Standard.A1.Flex Oracle Linux 8
  msg: VM.Standard.A1.Flex Canonical Ubuntu 20.04
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文