---
-hosts:all
gather_facts:yes
tasks:
-name:vikram
# debug: msg="vikram"
#debug: msg={{ hostvars[item] }}
debug:msg={{hostvars[item]['ansible_default_ipv4']['address']}}
with_items:
-"{{ groups['all'] }}"
*Output:*
TASK
[setup]*******************************************************************
fatal:[openam-instance]:UNREACHABLE!=>{"changed":false,"msg":"Failed to
connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
fatal:[kuberenetes-node-2]:UNREACHABLE!=>{"changed":false,"msg":"Failed
to connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
fatal:[kuberenetes-node-3]:UNREACHABLE!=>{"changed":false,"msg":"Failed
to connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
fatal:[kuberenetes-node-4]:UNREACHABLE!=>{"changed":false,"msg":"Failed
to connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
fatal:[kuberenetes-node-5]:UNREACHABLE!=>{"changed":false,"msg":"Failed
to connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
fatal:[kuberenetes-master-2]:UNREACHABLE!=>{"changed":false,"msg":"Failed to
connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
fatal:[kuberenetes-master-3]:UNREACHABLE!=>{"changed":false,"msg":"Failed to
connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
fatal:[edge-instance-1]:UNREACHABLE!=>{"changed":false,"msg":"Failed to
connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
fatal:[kuberenetes-master-1]:UNREACHABLE!=>{"changed":false,"msg":"Failed to
connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
fatal:[kuberenetes-node-1]:UNREACHABLE!=>{"changed":false,"msg":"Failed
to connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
ok:[inception]
fatal:[edge-instance-2]:UNREACHABLE!=>{"changed":false,"msg":"Failed to
connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
fatal:[opendj-instance]:UNREACHABLE!=>{"changed":false,"msg":"Failed to
connect to the host via ssh: Connection timed out during banner
exchange\r\n","unreachable":true}
This is the fact gathering step, as you can see from the output, only 1 of 13 hosts are reachable
TASK
[vikram]******************************************************************
fatal:[inception]:FAILED!=>{"failed":true,"msg":"the field 'args' has an
invalid value, which appears to include a variable that is undefined.
The error was: 'dict object' has no attribute
'ansible_default_ipv4'\n\nThe error appears to have been in
'/home/ubuntu/ansible/test1.yml': line 7, column 5, but may\nbe
elsewhere in the file depending on the exact syntax problem.\n\nThe
offending line appears to be:\n\n\n - name: vikram\n ^ here\n"}
to retry,use:--limit @/home/ubuntu/ansible/test1.retry
Since you have 12 unreachable host your loop will have 12 undefined variables.
In the first iteration
hostvars[item]['ansible_default_ipv4']['address']
will be
hostvars['openam-instance']['ansible_default_ipv4']['address']
Since host openam-instance is unreachable this is a undefined variable.
To make it not fail you need to filter it through the default filter.
debug: msg={{ hostvars[item]['ansible_default_ipv4']['address'] | deffault() }}