How can i get ip addresses of group hostnames

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'

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

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

and playbook

  > 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

  > ansible remote_host -m setup

HTH,

  -vlado

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:

{% 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

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

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

So what should i do?

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

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