Creating network configuration templates

As we are now familiar with the basics of Ansible, let us look at an example in which we generate configs ready to be deployed for some routers. To start with, we need to understand roles in Ansible. Roles are used to create a file structure for Ansible playbooks. Based upon roles, we can group similar data. Sharing the roles with others would mean we share the entire defined file structure for a common set of content. A typical role file structure would contain the main folder and the content folder, and under the content folder, we would have templates, vars, and tasks folders.

In our case, the hierarchy is as follows:

  • Main directory
    • -Roles
      • -Routers
        • -Templates
        • -Vars
        • -Tasks

Under each of the templates, vars, or tasks folders, if we call that specific role, a file named main.yml is searched automatically and any configs in that file are taken into consideration for that particular role. Using the details of hierarchy as previously mentioned , in our test machine (running Ubuntu) here is how our file structure looks in the example:

As we see, under the rtrconfig folder, we have defined the folders as per Ansible standards. Once we create the folder hierarchy, the next step is to configure/create the files under each of the sections, based upon our requirements.

To start with, as we will be using a router template to generate the configs, we create a template and put it in the roles/routers/templates folder.

Router config template is as follows (used as a generic router template to generate router configs):

no service pad
service tcp-keepalives-in
service tcp-keepalives-out
service password-encryption
username test password test
!
hostname {{item.hostname}}
logging server {{logging_server}}
!
logging buffered 32000
no logging console
!
ip domain-lookup enable
!
exit

As we can see in the template, {{item.hostname}} and {{logging_server}} are two values that we would replace while creating the actual config. As this is a Jinja template, we would save this template as somename.j2 (in our case, routers.j2). The next step is to define the variable values.

As we saw earlier, we need to ensure that the logging_server variable has already been defined a value. This will be in the roles/routers/vars folder:

---
logging_server: 10.10.10.10

We save this file as main.yml in the vars folder, which will be picked by default while executing the playbook for the variable value declarations. Once we have the definitions and template in place, the next step is to define the actual task that needs to be performed.

This will be done in the roles/routers/tasks folder and again saved as main.yml for auto discovery during the execution of that specific role.

Let us see the config for this:

---
- name: Generate configuration files
template: src=routers.j2 dest=/home/abhishek/{{item.hostname}}.txt
with_items:
- { hostname: myrouter1 }
- { hostname: myrouter2 }

In the config for the task, we call the template that we created (in this case, routers.j2), and provide a destination folder where the config files will be saved (in this case, /home/abhishek/{{item.hostname}}.txt).

A specific point to note here is that {{item.hostname}} will resolve to each of the hostnames that we have provided using the with_items loop. As a result, the filename that will be generated will be each item defined in the with_items loop (in our case, myrouter1.txt and myrouter2.txt).

As mentioned previously, the with_items will loop with each value, with the hostname variable value being changed with each iteration. Once we have the template, vars, and task created, we will call the role in our main playbook and get it executed.

The main playbook config is as follows:

---
- name: Generate router configuration files
hosts: localhost

roles:
- routers

Here we just call the hosts (which is localhost in our case since we want to get this executed locally), and call the role that needs to be executed in the playbook (in our case, routers). We save it as any name with a .yml extension (in our case, makeconfig.yml).

The final validation to ensure all .yml files are created in respective folders are as follows:

  1.  To recap, here is the detailed file structure, as we now see the files under the rtrconfig folder:
  1. To generate the config for routers , we execute the makeconfig.yml playbook:
  1. Once executed successfully, we should have two files (myrouter1.txt and myrouter2.txt) in the /home/abhishek folder with the generated config:
  1. Here is the content from one of the generated files:
  1. As we can see, now we have a generated config, using the template, with the values replaced for the hostname and logging _server section.

The config is now generated and ready to be pushed on those particular routers (which were part of main.yml under roles/routers/tasks), and in a similar way, we can generate configs with various roles and multiple devices in each role, such as switches, routers, load balancers, and so on with each role containing specific information, such as variables, templates, and tasks relevant to that role.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.197.198