Creating Ansible Inventory from Vagrant SSH Configuration
From Building Network Automation Solutions
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.
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=127.0.0.1 ansible_port=2222 spine-2 ansible_host=127.0.0.1 ansible_port=2200 leaf-1 ansible_host=127.0.0.1 ansible_port=2201 leaf-2 ansible_host=127.0.0.1 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.
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 127.0.0.1. Use –a 10.0.2.2 if you want to access Vagrant VMs from within another VM in VirtualBox environment.
- --vm: Ansible is running within a VM in VirtualBox. Use 10.0.2.2 instead of 127.0.0.1
- -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)