【问题标题】:Ansible - items list from a variable list defined from inventory hostnameAnsible - 从清单主机名定义的变量列表中的项目列表
【发布时间】:2019-09-28 04:00:21
【问题描述】:

我的 host_vars 中有一个名为“bonding”的变量 - 这是要聚合到 bond0 接口中的网络接口列表。这些值是在清单中定义的,并且它们在我的每个主机的变量中正确列出。

production/
├── group_vars
│   └── ipbatch.yaml
├── hosts.yaml
└── host_vars
    ├── ipbatch1.yaml
    ├── ipbatch2.yaml
    └── ipbatch3.yaml

production/host_vars/ipbatch3.yaml 的内容:

---
bonding:
  - eno3
  - eno4

检查此变量是否正确设置:

tasks:
  - name: debug test - hostvars
    debug:
      var: hostvars[inventory_hostname]

输出提取 - 看起来正确:

        "ansible_virtualization_type": "kvm",
        "bonding": [
            "eno3",
            "eno4"
        ],
        "dns": true,
        "ftp": true,

现在,我想在角色中使用这个变量,这样:

  tasks:
    - set_fact:
        interface_bond: "{{ ansible_interfaces | select('match','^bond[0-9]') | sort | list | first }}"
  roles:
    - role: network
      network_ifaces:
      - device: "{{ item }}"
        bondmaster: "{{ interface_bond }}"
      with_items: "{{ hostvars[inventory_hostname][bonding] | list }}"

这是问题:ansible 说我的项目列表是空的。 我正在尝试以这种方式调试我的变量请求:

    - debug:
        var: "{{ hostvars[inventory_hostname][bonding] | list }}"

输出是一条错误消息。但是,错误消息中会显示正确的值:

fatal: [ipbatch2]: FAILED! => {"msg": "ansible.vars.hostvars.HostVarsVars object has no element ['eno1']"}
fatal: [ipbatch1]: FAILED! => {"msg": "ansible.vars.hostvars.HostVarsVars object has no element ['eth0', 'eth1']"}
fatal: [ipbatch3]: FAILED! => {"msg": "ansible.vars.hostvars.HostVarsVars object has no element ['eno3', 'eno4']"}

我尝试过的:

var: "{{ hostvars[inventory_hostname][bonding] | list }}"
var: "{{ bonding }}"
var: "{{ bonding | list }}"
var: "{{ map('extract','hostvars[inventory_hostname]','bonding')| list }}"
var:  "{{ hostvars[inventory_hostname] | map(attribute='bonding') | list }}"
var: "{{ hostvars[inventory_hostname].bonding | list }}"

但是最近的输出,即使是错误的,也是第一行。

预期结果:with_items 语句应返回以太网接口列表,如 host_vars 库存文件中所述

【问题讨论】:

    标签: ansible jinja2 ansible-inventory


    【解决方案1】:

    bonding 是哈希中键的名称(作为字符串),而不是要用作键的 var 的名称。此外,您的 yaml 结构中的bonding 已经是您可以直接访问的列表。在这种情况下不需要使用list 过滤器。

    创建循环的正确语法是以下之一:

    • with_items: "{{ hostvars[inventory_hostname]['bonding'] }}"
    • with_items: "{{ hostvars[inventory_hostname].bonding }}"

    【讨论】:

    • 感谢@Zeitounator 的回答。但是,这些解决方案都不起作用,并且它们都输出相同的错误:TASK [debug] *** ok: [ipbatch3] => { "": "VARIABLE IS NOT DEFINED!" } 任务 [网络:网络 |设置 nics] *** 致命:[ipbatch3]:失败! => {"msg": "'item' 未定义"}
    • 也许我应该改变声明变量的方式以获得一个干净的变量名?
    • 包含角色的方式似乎很奇怪。你能发布你的完整剧本吗?我这包含在剧本roles 部分中,它无法按预期工作。您需要将 include 角色用作 tasks。考虑看看stackoverflow.com/help/mcve。根据经验:您通常会想出自己的解决方案,只是想通过一个简单的示例来尝试重现
    • 实际上我已经删减了部分剧本以使其更短。独立任务在我的角色之前启动。但这应该以这种方式工作:``` - 主机:ipbatch3 变为:true 收集事实:真正的角色:- 角色:网络 network_ifaces:- 设备:“{{ item }}”bondmaster:bond0 with_items:“{{ hostvars[inventory_hostname] .bonding }}" ```
    猜你喜欢
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 2021-10-16
    • 2018-04-13
    • 2021-03-31
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多