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...
> On Apr 16, 2021, at 7:54 AM, Anurag Bhatia <
m...@anuragbhatia.com> wrote:
>
> 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
>
---
R:
- name: R1
index: 1
asn: 65101
ipv4: 172.20.0.1
ipv6: 2001:db8:d::1
- name: R2
index: 2
asn: 65102
ipv4: 172.20.0.2
ipv6: 2001:db8:d::2
> 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
Play would work out to be...
- name: Set IPv4 and IPv6 address on all 30 routers
some_module:
- address R.{{ item.ipv4 )}.IPv4
- address R.{{ item.ipv6 )}.IPv6
loop: "{{ R }}"
Pretty sure I translated that right. :-)
- tim