【问题标题】:Using Dict with Ansible在 Ansible 中使用字典
【发布时间】:2022-11-22 03:30:05
【问题描述】:

我正在尝试创建一个循环,使用 Ansible 从字典中提取数据并创建资源(例如组),基于 https://docs.ansible.com/ansible/2.9/plugins/lookup/dict.html

# main.yaml
- name: 'Linux | Adding groups'
  ansible.builtin.group:
    name: "{{ item.key }}"
    state: "{{ item.value.state }}"
    gid: "{{ item.value.gid }}"
    system: False
  loop: "{{ lookup('dict', groups) }}"
# vars.yaml
groups:
  my_group:
    state: present
    gid: 1004

上面的代码总是返回以下错误:

fatal: [master-1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'state'\n\nThe error appears to be in 'path': line 3, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: 'Linux | Adding groups'\n  ^ here\n"}

有什么问题的线索吗?

版本:ansible [core 2.13.6]

【问题讨论】:

  • 不要将您的群组称为groups,这是一个保留变量。

标签: ansible


【解决方案1】:

您正在寻找 dict2items 过滤器。

您还遇到了您的变量使用保留字 groups 的问题。

因此,如果您将变量重命名为 linux_groups,您的任务将变为:

- name: 'Linux | Adding groups'
  ansible.builtin.group:
    name: "{{ item.key }}"
    state: "{{ item.value.state }}"
    gid: "{{ item.value.gid }}"
    system: False
  loop: "{{ linux_groups | dict2items }}"

【讨论】:

    猜你喜欢
    • 2016-08-19
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2021-11-16
    • 2021-01-31
    • 2017-05-25
    • 1970-01-01
    • 2020-01-16
    相关资源
    最近更新 更多