Skip to content

Instantly share code, notes, and snippets.

@kustodian
Created March 23, 2017 09:21
Show Gist options
  • Save kustodian/385f70fa7b55a01ccd8aff2f59bd4108 to your computer and use it in GitHub Desktop.
Save kustodian/385f70fa7b55a01ccd8aff2f59bd4108 to your computer and use it in GitHub Desktop.
Ansible - Use template lookup in a role to generate a list of IP addresses from a hostgroup
{% if myhostgroup != '' %}
{% for host in groups[myhostgroup] %}
- {{ hostvars[host]["ansible_default_ipv4"]["address"] }}
{% endfor %}
{% endif %}
---
- hosts: myhosts
vars:
hostgroup_ips: '{{ lookup("template", "hostgroup_ips.j2") | from_yaml }}'
tasks:
- debug:
var: '{{ hostgroup_ips|join(",") }}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment