Ansible Playbook for Cisco BGP Routing Topology

This is my Ansible Playbook for a simple Cisco BGP routing topology and using a CICD pipeline for integration testing. The virtual network environment is created on-demand by using Vagrant, see my post about Cisco IOSv and XE network simulation using Vagrant.

Network overview:

Here’s my Github repository where you can find the complete Ansible Playbook:

You can find all the variables for the interface and routing configuration under host_vars. Below is an example for router rtr-1:


hostname: rtr-1
domain_name: lab.local


    alias: connection rtr-2

    alias: connection rtr-3

  asn: 65001
    - {address:, remote_as: 65000}
    - {address:, remote_as: 65000}
    - {network:, mask:}
    - {network:, mask:}
    - {network:, mask:}
  maxpath: 2


  • Hostname: The task in main.yml uses the Ansible module ios_system and configures hostname, domain name and disables dns lookups.
  • Interfaces: This role uses the Ansible module ios_config to deploy the template interfaces.j2 to configure the interfaces. In the main.yml is a second task to enable the interfaces when the previous template applied the configuration.
  • Routing: Very similar to the interfaces role and uses also the ios_config module to deploy the template routing.j2 for the BGP routing configuration.

Main Ansible Playbook site.yml:


- hosts: all

  connection: local
  user: vagrant
  gather_facts: 'no'

    - hostname
    - interfaces
    - routing

When a change triggers the gitlab-ci pipeline it spins up the Vagrant instances and executes the main Ansible Playbook:

After the main site.yml ran, a second Playbook is executed for basic connectivity testing cisco_check_icmp.yml. This uses the Ansible module ios_ping and can be useful in my case to validate if the configuration was correctly applied:

If everything goes well, like in this example, the job is successful:

I will continue to improve the Playbook and the CICD pipeline so come back later to check it out.

Leave a comment

12 Replies to “Ansible Playbook for Cisco BGP Routing Topology”

  1. Bernd, great article! Very impressed with the end to end content on Ansible and the layout of your website in general. I am starting my own blog soon on automation as I find very few end to end complete walk throughs of Ansible deployments. Thanks for this and keep up the good work,

    Ken Murphy, Senior Network Engineer, Dell EMC

    1. Thank you, glad you like my blog.
      Network automation is a very interesting topic and missing more information on the internet. You should for sure start your own blog, share your link with me when you’re ready.


      1. Hi Bernd, check out my new blog at, my first post is an MPLS lab deployment, inspired by your own Cisco Lab Provisioning with Ansible. Like your own blog I hope to get the interest going in Ansible so that other Network Engineers can benefit from this great automation tool. Happy new year!

        1. Hi Ken,
          Thanks for sharing, I will have a look and try it out 🙂
          Nice to hear that my blog inspired you to start your own. I set up a blogroll and link to your page.
          Happy New Year!

  2. This is great. So far I’ve just been playing around with ansible doing backups and show commands. This is definitely a step up from that.

    1. You’re welcome, have a look also at my other Cisco related Ansible automation articles, especially the CI/CD pipelining stuff for network automation is great.

  3. I’ve been working on and off on J2 templates and variables for Cisco routers for a few days now and this is the only tutorial that has actually worked for me.

    Thanks for the help!!!

  4. Hi, I’m having a issue with
    “msg”: “The task includes an option with an undefined variable. The error was: ‘hostname’ is undefined

    could you kindly help me to figure it out. thanks

Leave a Reply

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

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