希望有人能拯救我的理智,因为我一直在谷歌上搜索这个问题,却无功而返。看起来应该很简单。我正尝试使用 Ansible 事实收集主机列表上所有接口的双工。幸运的是,我们到处都有 chef,ohai 事实包含此信息。
我的问题是我不知道如何循环遍历接口来打印此双工信息。具体来说,我需要打印事实 [ohai_network.interfaces.{interface_name}.duplex]。以下是我尝试过的方法:
tasks:
- name: Print interface duplex
when: "{{ hostvars[ansible_fqdn]['ansible_'~item]['ipv4'] is defined }}"
debug:
msg="{{ ansible_hostname }} {{ item }} interface duplex is {{ ohai_network.interfaces.[item].duplex }}"
with_items:
- "{{ ansible_interfaces | map('replace', '-','_') | list }}"
它似乎不认可我将 [item] 变量放入事实引用中。任何帮助都将不胜感激!
更新 - “map('replace', '-','_') | list”部分是从其他地方抓取的。我发现没有它输出似乎也很好。我还修改了事实引用的格式,因此剧本现在看起来像这样:
tasks:
- name: Print interface duplex
when: "{{ hostvars[ansible_fqdn]['ansible_'~item]['ipv4'] is defined }}"
debug:
msg="{{ ansible_hostname }} {{ item }} interface duplex is {{ ohai_network['interfaces'][item]['duplex'] }}"
with_items:
- "{{ ansible_interfaces }}"
我仍然收到错误:
exception type: <class 'ansible.errors.AnsibleUndefinedVariable'>
exception: 'dict object' has no attribute 'duplex'
以下是所要求的 var 字符串的输出:
TASK [Print interface duplex] ********************************************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{
hostvars[ansible_fqdn]['ansible_'~item]['ipv4'] is defined }}
ok: [REDACTED] => (item=lo) => {
"changed": false,
"item": "lo",
"ohai_network.interfaces": {
"ens160": {
"addresses": {
"00:00:00:00:00:01": {
"family": "lladdr"
},
"10.204.76.244": {
"broadcast": "10.0.0.255",
"family": "inet",
"netmask": "255.0.0.0",
"prefixlen": "8",
"scope": "Global"
}
},
"arp": {
"10.204.76.1": "00:00:00:00:00:01",
"10.204.76.3": "00:00:00:00:00:01"
},
"auto_negotiation": "off",
"duplex": "Full",
"encapsulation": "Ethernet",
"flags": [
"BROADCAST",
"MULTICAST",
"UP",
"LOWER_UP"
],
"link_speed": 10000,
"mdi_x": "Unknown",
"mtu": "1500",
"number": "0",
"port": "Twisted Pair",
"ring_params": {
"current_rx": 256,
"current_rx_jumbo": 128,
"current_rx_mini": 0,
"current_tx": 512,
"max_rx": 4096,
"max_rx_jumbo": 4096,
"max_rx_mini": 0,
"max_tx": 4096
},
"routes": [
{
"destination": "default",
"family": "inet",
"metric": "100",
"proto": "static",
"via": "10.0.0.1"
},
{
"destination": "10.0.0.0/8",
"family": "inet",
"metric": "100",
"proto": "kernel",
"scope": "link",
"src": "10.0.0.2"
}
],
"state": "up",
"transceiver": "internal",
"type": "ens16"
},
"lo": {
"addresses": {
"127.0.0.1": {
"family": "inet",
"netmask": "255.0.0.0",
"prefixlen": "8",
"scope": "Node"
}
},
"encapsulation": "Loopback",
"flags": [
"LOOPBACK",
"UP",
"LOWER_UP"
],
"mtu": "65536",
"state": "unknown"
}
}
}
ok: [REDACTED] => (item=ens160) => {
"changed": false,
"item": "ens160",
"ohai_network.interfaces": {
"ens160": {
"addresses": {
"00:00:00:00:00:01": {
"family": "lladdr"
},
"10.204.76.244": {
"broadcast": "10.0.0.255",
"family": "inet",
"netmask": "255.0.0.0",
"prefixlen": "8",
"scope": "Global"
}
},
"arp": {
"10.0.0.1": "00:00:00:00:00:01",
"10.0.0.2": "00:00:00:00:00:01"
},
"auto_negotiation": "off",
"duplex": "Full",
"encapsulation": "Ethernet",
"flags": [
"BROADCAST",
"MULTICAST",
"UP",
"LOWER_UP"
],
"link_speed": 10000,
"mdi_x": "Unknown",
"mtu": "1500",
"number": "0",
"port": "Twisted Pair",
"ring_params": {
"current_rx": 256,
"current_rx_jumbo": 128,
"current_rx_mini": 0,
"current_tx": 512,
"max_rx": 4096,
"max_rx_jumbo": 4096,
"max_rx_mini": 0,
"max_tx": 4096
},
"routes": [
{
"destination": "default",
"family": "inet",
"metric": "100",
"proto": "static",
"via": "10.0.0.1"
},
{
"destination": "10.0.0.0/8",
"family": "inet",
"metric": "100",
"proto": "kernel",
"scope": "link",
"src": "10.0.0.2"
}
],
"state": "up",
"transceiver": "internal",
"type": "ens16"
},
"lo": {
"addresses": {
"127.0.0.1": {
"family": "inet",
"netmask": "255.0.0.0",
"prefixlen": "8",
"scope": "Node"
}
},
"encapsulation": "Loopback",
"flags": [
"LOOPBACK",
"UP",
"LOWER_UP"
],
"mtu": "65536",
"state": "unknown"
}
}
}
答案1
有点难以弄清楚这里发生了什么。我不清楚“ohai_network.interfaces”如何在您的主机变量中公开。如果您能够添加打印主机变量的任务(- debug:var=hostvars[inventory_hostname]),然后将该输出中的 ohai 部分添加到您的问题中,我应该能够为您提供完整的解决方案。
下面的片段应该可以帮助您解决目前遇到的一些错误,但可能无法解决 ohai 数据:
# As per the warning in your output, 'when' clauses should not be wrapped with curly braces. Also better to use 'inventory_hostname' in this case
- name: Print interface duplex
debug:
msg: "{{ ansible_hostname }} {{ item }} interface duplex is {{ ohai_network['interfaces'][item]['duplex'] | default('not_defined') }}"
when: hostvars[inventory_hostname]['ansible_' + item]['ipv4'] is defined
with_items: "{{ ansible_interfaces }}"
如果这不起作用,请尝试:
msg: "{{ ansible_hostname }} {{ item }} interface duplex is {{ ohai_network.interfaces[item]['duplex'] | default('not_defined') }}"