ansible_default_ipv4 not getting populated

I setting up kubernetes cluster with 3 master and 5 node.I am deploying through ansible.

I am able to create these node.

I am using ansible 2.2.0.0. on host machine from where I am launching my script.

ansible_default_ipv4 is not getting populated and ansible script is failing while executing the following line.

{{ hostvars[item]['ansible_default_ipv4']['address'] }} {{ item }}

I have feeling that this could be because of environmental issue.
Please help me figure out this issue.

I am using

Linux installterinceptionvm2 4.2.0-30-generic #36~14.04.1-Ubuntu SMP Fri Feb 26 18:49:23 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

What error does the playbook throw?

TASK [common : Hosts | populate inventory into hosts file] *********************
fatal: [inception-vm]: 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/roles/common/tasks/main.yml’: line 52, 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- name: Hosts | populate inventory into hosts file\n ^ here\n”}

ansible -m setup -i inventory/inventory all -a “filter=ipv4

Vikram,

I believe ansible_default_ipv4 is not fetched by ansible in the first go. Try removing the facts of the respective host and running again. Make sure you are gathering the facts first.

-AliJ

HI Ali,

I added gather_facts: yes but no luck

`

---
-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() }}

Thanks kai for your suggestion.
but actual purpose of
hostvars[item][‘ansible_default_ipv4’][‘address’]
Is to add entry in hosts file in each machine.
Do you have any idea why this ansible_default_ipv4 is not available??

I tried to explain that in my previous mail, I'm not sure if I can explain it more clearly than I did.
Maybe someone else can explain it better.

I can try one more go.

You have 13 hosts, but you are only able to reach 1 of them.
You need to fix that so you can log in to all 13 and not just the one host called "inception".
The other 12 hosts you get a UNREACHABLE! if you look at you output, solve them at it will work.

Since 12 of 13 hosts are UNREACHABLE you are not able to log in and collect the facts about them and thats why ansible_default_ipv4 is not available.

Hi Vikram,

Kai is right, now that you have shared your code. You are trying to display default_ipv4_address of all the hosts. Since the hosts are unreachable, Ansible is unable to gather facts, hence it cannot find default_ipv4_address of all the hosts and verily cannot display them. Try to resolve the connectivity issue first.

Thanks
-AliJ