Reduce Vagrant code duplication by using functions

Aug 27, 2016 linux
This post is more than 18 months old. Since technology changes too rapidly, this content may be out of date (but that's not always the case). Please remember to verify any technical or programming information with the current release.

I use Vagrant for most of my server management. One thing I noticed is that my Vagrantfile can get pretty large - especially if I have multiple environments that share the same configuration.

Then it occurred to me - this is just a ruby file! So, I decided to use a function to limit down some of my configuration.

I have a lot of duplication around the sections of my file where I configure an ansible provisioner.

For example, see this configuration:

local.vm.provision "ansible" do |ansible|
  ansible.playbook = "ansible/playbook.yml"
  ansible.host_key_checking = true
  ansible.sudo = true
    
  ansible.host_key_checking = false
  ansible.extra_vars = {
    server_name: local.vm.hostname,
    server_env: "development"
  }
end

The first three lines in this particular configuration are used for every configuration in my file (in some instances, there are many more files). So, instead, I decided to create the following method at the top of the configuration block:

def shared_ansible_config(ansible)
  ansible.playbook = "ansible/playbook.yml"
  ansible.host_key_checking = true
  ansible.sudo = true
end

Then, I called this function while passing in the configuration object to it in my provision statement like so:

local.vm.provision "ansible" do |ansible|
  shared_ansible_config ansible
    
  ansible.host_key_checking = false
  ansible.extra_vars = {
    server_name: local.vm.hostname,
    server_env: "development"
  }
end

There - less code duplication! :)

Go to All Posts