ansible - Ansible 主机组和过滤 IP 的问题
问题描述
我在循环访问 Ansible 组中的主机列表时遇到问题,我得到了我想要的值,但我得到了虚假错误,我无法弄清楚。本来以为如果有错误它根本不应该打印这些值:
fatal: [worker01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_eth1'\n\nThe error appears to be in 'roles/vagrant/tasks/main.yml': line 15, 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- debug:\n ^ here\n"}
从 vagrant 文件中提取
ansible.groups = {
"workers" => ["worker01", "worker02", "worker03"],
"controllers" => ["controller01", "controller02"],
"kubernetes" => ["kubernetes"],
"all_groups:children" => ["workers", "controllers", "kubernetes"]
}
vagrant 使用的示例主机定义
{
"name": "worker03",
"alias": "worker03",
"box": "bento/ubuntu-18.04",
"memory": 2048,
"vcpu": 2,
"provider": "virtualbox",
"autostart": "false",
"cpus": 2,
"cpu_percentage": 100,
"Controller": "SATA Controller",
"lv_disks": 4,
"pv_size": 5,
"infra_ip_addr": "10.2.15.60",
"service_ip_addr": "10.96.0.60",
"pod_ip_addr": "192.168.0.60",
"port_forwards": {},
"project": "wks",
"tags": "all",
"group": "workers",
"extra_vars": {}
}
以下代码:
- debug:
msg: "External: {{ hostvars[item]['ansible_eth1']['ipv4']['address'] }} ; Service: {{ hostvars[inventory_hostname]['ansible_eth3']['ipv4']['address'] }} "
with_items:
- "{{ groups['workers'] }}"
产生输出 - 注意错误“ansible.vars.hostvars.HostVarsVars object”没有属性“ansible_eth1”
ok: [worker01] => (item=worker01) => {
"msg": "External: 10.2.15.40 ; Service: 192.168.0.40 "
}
fatal: [worker01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_eth1'\n\nThe error appears to be in 'roles/vagrant/tasks/main.yml': line 15, 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- debug:\n ^ here\n"}
PLAY RECAP ****************************************************************************************************************************************************************************************************************************
worker01 : ok=3 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
解决方案
在现代 Ubuntu 映像上,接口名称很愚蠢(enp0s31f6
例如wlp0s20f3
);您想要的是使用ansible_default_ipv4
而不是命名特定接口(或者,当然,您可以进一步循环遍历所有发现的接口,但对于您正在做的事情,这似乎不是您想要的)
- debug:
msg: "External: {{ hostvars[item]['ansible_default_ipv4']['address'] }} ; Service: {{ hostvars[inventory_hostname]['ansible_eth3']['ipv4']['address'] }} "
with_items:
- "{{ groups['workers'] }}"
虽然这将解决您的特定问题,但对您未来的调试帮助更一般的建议是使用debug: var=hostvars
以查看可用的值,因为错误
'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_eth1'
不可能更明确地说明问题所在。
推荐阅读
- javascript - PHP http错误不允许访问
- python - 努力迭代 Django 2.0 中的属性
- android - android studio 3.1.2:gradle 同步失败:在 ubuntu 16.04 上读取超时
- build - Nuget 命令行
- python - 困惑于 PyCharm、WinPython 和 IPython
- java - Codecheck 不让我通过
- javascript - React:使用最小 HTML 模板中的类
- angular - 使用 2 个 json 的 Angular ngprime 表
- jekyll - 为什么我的帖子上没有降价文字
- group-by - ax2012 ssrs 基于参数的分组条件