On Tue, 4 Feb 2020 11:56:28 -0800 (PST) Azir Guleroglu <[email protected]> wrote: > I try to automate MongoDB installation with Ansible, but i can't create a > replica set members automatically. > I want to get automatically 3 host's hostnames and add to members. > [...] > > - name: replica set > mongodb_replicaset: > login_host: localhost > login_user: admin > login_password: test > replica_set: rs0 > members: > - {{ mongodb1 }}:27017 > - {{ mongodb2 }}:27017 > - {{ mongodb3 }}:27017 > when: groups.mongod.index(inventory_hostname) == 0
Let's assume we have the inventory
$ cat hosts
[rs0]
test_01
test_02
test_03
The playbook below should do the job
- hosts: rs0
tasks:
- set_fact:
mongodb_members: "{{ [0,1,2]|map('extract', groups.rs0)|list }}"
run_once: true
- name: replica set
mongodb_replicaset:
login_host: localhost
login_user: admin
login_password: test
replica_set: rs0
members: "{{ mongodb_members }}"
when: groups.rs0.index(inventory_hostname) == 0
Notes
1) See "Selecting values from arrays or hashtables"
https://docs.ansible.com/ansible/devel/user_guide/playbooks_filters.html#selecting-values-from-arrays-or-hashtables
First 3 members of the group rs0 are replicaset members.
2) See "mongodb_replicaset – Initialises a MongoDB replicaset"
https://docs.ansible.com/ansible/latest/modules/mongodb_replicaset_module.html
members:
- A comma-separated string or a yaml list consisting of the replicaset
members.
- If a port number is not provided then 27017 is assumed.
3) The Ansible task "replica set" will run on the first member of the group
rs0.
HTH,
-vlado
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/20200205090204.191721a8%40gmail.com.
pgpFcz8rJ_vTm.pgp
Description: OpenPGP digital signature
