# How to get another host's IP/Hostname from the inventory as a variable?

**URL:** <https://forum.ansible.com/t/how-to-get-another-hosts-ip-hostname-from-the-inventory-as-a-variable/2125>\
**Category:** Get Help\
**Tags:** inventory\
**Created:** [November 3, 2023, 5:15am UTC](https://forum.ansible.com/t/how-to-get-another-hosts-ip-hostname-from-the-inventory-as-a-variable/2125 "2023-11-03T05:15:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chatguy](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@chatguy](https://forum.ansible.com/u/chatguy)\
**Post date:** [November 3, 2023, 5:15am UTC](https://forum.ansible.com/t/how-to-get-another-hosts-ip-hostname-from-the-inventory-as-a-variable/2125/1 "2023-11-03T05:15:08Z")

</div>

Hi all,

I’ve had a hard time finding the answer to this question online. Hoping someone can help here pls:

**What variable can I use to access the “mongodb” IP address from my inventory file?**

ie: What variable can I use: {{ ?? }} that would == 10.1.1.5 ?

```auto
[redis]
10.1.1.9

[rabbitmq]
10.1.1.12

[mongodb]
10.1.1.5

```

Thanks so much!  
CG

---

<div class="post-metadata">

**Author:** ![devnull](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@devnull](https://forum.ansible.com/u/devnull)\
**Post date:** [November 3, 2023, 9:10am UTC](https://forum.ansible.com/t/how-to-get-another-hosts-ip-hostname-from-the-inventory-as-a-variable/2125/2 "2023-11-03T09:10:29Z")

</div>

Did you try `groups['mongodb'][0]` or `groups.mongodb[0]` ?

Using this task

```auto
    - name: mongodb host
      debug:
        msg: "{{ groups['mongodb'][0] }}"

```

Gives this output:

```auto
TASK [mongodb host] *************************************
ok: [10.1.1.9] => {
    "msg": "10.1.1.5"
}

```

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [November 3, 2023, 10:42am UTC](https://forum.ansible.com/t/how-to-get-another-hosts-ip-hostname-from-the-inventory-as-a-variable/2125/3 "2023-11-03T10:42:14Z")

</div>

Hi,

Another suggestion would be to use [inventory\_hostnames lookup plugin](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/inventory_hostnames_lookup.html):

```yaml
- name: test
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - ansible.builtin.debug:
        msg: "{{ item }}"
      with_inventory_hostnames: mongodb

```

```bash
$ ansible-playbook -i ./inv ./pbtest.yml
...
TASK [ansible.builtin.debug] ***********************************************************************************************************************************************************************************************************************************************************************************************
Friday 03 November 2023 11:40:57 +0100 (0:00:00.009) 0:00:00.009 ******* 
ok: [localhost] => (item=10.1.1.5) => {
    "msg": "10.1.1.5"
}
...

```

---

<div class="post-metadata">

**Author:** ![chatguy](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@chatguy](https://forum.ansible.com/u/chatguy)\
**Post date:** [November 3, 2023, 4:02pm UTC](https://forum.ansible.com/t/how-to-get-another-hosts-ip-hostname-from-the-inventory-as-a-variable/2125/4 "2023-11-03T16:02:18Z")

</div>

Thank you, devnull, this works great!  
…Your solution is perfect for my scenario – since I need to only use the first host entry.

…Others who are reading this may also be interested in ptn’s solution, in case they need to process for each host entry. Thank you also to ptn!

---

<div class="post-metadata">

**Author:** ![devnull](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@devnull](https://forum.ansible.com/u/devnull)\
**Post date:** [November 5, 2023, 11:11pm UTC](https://forum.ansible.com/t/how-to-get-another-hosts-ip-hostname-from-the-inventory-as-a-variable/2125/5 "2023-11-05T23:11:53Z")

</div>

Hi @chatguy

good to hear it’s working fine for you 👍🏼

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 5, 2023, 11:12pm UTC](https://forum.ansible.com/t/how-to-get-another-hosts-ip-hostname-from-the-inventory-as-a-variable/2125/6 "2023-12-05T23:12:44Z")

</div>

Closed on December 5, 2023, 11:12pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
