I have some tasks like:
`
-
debug: var=hostvars[item]
with_items: groups.rabbitmq_server -
name: Update /etc/hosts with rabbitmq-server hostnames
lineinfile:
dest: /etc/hosts
regexp: “^{{hostvars[item][‘ansible_default_ipv4’][‘address’]}} {{hostvars[item][‘ansible_hostname’]}}$”
line: “{{hostvars[item][‘ansible_default_ipv4’][‘address’]}} {{hostvars[item][‘ansible_hostname’]}}”
state: present
with_items: groups.rabbitmq_server
`
When the second runs I get:
TASK: [Update /etc/hosts with rabbitmq-server hostnames] **********************
fatal: [10.0.136.70] => One or more undefined variables: ‘dict object’ has no attribute ‘ansible_default_ipv4’
fatal: [10.0.137.195] => One or more undefined variables: ‘dict object’ has no attribute ‘ansible_default_ipv4’
But just in the debug above I have:
`
“ansible_default_ipv4”: {
“address”: “10.0.137.195”,
“alias”: “eth0”,
“gateway”: “10.0.137.1”,
“interface”: “eth0”,
“macaddress”: “0e:ab:ba:38:c4:a1”,
“mtu”: 9001,
“netmask”: “255.255.255.0”,
“network”: “10.0.137.0”,
“type”: “ether”
},
`
( I used to reference the hostvars like hostvars[item].ansible_default_ipv4.address but tried using just out of frustration. )