How to create an Ansible project abstracted from the physical devices

Automation projects for networks devices are often very specific to a particular set of physical devices because of the physical topologies of these devices and how they are connected to each other. There are multiple situations where it’s important and very useful to be able to reuse the same automation projects across multiple “network” or group of devices, to name a few:

  • Production network with multiple sites
  • Configuration validation between production and pre-production networks
  • Dynamic/multiple lab environments

Even if you are able to share configuration templates across different projects, there are many information that are difficult to share or reuse. When you want to reuse one project between multiple topology you have to change many information: devices name, interfaces name, IPs etc …

With Ansible, this challenge is solved using dynamic inventory to dynamically load inventory information and device specific variables. Dynamic inventory is working great for deployment in production but require a back-end system that will store all information. For smaller deployment and for lab most people usually keep their inventory and variables in files stored on the local file system. (TODO, Add link to dynamic inventory) Using static inventory and variables defined in local yaml files, it’s possible to make a project easy to reuse across multiple physical topologies.

By using the 3 following steps you’ll be able to create to abstract your project from your physical topology:

  • Use alias for device in the inventory file
  • Centralize all physical information related to a given physical topology in a central topology file
  • Define remaining variables specific to a given topology in the inventory file

1/ Use alias for device in the inventory file

The inventory file is one of the main components of Ansible, it contains a lot of information including devices names and groups. The name you use to define your device is very important because it is used in many other places inside ansible. For example, variables are classified per devices using the name defined in the inventory, also available with the variable “inventory_hostname”.

_images/inventory_variable.png

It’s seam natural to use the real name of your devices in the inventory file but by doing so you’ll make it difficult to reuse your project across multiple devices. It’s possible to keep your project portable by using an alias in the inventory and create a new variable that contain the real name or IP of the device

_images/inventory_variable_alias.png

3/ Define remaining variables specific to a given topology in the inventory file

Remaining information that are device specific like management IP, loopback address etc .. can be defined in the inventory file directory. It’s possible to define any type of variable in the inventory file, either per device or per group

_images/inventory.png