How can i get ip addresses of group hostnames

18 views
Skip to first unread message

Arko Köst

unread,
Jun 2, 2020, 5:45:41 AM6/2/20
to Ansible Project

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

- 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'

Vladimir Botka

unread,
Jun 2, 2020, 6:28:10 AM6/2/20
to Arko Köst, ansible...@googlegroups.com
On Tue, 2 Jun 2020 02:45:41 -0700 (PDT)
Arko Köst <arko...@gmail.com> wrote:

> ... 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"

shell> cat pg_hba.conf.j2
{% for host in groups['opmon'] %}
host {{ host }} opmonitor {{ hostvars[host]['ansible_all_ipv4_addresses'] }}
{% endfor %}

and playbook

shell> cat pb.yml
- hosts: opmon
gather_facts: true
tasks:
- template:
src: pg_hba.conf.j2
dest: /tmp/pg_hba.conf
delegate_to: localhost
run_once: true

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

shell> ansible remote_host -m setup

HTH,

-vlado

--
Vladimir Botka

Arko Köst

unread,
Jun 2, 2020, 6:33:02 AM6/2/20
to Ansible Project
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'"}

Arko Köst

unread,
Jun 2, 2020, 6:37:06 AM6/2/20
to Ansible Project
{% for item in groups['docker_servers']-%}
{{ hostvars[item]['ansible_default_ipv4']['address'] }}
{% endfor %}

- 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

Vladimir Botka

unread,
Jun 2, 2020, 6:49:49 AM6/2/20
to Arko Köst, ansible...@googlegroups.com
On Tue, 2 Jun 2020 03:37:06 -0700 (PDT)
Arko Köst <arko...@gmail.com> wrote:

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

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

--
Vladimir Botka

Arko Köst

unread,
Jun 2, 2020, 6:57:57 AM6/2/20
to Ansible Project
So what should i do?

Vladimir Botka

unread,
Jun 2, 2020, 7:29:50 AM6/2/20
to Arko Köst, ansible...@googlegroups.com
On Tue, 2 Jun 2020 03:57:57 -0700 (PDT)
Arko Köst <arko...@gmail.com> wrote:

> So what should i do?

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

--
Vladimir Botka
Reply all
Reply to author
Forward
0 new messages