我的host_vars中有一个名为"bonding“的变量--这是要聚合到bond0接口中的网络接口列表。这些值是在清单中定义的,它们正确地列在我的每个主机的变量中。
production/
├── group_vars
│ └── ipbatch.yaml
├── hosts.yaml
└── host_vars
├── ipbatch1.yaml
├── ipbatch2.yaml
└── ipbatch3.yaml生产内容/主机_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库存文件中所述
发布于 2019-05-10 09:30:09
bonding是散列中键的名称(作为字符串),而不是要用作键的变量的名称。此外,yaml结构中的bonding已经是您直接访问的列表。在这种情况下,不需要使用list过滤器。
创建循环的正确语法为:
with_items: "{{ hostvars[inventory_hostname]['bonding'] }}"with_items: "{{ hostvars[inventory_hostname].bonding }}"https://stackoverflow.com/questions/56074277
复制相似问题