Skip to content

Instantly share code, notes, and snippets.

@agile
Last active August 29, 2015 13:55
Show Gist options
  • Save agile/8744475 to your computer and use it in GitHub Desktop.
Save agile/8744475 to your computer and use it in GitHub Desktop.
# {{ansible_managed}}
127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
{% for item in groups['all'] %}
{% set ip = next(ip for ip in hostvars[item]['ansible_all_ipv4_addresses'] if ip.find("10.") == 0, hostvars[item]['ansible_default_ipv4']) %}
{% if ip %}
{{ ip }} {{ item }}
{% endif %}
{% endfor %}
>>> hostvars = { 'fred': { 'ansible_all_ipv4_addresses': ["10.123.123.123", "184.123.123.123"] } }
>>> item = 'fred'
>>> next((ip for ip in hostvars[item]['ansible_all_ipv4_addresses'] if ip.find("10.") == 0), None)
'10.123.123.123'
fatal: [somehost] => {'msg': "file: /some/path/templates/hosts.j2, line number: 5, error: expected token ',', got 'for'", 'failed': True}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment