1
votes

Are there any possible ways to check if registered variables (hostnames) with a loop are in inventory and print their groups?

I am creating a role to install heartbeat with ansible role. But it will be installed only on one jumphost which will ping the other.

Inside this role i have created separated files and put config for each host in them. But i need to add some fields to to find out which group they belong to (we need it in Kibana).

I used Jinja templates but it paste the group that owns the host I play the role.

{% for group in group_names %}
    group: {{ group }}
{% endfor %}

so I started to search if there is any way in ansible itself to do it. I

- name: list of hosts
  debug:
    msg: "{{ item }}"
  with_items:
    - "{{ groups['all'] }}"

it returned hosts and i would like compare this list with list in our inventory and get their group names to paste them to the icmp.yml configs of heartbeat

1

1 Answers

0
votes

Q; Are there any possible ways to check if registered variables (hostnames) with a loop are in inventory and print their groups?

A: Yes. It's possible to use Special Variables

ansible_play_hosts_all: List of all the hosts that were targeted by the play

group_names: List of groups the current host is part of

- debug:
    msg: "{{ hostvars[item].group_names }}"
  loop: "{{ hostnames }}"
  when: item in ansible_play_hosts_all