该任务包含一个带有未定义变量的选项。错误是:
ansible_all_ipv4_addresses
未定义。
如果我通过 ipv4 连接,为什么会收到此错误?我正试图把它扔掉,
"{{ ansible_all_ipv4_addresses[0] }}"
我可以验证它是否有效,
$ ansible -u centos -m setup 10.1.38.15 | grep ansible_all_ipv4_addresses -A2 -B1
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.16.0.13"
],
但与上面非常相似,
$ ansible -u centos -m debug -a "msg='{{ansible_all_ipv4_addresses}}'" 10.1.38.15
10.1.38.15 | FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'ansible_all_ipv4_addresses' is undefined"
}
答案1
答案2
对我来说,docker中的Ubuntu默认没有iproute2包。
我看到你正在使用 CentOS,但这是 Google 中的第一个链接,所以我认为值得一提这个线程,我实际上找到了答案。
这是我添加到我的剧本中的一个块,以防有人发现它有用:
---
- name: Install iproute2 on ubuntu to gather facts properly
block:
- name: install iproute
apt:
name: iproute2
state: present
- name: Recollect facts
ansible.builtin.setup:
gather_subset:
- all
- debug: msg="{{ hostvars[inventory_hostname]['ansible_all_ipv4_addresses'] }}"
when: ansible_distribution == "Ubuntu"