Using Vagrant to Help Learn Ansible8 June 2015 · Filed in Explanation
I’ve been spending some time with Ansible recently, and I have to say that it’s really growing on me. While Ansible doesn’t have a steep learning curve, there is still a learning curve—albeit a smaller/less steep curve—so I wanted to share here a “trick” that I found for using Vagrant to help with learning Ansible. (I say “trick” here because it isn’t that this is complicated or undocumented, but rather that it may not be immediately obvious how to combine these two.)
Note that this is not to be confused with using Ansible from within Vagrant as a provisioner; that’s something different (see the Vagrant docs for more information on that use case). What I’m talking about is having a setup where you can easily explore how Ansible works and iterate through your playbooks using a Vagrant-managed VM.
Here are the key components:
- You’ll need a Vagrant environment (you know, a working
Vagrantfileand any associated support files).
- You’ll need Ansible installed on the system where you’ll be running Vagrant and the appropriate back-end virtualization platform (I tested this with VMware Fusion, but there’s nothing VMware-specific here).
- In the same directory as the
Vagrantfile, you’ll need an Ansible configuration file and an Ansible inventory file (more on both of these in just a moment).
With the right setup (and I’ll go into details on the setup shortly), you’ll be able to do this:
vagrant upto turn up the VMs in the
- Then run Ansible commands (ad-hoc commands or playbooks) against the VMs in the Vagrant environment.
vagrant destroyto kill the VMs and wipe the environment clean.
vagrant upagain in order to start again with a fresh configuration for additional playbook testing or other ad-hoc commands.
Maybe this doesn’t seem like a big deal to you, but having this ability to easily iterate over an Ansible playbook, or to test an Ansible playbook against various Linux distributions, has come in extraordinarily handy as I’ve been learning Ansible.
The “trick” here lies in the Ansible configuration file and the Ansible inventory. Ansible, by default, will look in the current directory for a file named
ansible.cfg for its configuration. By placing an Ansible configuration file with this name in the same directory as the Vagrantfile, we can configure Ansible specific for that particular Vagrant environment.
Here’s a very generic Ansible configuration file I use:
The documentation on the Ansible configuration file can explain these in greater detail, but here’s a quick breakdown of each line:
- Look in the current directory for the inventory in a file named
- Use the default Vagrant insecure SSH key, which (pretty much) every Vagrant box out there has pre-installed.
- Tell Ansible to use the “vagrant” account, which—like the SSH key—every Vagrant box out there has pre-defined.
- Don’t bother with checking host SSH keys.
Then, in the Ansible inventory file (named
hosts in the same directory as the Ansible configuration file and the
Vagrantfile), you simply place the IP addresses of each VM in the Vagrant environment. If you’re assigning IP addresses in the
Vagrantfile (recommended), then it’s a cinch to put this in the Ansible inventory as well. (I’m working on a way to automate this via Ruby in the
With this configuration, Ansible will use the inventory file in the same directory as the
Vagrantfile, which is where you’d need to be to run
vagrant up anyway. This will tell Ansible to work on the VMs defined in the Vagrant environment, using the default “vagrant” user and the default (insecure) SSH key. No special inventory variables, no command-line variables, nothing special needed…just run your Ansible commands or Ansible playbooks. Easy!
The complete workflow looks something like this:
- If you are statically assining IP addresses in the
Vagrantfile, copy those same addresses to the file named
hostsin the same directory.
- From that directory, run
vagrant upto create the VMs defined in the
Once the VMs are provisioned, run Ansible ad-hoc commands (or run playbooks using
ansible-playbook). For example, I wanted to verify some of the facts that are gathered by Ansible across various Linux distributions. I can show that information with this command:
ansible -m setup all
No need to specify the username (found in the configuration file named
ansible.cfgin the same directory), the authentication method (it will use the private SSH key from the configuration file), or the inventory (also supplied by the configuration file). Just run Ansible and be done.
- When you’re done with the environment, just run
vagrant destroy. If you want to start with a clean, fresh environment, then
vagrant upagain and you’re off to the races.
As I said earlier, I don’t think there’s anything terribly unique here, but it is a pretty useful configuration (for me, at least). Hopefully others will find it useful as well.Tags: Ansible · CLI · Linux · Vagrant Previous Post: Some Cumulus Linux Networking Concepts Next Post: Automatic Ansible Inventory with Vagrant