Installing a Vagrant plugin

Let's install a plugin from the RubyGems website. I've searched for vagrant and found one called vagrant-hostsupdater, which is currently version 1.1.1.160 and has just over 500,000 downloads. Here is a screenshot of the plugin in the RubyGems search results:

This plugin will attempt to edit your /etc/hosts file by adding and removing hosts when Vagrant machines are created and destroyed, respectively. This means that you can access the Vagrant machine by a domain name such as machine.dev instead of 192.168.10.10.

We can install this plugin by running the following command:

vagrant plugin install vagrant-hostsupdater

You should see an output similar to this:

We can verify that this has been installed by running the vagrant plugin list command as shown here:

Let's now use and test our Vagrant plugin. This specific plugin is configured in the Vagrantfile, so let's create a basic one to get started:

  1. Run the vagrant init -m command.
  2. Edit your Vagrantfile to include the following code:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network :private_network, ip: "192.168.100.23"
config.vm.hostname = "vagrant.dev"
config.vm.provision "shell", inline: <<-SHELL
sudo apt-get update
sudo apt-get install -y nginx
SHELL
end

We create a basic Vagrant machine to test our plugin. The main lines we are concerned with are the config.vm.network and config.vm.hostname lines as they are required by our plugin.

We have created a Ubuntu machine that uses a private static IP address, the hostname of vagrant.dev, and a basic shell provisioner to update the system and then install the nginx web server. This will allow us to quickly and easily see that everything has worked as nginx has a default page available on port 80 once it's been installed and is running.

  1. Run the vagrant up --provision command to get the box up and running. 

You should now see a message from the [vagrant-hostsupdater] plugin, which will attempt to enter the machine's IP address and host name into the /etc/hosts file. The hosts file is an important system file and requires root permission to edit. You will be asked for the root password for your host machine:

  1. To test that the plugin works, we can check the /etc/hosts file before we start up the vagrant machine. Here is a basic example. If you have edited yours before, you may see more entries:

  1. Once you have entered your root password and the plugin successfully writes to the /etc/hosts file, you should see this message as part of the vagrant up process:

  1. Once the machine is up and running, check out the /etc/hosts file again to see whether a new entry has been added. All new entries are added at the bottom of the file. In the following screenshot,  we can see that our entry is there, the IP address is 192.168.100.23 and the hostname is vagrant.dev. The plugin has also added in a comment using the # character:

  1. Great! Let's now test the hostname and see what we get. While we are in the terminal, we can run the curl vagrant.dev command, which will attempt to load that URL and return the contents. We can see that the default nginx page has been returned:

  1. Ping the hostname to see whether there is a live connection, packet loss, and what sort of connection times we get. As the machine is local, the speed will be very quick (less than 1 ms) and we will see the IP address returned, which in this case is 192.168.100.23:

  1. vagrant halt the machine. You'll see in the terminal that the plugin will now kick into action and remove that entry from the /etc/hosts file. You'll need to enter the root password again:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.252.8