【问题标题】:Using nested inventory lists in Ansible playbooks在 Ansible 剧本中使用嵌套清单列表
【发布时间】:2019-09-24 04:35:41
【问题描述】:

当我在 Ansible 的帮助下自动化我们的一些网络环境时,我想开始使用嵌套的库存/变量文件。这首先要保持一切整洁有序。但截至目前,我无法让它正常工作,也无法判断问题所在。

库存如下所示:(虚拟内容)

interfaces:
- name: "..."
  port: "..."
  description: "..."
    parameters:
      - speed: "..."
        duplex: "..."
- name: "..."
  port: "..."
  description: "..."
    parameters:
      - speed: "..."
        duplex: "..."  

如您所见,我有一个“接口”列表,其中包含一个“参数”列表,我想在我的剧本中解决这两个列表。

Playbook 如下所示:

- name: Configuring network ports
  "Some network module":
    name: '{{ item.0.name }}'
    port: '{{ item.0.port }}'
    description: '{{ item.0.description }}'
    speed: '{{ item.1.speed }}'
    duplex: '{{ item.1.duplex }}'
    state: present
  delegate_to: localhost
  with_subelements:
    - "{{ interfaces }}"
    - "{{ parameters }}

在此过程中,我尝试了不同的库存和剧本语法,并尝试了一些尝试使事情正常进行但没有结果。

以下是我在运行 playbook 时收到的一些错误消息。

fatal: [**.**.**.**]: FAILED! => {"msg": "'parameters' is undefined"}
ERROR! could not find 'parameter' key in iterated item '{u'speed': u'...', u'duplex': u'...'}'
fatal: [**.**.**.**]: FAILED! => {"msg": "'list object' has no attribute 'parameter'"}

我做错了什么?

【问题讨论】:

  • 您好,欢迎来到 SO。请阅读stackoverflow.com/help/mcve。如果您不先给出正确的示例,就很难重现错误并帮助您修复它们。还有我的 2 美分 yml:始终使用 yamllint 验证您的文件以修复错误,然后再将它们发布到问题中(您可以使用 pip install yamllint 安装该工具的本地命令行副本。)

标签: variables ansible yaml ansible-inventory


【解决方案1】:

以下是无效的 YAML 语法:

interfaces:
- name: "..."
  port: "..."
  description: "..."
    parameters:
      - speed: "..."
        duplex: "..."

description 不能同时是标量映射。

你可以有

interfaces:
- name: "..."
  port: "..."
  description: "..."

interfaces:
- name: "..."
  port: "..."
  description:
    parameters:
      - speed: "..."
        duplex: "..."

但不是两者兼而有之。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多