Launching a virtual machine

In order to launch a virtual machine to run our first set of Ansible commands against, we are going to use Vagrant.

Please note that these instructions may not work if you are running WSL.

Vagrant is a virtual machine manager developed by HashiCorp. It can manage both local and remote virtual machines, and supports hypervisors, such as VirtualBox, VMware, and Hyper-V.

To install Vagrant on macOS, we can use Homebrew along with cask. To install cask, run the following command:

$ brew install cask
VirtualBox is an open source hypervisor for x86-based computers. It is currently being developed by Oracle and supports both software- and hardware-based virtualization.

By default, Vagrant uses VirtualBox. Once cask is installed, you can use VirtualBox and Vagrant by running the following command:

$ brew cask install virtualbox vagrant

To install on Ubuntu, you can run the following command:

$ sudo apt-get install virtualbox vagrant

Next up, if you don't already have one, we need to generate a private and public key for your user. To do this, run the following command, but if you already have a key, you can skip this part:

$ ssh-keygen -t rsa -C "[email protected]"

This will create a key and store it in the .ssh folder in your user directory. We will use this key to inject into our Vagrant managed CentOS 7 virtual machine. To launch the virtual machine or box, as Vagrant calls it, we need a Vagrantfile. This is the configuration that Vagrant uses to create and boot the box.

The Vagrantfile we are going to be using is shown in the following code. You can also find a copy in the Chapter02 folder in the code examples that accompany this book and also in the GitHub repository, which can be found at https://github.com/PacktPublishing/Learn-Ansible/tree/master/Chapter02:

# -*- mode: ruby -*-
# vi: set ft=ruby :

API_VERSION = "2"
BOX_NAME = "centos/7"
BOX_IP = "192.168.50.4"
DOMAIN = "nip.io"
PRIVATE_KEY = "~/.ssh/id_rsa"
PUBLIC_KEY = '~/.ssh/id_rsa.pub'

Vagrant.configure(API_VERSION) do |config|
config.vm.box = BOX_NAME
config.vm.network "private_network", ip: BOX_IP
config.vm.host_name = BOX_IP + '.' + DOMAIN
config.ssh.insert_key = false
config.ssh.private_key_path = [PRIVATE_KEY,
"~/.vagrant.d/insecure_private_key"]
config.vm.provision "file", source: PUBLIC_KEY, destination:
"~/.ssh/authorized_keys"

config.vm.provider "virtualbox" do |v|
v.memory = "2024"
v.cpus = "2"
end

config.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = "2024"
v.vmx["numvcpus"] = "2"
end

end

As you can see from the preceding file, there are a few things going on. First of all, in the top section, we are defining a few variables for the following:

  • API_VERSION: This is the Vagrant API version to use. This should stay at 2.
  • BOX_NAME: This is the base image we want to use. In our case, this is the official CentOS 7 Vagrant box image, which can be found at https://app.vagrantup.com/centos/boxes/7
  • BOX_IP: This is the private IP address of the machine we are launching. Typically, you shouldn't need to hardcode the IP address, but in our case, we will need a resolvable hostname and also a fixed IP address for the examples in the next section of the chapter.
  • DOMAIN: This is the domain name that is used to configure the hostname on the machine. We are using the http://nip.io/ service. This provides free wildcard DNS entries. This means that our domain 192.168.50.4.nip.io will resolve to 192.168.50.4.
  • PRIVATE_KEY: This is the path to your private key. This will be used to SSH into the virtual machine once launched.
  • PUBLIC_KEY: This is the path to your public key. When the machine is being launched, this will be injected into the host, which means that we can access it using our private key.

The next section takes the preceding values and configures the Vagrant box. We then define settings that are just for the two providers that the Vagrantfile supports. As you can see, the file will launch a box using either VirtualBox or, if you have it installed, VMware Fusion.

For more information on the VMware provider plugin for Vagrant, visit https://www.vagrantup.com/vmware/. Please note that this part of Vagrant requires a license, which is chargeable, as well as requiring you to have VMware Fusion or Workstation installed on your host machine.

Now that we have our Vagrantfile, we just need to run the following command to launch the Vagrant box:

$ vagrant up

If you do not pass a provider along, it will default to using VirtualBox. If you have the VMware plugin, like me, you can run the following command:

$ vagrant up --provider=vmware_fusion

It will take a few minutes while the appropriate box file is downloaded and the virtual machine is configured:

As you can see from the Terminal output, the launch process is very verbose and you receive informative feedback at each stage.

Once the Vagrant box is launched, you can check connectivity to it by running the following commands. This will log you into the Vagrant box as the Vagrant user and check the details on the hostname and kernel:

$ vagrant ssh
$ hostname
$ uname -a
$ exit

Your Terminal should look like the following:

As you can see, because we have told Vagrant which private key to use when accessing the box, we have been let straight into the box and can run the commands without issue. However, we won't be using the vagrant ssh command in the next section, which is why we needed to inject our public key into the host. Instead, we will be SSHing directly into the machine from our host machine. To test this, you should be able to run the following command:

$ ssh [email protected]

You should be asked to establish the authenticity of the host by typing yes. Once you are logged in, you will be able to run the following commands:

$ hostname
$ uname -a
$ exit

Your Terminal should look like the following:

As you can see, we have resolved and connected to 192.168.50.4.nip.io using the Vagrant user and have authenticated using our private key. Before we move on to the next section and try running Ansible for the first time, we should discuss Vagrant provisioners.

No doubt you will have already looked at the Vagrant website, which can be found at http://vagrantup.com/, and may have spotted that Vagrant actually supports Ansible out of the box. If we were to use the Ansible provisioner, then Vagrant would dynamically create a host inventory and run our playbook against the box as part of the launch process. Before we look at this, I believe it is important for us to understand how the host inventory works, so we will look at the Ansible provisioner in the next chapter.

Before then, however, let's take a look at some basic playbooks and how we can interact with our Vagrant box using Ansible.

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

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