Rate this lesson

If you want to configure a lot of devices through Ansible and the configuration is the same but the parameters are different. then Ansible with jinja2 template comes into play, if you don’t want to write a different task for each device.. For example, you want to configure EIGRP in many routers, but the AS-number, router-ID and networks-numbers in EIGRP differ between the devices. Ansible with Jinga2 Template is the Topic of this video.

access ansible codes of this course in github

this is a video-based training therefore the output of running commands are not show in the text.

Jinja2 template is actually a configuration file, but with variables. the variables will be replaced by device-specific values in execution time of playbook. For example, if you want to configure EIGRP in devices, we will create a template with EIGRP configuration, but with variables in place of “AS-number”, “router-ID”, “network-number” and other parameters that may differ between devices. In our template we delete the previous configuration of EIGRP and replace it with a new configuration.

Jinja2 Templating in Ansible website

As you can see in the EIGRP template example, the template is more than just a variable substitution. Here “for loop” is used because the network number in EIGRP configuration is not just a simple value, but a list of networks. the number and the value of networks may differ in devices. The looping capability of Jinga2 Template helps to iterate over a list of networks variable that is device specific.

[email protected]:~/ansible-project1# cat eigrp_template.j2
no router eigrp {{ as }}
router eigrp {{ as }}
router-id {{ router_id }}
{% for network in networks %}
network {{ network }}
{% endfor %}

In the next section we will learn more about the capabilities of jinja2 Template.

The value of variables can be extracted from any of the valid places, discussed in the previous video. As we discussed in the video discussing variables, one of the best places to store the value of variables are in the “group_vars” and “host_vars” folders. host-specific values are stored in the “host_vars” folder and group-specific values in the “group_vars” folder.

Here the as-number is stored in the “group_vars” folder and in the csr file, which is the group name of the csr routers. we store as-number in “group_vars” folder since as-number is the same for all routers in csr group. but router-id and network-list are stored in the host_vars folder and in csr1 and csr2 files, as these variables are host-specific.

In order to call jinja2 template for many Cisco devices, you need to create a playbook with the Cisco “ios_config” module and the parameter “src” to call the jinja2 template.

[email protected]:~/ansible-project1# cat eigrp_with_jinja2.yaml
- hosts: csr
  gather_facts: false
  connection: network_cli

    - name: eigrp config
        src: "eigrp_template.j2"
        match: none

match parameter is set to none. This means that the playbook does not need to compare the current configuration of the router with our intended configuration since we delete the previous EIGRP configuration and replace it with a new one.

Now let’s run the playbook. But before we run playbook, let’s check the routers eigrp configuration to make sure the configurations are published via ansible.


Back to: Ansible for Network Engineers > Ansible with Jinja2

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment