Looping over inventory while reading variables from dict

Hello,

I am having a hard time in finding syntax needed for playbooks to loop over inventory with 30 routers (R1 to R30) where some values (IP addresses) are stored in an external .yml file.

Example of file storing IP addresses:

R:
R1:
index: 1
asn: 65101
ipv4: 172.20.0.1
ipv6: 2001:db8:d::1

R2:
index: 2
asn: 65102
ipv4: 172.20.0.2
ipv6: 2001:db8:d::2

R3:
index: 3
asn: 65103
ipv4: 172.20.0.3
ipv6: 2001:db8:d::3

What I want to achieve here is have a single task which loops inventory (R1…R30) and sets IP addresses. Due to some reason I cannot store these IPs, ASNs etc in inventory itself.
Pseudo code of what I want:

  • name: Set IPv4 and IPv6 address on all 30 routers
    some_module:
  • address R.{{ inventory_hostname )}.IPv4
  • address R.{{ inventory_hostname )}.IPv6

Any advice on how to achieve this would be very helpful!

Thanks.

Pretty sure I'm doing what you're looking to accomplish, Anurag.

How I have my inventory file configured would be like using your example below...

Hello Tim

That is really useful information.

I think I can make things work that way but essentially you are keeping all variable values within the host inventory, is that understanding correct?
I have a requirement data is scattered across different files.
Can you please suggest a way where I could loop over the inventory but read specific data from specific files? So I may be reading from souce1.yml in test1 and source2.yml in task 2 but running each task over all 30 routers as defined in the group.

Thanks.