3

I'm building a couple of roles where I need to give the config the IP addresses of all hosts in a specified group as a comma separated string.

Is there a jinja2 combination of filters (like map, match, select, join etc) that produce the same as this:

{% set comma = joiner(",") %}
{% for hostname in groups['mongodb'] %}
{{ comma() }}{{ hostvars[hostname].ansible_default_ipv4.address }}
{%- endfor %}

1 Answer 1

6

There is almost exact answer in the documentation...

Slight modification, and here you go:

- debug:
    msg: "{{ groups['mongodb'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | join(',') }}"
1
  • That's very cool. Hypothetically, how could we have a default value incase one of the host doesn't have the object ?
    – Venu S
    Mar 13, 2021 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.