Brian so the basic idea is using ansible to build out switch templates leveraging variables that are specific to each switch device.
For the purpose of this test I’ve made the template very simple I need to populate the variable {{ from inventory_hostname }} and {{ip}} into the template below for each switch.
The {{ inventory hostname }} variable is retrieved from iterating over the hosts:routers in the play as you mention above
The {{ip}} variable is retrieved from host_vars/tor01.a01.yml and host_vars/tor01.a02.yml file
See example below for one of these switches. Again this is being kept very simple for demo purposes.
[vagrant@dev4 host_vars]$ more tor01.a01.yml
hostname: tor01.a01
ip : 10.1.1.1
[vagrant@dev4 host_vars]$
The basic template is below:
[vagrant@dev4 templates]$ more base.j2
!
hostname {{ inventory_hostname}}
!
!
interface GigabitEthernet0/0
ip access-group INTERNET in
ip address {{ ip }}
no ip redirects
no ip proxy-arp
ip nat outside
ip virtual-reassembly
duplex auto
speed auto
no cdp enable
!
[vagrant@dev4 templates]$
When I run the playbook it combines the variables and the templates to create a populated file for each switch that can be uploaded to the device.
[vagrant@dev4 RTR-TEMPLATE]$ ansible-playbook -i test site.yml
PLAY [Generate router configuration files] ************************************
TASK: [router | Generate configuration files] *********************************
changed: [tor01.a02]
changed: [tor01.a01]
PLAY RECAP ********************************************************************
tor01.a01 : ok=1 changed=1 unreachable=0 failed=0
tor01.a02 : ok=1 changed=1 unreachable=0 failed=0
This is fine as currently I only have two devices to template tor01.a01 and tor01.a02 but if this was several hundred devices I’d prefer to keep variables in single YAML file below and loop over this to extract the {{ip}} for each hostname?
Is this currently possible with Ansible?
I’ve tried to do this but can’t seem to access the variable {{ip}} within tor_switches.yml and not sure how I load variables from this file into Ansible for use in playbook?
[vagrant@dev4 RTR-TEMPLATE]$ cat host_vars/tor_switches.yml