Network Automation with Ansible – Part 3 – Generating Device Configuration with Jinja2 Templates
In this example I generated configuration for two switches, where I configured few VLANs and their interfaces and some DHCP Pools.
Jinja2 Template
I am using Jinja2 templating module to generate the configuration. I am using a tree structure of directory and files to pass the configuration commands and variable required to generate the configuration. I create a role named switch which I called in my playbook.
The directories are structured as
![](https://i1.wp.com/mayanknauni.com/wp-content/uploads/2019/07/tree-structure.png?fit=604%2C383&ssl=1)
Playbook
![](https://i2.wp.com/mayanknauni.com/wp-content/uploads/2019/07/Playbook.png?fit=604%2C265&ssl=1)
Task/main.yml
![](https://i1.wp.com/mayanknauni.com/wp-content/uploads/2019/07/Task.png?fit=604%2C122&ssl=1)
Templates/switch. j2
![](https://i1.wp.com/mayanknauni.com/wp-content/uploads/2019/07/Template.png?fit=604%2C467&ssl=1)
Vars/main.yml
![](https://i0.wp.com/mayanknauni.com/wp-content/uploads/2019/07/Variables.png?fit=604%2C232&ssl=1)
Sample Configuration Generated by Playbook, Template and Variables
I generated configuration for two switches, as I passed the variables for two switches.
Switch 1
![](https://i0.wp.com/mayanknauni.com/wp-content/uploads/2019/07/Configuration-Switch1.png?resize=797%2C932)
Switch 2
![](https://i0.wp.com/mayanknauni.com/wp-content/uploads/2019/07/Configuration-Switch2.png?resize=752%2C951)
Recent Comments