Creating Ansible Inventory from Vagrant SSH Configuration

From Building Network Automation Solutions

Building Network Automation Solutions
6 week advanced interactive online course Button-click-here.png
Course starting in
September 2018

At a Glance

  • High-intensity interactive online course;
  • Jump-start your network automation career;
  • Hands-on experience working on a solution to your own problem;
  • 9 module course spread across ~3 months;
  • Live discussion and guest speaker sessions;
  • Design and coding assignments and group work;
  • Final course completion certificate.
Vagrant-based labs running on top of VirtualBox use port mapping from host TCP ports in range 2000 – 2500 to SSH ports on managed virtual machines.

It’s relatively easy to create Ansible inventory that works in that environment: all you have to do is to specify ansible_host and ansible_port for every managed device, for example:

spine-1    ansible_host=         ansible_port=2222
spine-2    ansible_host=         ansible_port=2200  
leaf-1     ansible_host=         ansible_port=2201  
leaf-2     ansible_host=         ansible_port=2202

Extracting the information from the printout produced by vagrant ssh-config and copying the values into an Ansible inventory file is a perfect job for an automation script. You’ll find mine in the topologies directory of my Network Automation Workshop GitHub repository.

Using Vagrant2Inventory

The Vagrant2Inventory script executes vagrant ssh-config, parses the printout and prints Ansible inventory file generated from that information to STDOUT. These command line options are recognized by the script:

  • -a (or --address): specify IP address to use instead of Use –a if you want to access Vagrant VMs from within another VM in VirtualBox environment.
  • --vm: Ansible is running within a VM in VirtualBox. Use instead of
  • -s (or --skip): skip hosts matching a regular expression. I use -s nms to remove my Ansible VM from the inventory file.
  • -u (or --username): use alternate username (default: vagrant)
  • -p (or --password): use alternate password (default: vagrant)