printjson(rs.initiate( { "_id" : "{{ mongodb_replication_set_name }}", "version" : 1, "members" : [ {% for host in groups["mongodb"] %} { "_id" : {{ loop.index }}, "host" : "{{ hostvars[host]['ansible_' + hostvars[host]['api_interface']]['ipv4']['address'] }}:{{ mongodb_port }}" }{% if not loop.last %},{% endif %} {% endfor %} ] } ))