23 Nov

Dynamic VIRL inventory for Ansible playbooks

Ansible is one of the powerful tools providing us an automation of recurring tasks. In the current world, it is impossible to manage infrastructure manually efficiently. Many people still do this but the world has already changed and we need to progress otherwise our business will be cost ineffective. You can provide static inventory – list of the devices where you want to execute the playbook. But in dynamic environments, such as Cisco VIRL simulations you don’t want to edit inventory file manually. That is why I use Python script that will generate Dynamic VIRL inventory for Ansible playbook for me.

You can use this Python script in two ways:

  • Provide it as a ‘-i’ argument to ansible-playbook execution
  • Use it from command shell to list inventory and running simulations

The ansible-playbook can use multiple sources for dynamic inventories. This is useful in environments that are frequently changing, or inventory is dynamic. Example of such situation is Dev or Test lab or VIRL simulation. In such case, the inventory can be provided as JSON structure generated by the script.

I will not focus much on the JSON structure because all details are described in Ansible documentation. The script itself is provided as -i argument to ansible-playbook execution, like in the example below

$ ansible-playbook -i inventory.py site.yml

Please notice, that we give the script as an argument, not the output of the script. The ansible-playbook executes the script by itself, so it has to be deployed in a predefined way.

The script has to accept at least –list as an argument. When you call the stript with this argument, the output is the JSON structure, and ansible-playbook use it instead of a static file. In simple scripts, you may omit the parameters and always print the JSON.

My VIRL dynamic inventory

The script is available in my GitLab – inventory.py. The description below refers to version 0.1 of the script. It is available in commit 49196b15. The script is not entirely made by me because I used the code found on cidrblock GitHub repository and extended it. You will find the link to the original version in the source code. You can use it to generate dynamic inventory as well as to list running simulation. If multiple simulations are running, you can produce inventory for selected simulation. However, this is only for command line execution, and as far as I know, it cannot be used this way for ansible playbooks.

Implemented parameters are:

  • –list – if only one simulation is running it produces JSON structure for dynamic inventory. Otherwise, it returns empty JSON or print error with list of active simulations
  • –host – provide hostname or IP address of VIRL host (usually the one you put in VM Maestro configuration). If not provided then script use my hardcoded IP
  • –listsim – returns list of running simulations
  • –sim – use provided simulation name for inventory output

Most of the parameters like VIRL host IP address or default VIRL API login and password are hardcoded in the source code, so you need to modify them before you run it.

Detected in the simulation devices are put into groups depending on device type so you can use them in the playbooks. Groups names start with “os_” appended by device type. Available groups are:

  • os_asa
  • os_ios
  • os_iosL2
  • os_ios-xr
  • os_ios-xr9000
  • os_CSR1000v

Additionally, there is a group all containing all detected devices. The node name is fetched from the simulation inventory via REST API. Note, that hosts key values are appended to my local domain I use for simulations, so I do not have to use IP addresses – this is just my way of running labs. I use standard node names, and on topology configuration, I assign a static management IP address that refers to the record in DNS zone configuration with the same name.

Script ignores the mgmt-lxc node.

2 thoughts on “Dynamic VIRL inventory for Ansible playbooks

  1. what is required to access the virl nodes using virlutils? I'm trying to run playbooks but cannot access the nodes.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.