mon host = {% for host in groups[mon_group_name] %} {% for key in hostvars[host].iterkeys() %} {% if hostvars[host][key]['macaddress'] is defined and hostvars[host][key]['device'] is defined and hostvars[host][key]['device'] == monitor_interface %} {{ hostvars[host][key]['ipv4']['address'] }} {% endif %} {% endfor %} {% if not loop.last %},{% endif %}{% endfor %}