2

I have this piece of the playbook.

- hosts: iperf_servers
  remote_user: root
  tasks:
  - name: print server iperf
    debug: msg="Starting Server Iperf with Data_IP > {{ hostvars[inventory_hostname].nodes[0].data_ip }}"
  - name: start server iperf
    shell: "iperf3 -B {{ hostvars[inventory_hostname].nodes[0].data_ip }} --server --one-off --daemon"
  - name: print friendly output
    debug: msg="Started Server IPERF On ip > {{ hostvars[inventory_hostname].nodes[0].data_ip }}"

- hosts: iperf_clients
  remote_user: root
  tasks: 
  - name: print iperf server data_ip's from client
    debug: var="{ hostvars['groups']['iperf_servers'][0].data_ip }}"

In the first play, with hosts 'iperf_servers' I'm accessing the var 'data_ip', which is stored inside host_vars, and this is how it looks:

---
name: server00
nodes:
-   instanceID: 0
    data_ip: 200.100.1.150

#** Note: under host_vars directory, I have 3 server files: **#

'ls  light-app/ansible/inventories/cluster_example/host_vars/'

server00.yml  server01.yml  server02.yml

As can be noticed, I'm accessing this var by:

{{ hostvars[inventory_hostname].nodes[0].data_ip }}

and it works just fine, I'm getting the value I'm expecting (the data_ip value) for each server file.

What I need in the second play, is to access the same var, but while I'm running it on other hosts groups (iperf_clients). The idea is to get the data_ip of the servers and set it on the clients. So basically I'm trying to use the data_ip of another host.

This way didn't work:

{{ hostvars['groups']['iperf_servers'][0].data_ip }}

I have tried so many ways and searched for so long. Is there a possible way to get these vars?

Yun
  • 3,056
  • 6
  • 9
  • 28
Leo_simon
  • 21
  • 2

1 Answers1

0

Did you mean to do the following?

{{ hostvars[groups['iperf_servers'][0]].nodes[0].data_ip }}

In ansible, groups is a special variable -- a dictionary where key is the name of a host group, and value is a list of the servers in that host group.

Doing hostvars['groups'] means it is looking for a server named "group" in your inventory which likely doesn't exist. So instead, groups['iperf_servers'] will access the dictionary and return a list of hostnames for all your iperf servers.

Edit:

To get IPs from all iperf_servers

{{ groups['iperf_servers'] | map('extract', hostvars, 'nodes') | map('first') | map(attribute='data_ip') | list }}

Check the docs for the map extract if you're interested.

Rickkwa
  • 2,197
  • 4
  • 23
  • 34
  • yes, exactly. Thanks Amazing. but this is prints just the first node's data_ip, what if I need it from all nodes in the iperf_servers group? – Leo_simon Sep 24 '21 at 10:57
  • @Leo_simon See my edit – Rickkwa Sep 24 '21 at 13:57
  • Thanks, but when i run this: debug: msg="{{ groups['iperf_servers'] | map('extract', hostvars, 'nodes') | map(attribute='data_ip') | list }}" I'm getting : TASK [print iperf server data_ip's from client] *********************************************************************************************************************************************************** ok: [server02] => { "msg": "[AnsibleUndefined, AnsibleUndefined]" } – Leo_simon Sep 24 '21 at 17:01
  • @Leo_simon Made another edit. I mistakenly assumed `nodes` was a dict instead of a list. – Rickkwa Sep 24 '21 at 17:10
  • @Leo_simon was this able to solve your issue? If so, I'd appreciate it if you can mark this as an accepted answer. – Rickkwa Oct 08 '21 at 02:15