I'm using Ansible to create a MongoDB replica set. I'm following the mongodb_replicaset
module, example:
# 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
(From the docs https://docs.ansible.com/ansible/latest/modules/mongodb_replicaset_module.html )
I've got my replication servers defined in the hosts inventory file
[replication_servers]
mongodb1 ansible_host=192.168.50.4
mongodb2 ansible_host=192.168.50.5
mongodb3 ansible_host=192.168.50.6
This works ok, but I'd like to avoid hardcoding (and duplicating) the host names in the members
attribute, and somehow use the already defined [replication_servers]
group. Is this possible with Ansible?
Quoting members : "... Supply as a simple csv string, ie mongodb1:27017,mongodb2:27017,mongodb3:27017. If a port number is not provided then 27017 is assumed."
Try
members: "{{ groups.replication_servers|join(',') }}"
This will create simple csv string (port 27017 is default)
members: "mongodb1,mongodb2,mongodb3"
Create replicaset with ports
It's possible to create the string with ports. For example, with the inventory below
[replication_servers]
mongodb1 ansible_host=192.168.50.4 mongodb_port=27017
mongodb2 ansible_host=192.168.50.5 mongodb_port=27017
mongodb3 ansible_host=192.168.50.6 mongodb_port=27017
this expression
members: "{{ groups.replication_servers|
zip(groups.replication_servers|
map('extract', hostvars, 'mongodb_port')|
list)|
map('join', ':')|
join(',') }}"
gives
members: "mongodb1:27017,mongodb2:27017,mongodb3:27017"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.