get hostname from multihost

39 views
Skip to first unread message

Azir Guleroglu

unread,
Feb 4, 2020, 2:56:28 PM2/4/20
to Ansible Project
Hello,

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. I tried to use setup module and with ansible_hostname and registered to variables but I failed.

Thanks in advance

  - name: get hostname
    setup:
      filter: ansible_hostname
  - name: debug
    debug:
      var: ansible_hostname
    register: mongodb1, mongodb2, mongodb3

  - 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



Stefan Hornburg (Racke)

unread,
Feb 4, 2020, 3:58:29 PM2/4/20
to ansible...@googlegroups.com
On 2/4/20 8:56 PM, Azir Guleroglu wrote:
> Hello,
>
> 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. I tried to use setup module and with ansible_hostname
> and registered to variables but I failed.
>
> Thanks in advance

Hello Azir,

it is unclear to me where these hostnames are coming from. And as far as I know you can only register one variable with
register:

Regards
Racke

>
>   - name: get hostname
>     setup:
>       filter: ansible_hostname
>   - name: debug
>     debug:
>       var: ansible_hostname
>     register: mongodb1, mongodb2, mongodb3
>
>   - 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
>
>
>
> --
> 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
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/ad763356-5108-4bea-9f24-239fd2255812%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/ad763356-5108-4bea-9f24-239fd2255812%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Azir Güleroğlu

unread,
Feb 4, 2020, 11:15:59 PM2/4/20
to ansible...@googlegroups.com

Hello Stefan,


I pasted here only part of my playbook. I have 3 remote servers and i want to get their hostnames and assign them to mongo replica set member section. 


For example;


Server 1 hostname; test1

Server 2 hostname; test2

Server 3 hostname; test3


I want to register each hostname to one variable.

Test1 should assign to “mongodb1” variable

Test2 should assign to mongodb2 variable

Test3 should assign to mongosb3 variable

I hope i was able to explain what i want try to do.

Regards
Azir 

Stefan Hornburg (Racke) <ra...@linuxia.de> şunları yazdı (4 Şub 2020 23:58):

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/281c38db-3db7-4dc8-649d-eb61dbfd3198%40linuxia.de.

Vladimir Botka

unread,
Feb 5, 2020, 3:02:23 AM2/5/20
to Azir Guleroglu, ansible...@googlegroups.com
On Tue, 4 Feb 2020 11:56:28 -0800 (PST)
Azir Guleroglu <azirgu...@gmail.com> 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

Azir Güleroğlu

unread,
Feb 5, 2020, 8:06:08 AM2/5/20
to Vladimir Botka, ansible...@googlegroups.com
Thanks Vladimir, I'll check them out. 

Vladimir Botka <vbo...@gmail.com>, 5 Şub 2020 Çar, 11:02 tarihinde şunu yazdı:
Reply all
Reply to author
Forward
0 new messages