Looping over inventory while reading variables from dict

15 views
Skip to first unread message

Anurag Bhatia

unread,
Apr 16, 2021, 8:55:02 AM4/16/21
to ansible...@googlegroups.com
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.  

--
Anurag Bhatia

Tim Shubitz

unread,
Apr 16, 2021, 10:44:34 AM4/16/21
to ansible...@googlegroups.com
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



Anurag Bhatia

unread,
Apr 19, 2021, 7:42:04 PM4/19/21
to ansible...@googlegroups.com
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. 


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5A2D0B1B-80CE-47F6-8792-D49A02FF38AD%40gmail.com.


--
Anurag Bhatia
Reply all
Reply to author
Forward
0 new messages