title: How to dynamically generate a list of MongoDB replica set members with Ansible
body: <p>I'm using Ansible to create a MongoDB replica set. I'm following the <code>mongodb_replicaset</code> module, example:</p>
  
  <pre><code># Create a replicaset called 'rs0' with the 3 provided members
  - name: Ensure replicaset rs0 exists
    mongodb_replicaset:
      login_host: localhost
      login_user: admin
      login_password: ?????
      replica_set: rs0
      members:
      - mongodb1:27017
      - mongodb2:27017
      - mongodb3:27017
    when: groups.mongod.index(inventory_hostname) == 0
  </code></pre>
  
  <p>(From the docs <a href="https://docs.ansible.com/ansible/latest/modules/mongodb_replicaset_module.html" rel="nofollow noreferrer">https://docs.ansible.com/ansible/latest/modules/mongodb_replicaset_module.html</a>)</p>
  
  <p>I've got my replication servers defined in the hosts inventory file</p>
  
  <pre><code>[replication_servers]
  mongodb1 ansible_host=192.168.50.4
  mongodb2 ansible_host=192.168.50.5
  mongodb3 ansible_host=192.168.50.6
  </code></pre>
  
  <p>This works ok, but I'd like to avoid hardcoding (and duplicating) the host names in the <code>members</code> attribute, and somehow use the already defined <code>[replication_servers]</code> group.
  Is this possible with Ansible?</p>