使用 Jinja2 过滤器获取组中主机的 IP 列表

Get list of IPs of hosts in group with Jinja2 filter

我正在构建几个角色,我需要将指定组中所有主机的 IP 地址作为逗号分隔字符串提供给配置。

是否有 jinja2 过滤器组合(如 map、match、select、join 等)产生与此相同的结果:

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

documentation...

中有几乎准确的答案

稍作修改,开始:

- debug:
    msg: "{{ groups['mongodb'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | join(',') }}"