# get hostname from multihost

**URL:** <https://forum.ansible.com/t/get-hostname-from-multihost/32136>\
**Category:** Ansible Project\
**Created:** [February 4, 2020, 7:56pm UTC](https://forum.ansible.com/t/get-hostname-from-multihost/32136 "2020-02-04T19:56:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azir\_Guleroglu](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Azir\_Guleroglu](https://forum.ansible.com/u/Azir_Guleroglu)\
**Post date:** [February 4, 2020, 7:56pm UTC](https://forum.ansible.com/t/get-hostname-from-multihost/32136/1 "2020-02-04T19:56:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 4, 2020, 8:58pm UTC](https://forum.ansible.com/t/get-hostname-from-multihost/32136/2 "2020-02-04T20:58:01Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Azir\_Guleroglu](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Azir\_Guleroglu](https://forum.ansible.com/u/Azir_Guleroglu)\
**Post date:** [February 5, 2020, 4:15am UTC](https://forum.ansible.com/t/get-hostname-from-multihost/32136/3 "2020-02-05T04:15:41Z")

</div>

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) \<[racke@linuxia.de](mailto:racke@linuxia.de)\> şunları yazdı (4 Şub 2020 23:58):

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 5, 2020, 8:02am UTC](https://forum.ansible.com/t/get-hostname-from-multihost/32136/4 "2020-02-05T08:02:04Z")

</div>

Let's assume we have the inventory

&nbsp;&nbsp;$ cat hosts  
&nbsp;&nbsp;[rs0]  
&nbsp;&nbsp;test\_01  
&nbsp;&nbsp;test\_02  
&nbsp;&nbsp;test\_03

The playbook below should do the job

&nbsp;&nbsp;- hosts: rs0  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mongodb\_members: "{{ [0,1,2]|map('extract', groups.rs0)|list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: replica set  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mongodb\_replicaset:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;login\_host: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;login\_user: admin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;login\_password: test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replica\_set: rs0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;members: "{{ mongodb\_members }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: groups.rs0.index(inventory\_hostname) == 0

Notes

1) See "Selecting values from arrays or hashtables"  
&nbsp;&nbsp;&nbsp;[https://docs.ansible.com/ansible/devel/user\_guide/playbooks\_filters.html#selecting-values-from-arrays-or-hashtables](https://docs.ansible.com/ansible/devel/user_guide/playbooks_filters.html#selecting-values-from-arrays-or-hashtables)  
&nbsp;&nbsp;&nbsp;First 3 members of the group rs0 are replicaset members.

2) See "mongodb\_replicaset – Initialises a MongoDB replicaset"  
&nbsp;&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/modules/mongodb\_replicaset\_module.html](https://docs.ansible.com/ansible/latest/modules/mongodb_replicaset_module.html)  
&nbsp;&nbsp;&nbsp;members:  
&nbsp;&nbsp;&nbsp;- A comma-separated string or a yaml list consisting of the replicaset  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;members.  
&nbsp;&nbsp;&nbsp;- 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  
&nbsp;&nbsp;&nbsp;rs0.

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Azir\_Guleroglu](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Azir\_Guleroglu](https://forum.ansible.com/u/Azir_Guleroglu)\
**Post date:** [February 5, 2020, 1:05pm UTC](https://forum.ansible.com/t/get-hostname-from-multihost/32136/5 "2020-02-05T13:05:38Z")

</div>

Thanks Vladimir, I’ll check them out.

Vladimir Botka \<[vbotka@gmail.com](mailto:vbotka@gmail.com)\>, 5 Şub 2020 Çar, 11:02 tarihinde şunu yazdı:
