#
# Build hosts file
#
hostname {{ inventory_hostname }}
cat <<SCRIPT >/tmp/hosts
#
# Generated by netlab initial
#
127.0.0.1 {{ inventory_hostname }}

{% for k,v in hostvars.items() if k != inventory_hostname and v.af.ipv4|default(False) %}
{%- if v.loopback.ipv4 is defined %}{{ v.loopback.ipv4|ipaddr('address') }} {% endif %}
{%- for l in v.interfaces|default([]) if 'ipv4' in l and l.ipv4 != True and l.ipv4|ipv4 %}{{ l.ipv4|ipaddr('address') }} {% endfor %}{{ k }}
{% endfor %}
SCRIPT
grep "netlab initial" /etc/hosts || uniq /tmp/hosts >>/etc/hosts

