# How can i get ip addresses of group hostnames

**URL:** <https://forum.ansible.com/t/how-can-i-get-ip-addresses-of-group-hostnames/33073>\
**Category:** Ansible Project\
**Created:** [June 2, 2020, 9:45am UTC](https://forum.ansible.com/t/how-can-i-get-ip-addresses-of-group-hostnames/33073 "2020-06-02T09:45:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arko\_Kost](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@Arko\_Kost](https://forum.ansible.com/u/Arko_Kost)\
**Post date:** [June 2, 2020, 9:45am UTC](https://forum.ansible.com/t/how-can-i-get-ip-addresses-of-group-hostnames/33073/1 "2020-06-02T09:45:41Z")

</div>

I can’t find anywhere, how can i get ip addresses of hosts in specific group

```auto

- hosts: opmon 
  tasks:
  - lineinfile:
       create: true
       line: "host\t'op-monitor'\topmonitor\t{{ item}}/24\t\tmd5"
       dest: /tmp/groups.txt 
       with_items: "{{ groups.nodes }}"

```

This playbook prints  
``

`host 'op-monitor' opmonitor node1.itarchive.home/24 md5`

to pg\_hba.conf  
I need, that node1.itarchive.home will be in IP  
Something like that:

`host 'op-monitor' opmonitor 192.168.0.95/24 md5'`

---

<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:** [June 2, 2020, 10:27am UTC](https://forum.ansible.com/t/how-can-i-get-ip-addresses-of-group-hostnames/33073/2 "2020-06-02T10:27:49Z")

</div>

> ... get ip addresses of hosts in specific group  
> - hosts: opmon
> 
> I need, that node1.itarchive.home will be in IP  
> Something like that:  
> host 'op-monitor' opmonitor 192.168.0.95/24 md5'

As a hint, try "template"

&nbsp;&nbsp;\> cat pg\_hba.conf.j2  
&nbsp;&nbsp;{% for host in groups['opmon'] %}  
&nbsp;&nbsp;host {{ host }} opmonitor {{ hostvars[host]['ansible\_all\_ipv4\_addresses'] }}  
&nbsp;&nbsp;{% endfor %}

and playbook

&nbsp;&nbsp;\> cat pb.yml  
&nbsp;&nbsp;- hosts: opmon  
&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: pg\_hba.conf.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/pg\_hba.conf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

Take a look at the facts collected by "setup" and fit the template to your  
needs. See what facts are available

&nbsp;&nbsp;\> ansible remote\_host -m setup

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Arko\_Kost](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@Arko\_Kost](https://forum.ansible.com/u/Arko_Kost)\
**Post date:** [June 2, 2020, 10:33am UTC](https://forum.ansible.com/t/how-can-i-get-ip-addresses-of-group-hostnames/33073/3 "2020-06-02T10:33:02Z")

</div>

I’ve found a working one, it does work on another cluster, but doesn’t work in my environment.

Template:  
{% for item in groups[‘nodes’]-%}  
{{ hostvars[item][‘ansible\_default\_ipv4’][‘address’] }}  
{% endfor %}

- hosts: opmon  
tasks:
- name: Configure pg\_hba  
template:  
src: test.j2  
dest: /tmp/groups.txt

It works on another environment, but in my environment it goes fatal: [xroad-a.itarchive.home]: FAILED! =\> {“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘ansible.vars.hostvars.HostVarsVars object’ has no attribute ‘ansible\_default\_ipv4’”}

teisipäev, 2. juuni 2020 13:28.10 UTC+3 kirjutas Vladimir Botka:

---

<div class="post-metadata">

**Author:** ![Arko\_Kost](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@Arko\_Kost](https://forum.ansible.com/u/Arko_Kost)\
**Post date:** [June 2, 2020, 10:37am UTC](https://forum.ansible.com/t/how-can-i-get-ip-addresses-of-group-hostnames/33073/4 "2020-06-02T10:37:06Z")

</div>

```auto
{% for item in groups['docker_servers']-%}
{{ hostvars[item]['ansible_default_ipv4']['address'] }}
{% endfor %}

```

```auto
- name: Configure pg_hba
  template:
    src: test.txt.j2
    dest: /tmp/groups.txt
  tags:
    - install_docker

[root@worker-server ~]# cat /tmp/groups.txt 
10.5.0.15

```

Here it works, but on another environment it gives error  
Version is 2.9.9

teisipäev, 2. juuni 2020 13:33.02 UTC+3 kirjutas Arko Köst:

---

<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:** [June 2, 2020, 10:49am UTC](https://forum.ansible.com/t/how-can-i-get-ip-addresses-of-group-hostnames/33073/5 "2020-06-02T10:49:27Z")

</div>

What facts are available depends on the configuration of the remote host. It  
might be tricky to find a working template in heterogeneous environment.

---

<div class="post-metadata">

**Author:** ![Arko\_Kost](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@Arko\_Kost](https://forum.ansible.com/u/Arko_Kost)\
**Post date:** [June 2, 2020, 10:57am UTC](https://forum.ansible.com/t/how-can-i-get-ip-addresses-of-group-hostnames/33073/6 "2020-06-02T10:57:57Z")

</div>

So what should i do?

teisipäev, 2. juuni 2020 13:49.49 UTC+3 kirjutas Vladimir Botka:

---

<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:** [June 2, 2020, 11:29am UTC](https://forum.ansible.com/t/how-can-i-get-ip-addresses-of-group-hostnames/33073/7 "2020-06-02T11:29:31Z")

</div>

Collect facts from all hosts and find items that fit the purpose.
