Understanding the learning environment
We assume that most of our learners would like to set up the environment locally, and thus recommend using the open source and freely available software VirtualBox and Vagrant, which have support for most of the desktop operating systems, including Windows, OSX, and Linux.
The ideal setup includes five virtual machines, the purpose of which is explained as follows. You can also consolidate some of the services, for example, the load balancer and web server can be the same host:
- Controller: This is the only host that needs to have Ansible installed, and works as a controller. This is used to launch the
ansible-playbook
commands from the controller. - Database (Ubuntu): This host is configured with Ansible to run the MySQL database service and runs the Ubuntu distribution of Linux.
- Database (CentOS): This host is configured with Ansible to run the MySQL database service, however, it runs the CentOS distribution of Linux. This is added to test multiplatform support while writing the MySQL role for Ansible.
- Web Server: This host is configured with Ansible to run the Apache web server application.
- Load balancer: This host gets configured with the haproxy application, which is an open source HTTP proxy service. This host acts as a load balancer, which accepts HTTP requests and spreads the load across available web servers.
For most up-to-date instructions on prerequisites, software and hardware requirements, as well as setup instructions, refer to the following GitHub repository:
https://github.com/schoolofdevops/ansible-playbook-essentials.
A moderately configured desktop or a laptop system should be sufficient to set up the learning environment. The following are the recommended prerequisites in the context of of software and hardware:
For the purpose of setting up the learning environment, we recommend using the following software:
- VirtualBox: Oracle's virtualbox is a desktop virtualization software, which is freely available. It works on a variety of operating systems, including Windows, OS X, Linux, FreeBSD, Solaris, and so on. It provides a layer of hypervisor and allows one to create and run virtual machines on top of an existing base OS. The code provided along with this book has been tested on 4.3x versions of virtualbox. However, any version of virtualbox, which is compatible with the vagrant version can be used.
- Vagrant: This is a tool that allows one to easily create and share virtual environments on most hypervisors and cloud platforms, including but not limited to virtualbox. It can automate tasks such as importing an image, specifying resources, such as memory and CPUs assigned to VMs, and setting up network interfaces, hostnames, user credentials, and so on. Since it provides a text configuration in the form of a Vagrant file, virtual machines can be provisioned programmatically, making them easy to use it with other tools such as Jenkins to automate build and test pipelines.
- Git for Windows: Even though we do not intend to use Git, which is a version control software, we use this software to install the SSH utility on the Windows system. Vagrant needs an SSH binary available in the path. Windows is not packaged with the SSH utility, and Git for Windows is the easiest way to install it on Windows. Alternative options such as Cygwin exist.
The following table lists the version OS the software used to develop the code provided with the book, with download links:
Learners are advised to download, install, and refer to the respective documentation pages to get familiar with these tools before proceeding.
Creating virtual machines
Once you have installed the base software, you can use vagrant to bring up the virtual machines required. Vagrant uses a specification file by the name Vagrantfile
, a sample of which is as follows:
The preceding Vagrant file contains specifications to set up five virtual machines, as described in the beginning of this chapter, which are, control
, db
, dbel
, www
, and lb
.
Learners are advised to use following instructions to create and start the virtual machines required to set up the learning environment:
- Create a directory structure for the learning environment setup, for example,
learn/ansible
, anywhere on the system. - Copy the
Vagrantfile
file provided previously to the learn/ansible
directory. The tree should now look as follows:Note
The Vagrantfile
file contains specifications for the virtual machines described in the earlier section.
- Open a terminal and go to
learn/ansible
. - Bring up the control node and log in to it, as follows:
- From a separate terminal window, from the
learn/ansible
directory, bring up the remaining virtual machine, one at a time, as follows:
Installing Ansible on the controller
Once the virtual machines are created and brought up, Ansible needs to be installed on the controller. Since Ansible is agentless and manages nodes using SSH transport, no additional setup is needed on the nodes except for ensuring that the SSH service is running. To install Ansible on the controller, refer to the following steps. These instructions are specific to the Ubuntu distribution of Linux, as that's what we use on our controller. For generic installation instructions, please refer to the following page:
http://docs.ansible.com/intro_installation.html.
The steps are as follows:
- Log in to the controller using the following command:
- Update the repository cache using the following command:
- Install the prerequisite software and repositories:
- Update the repository cache after adding a new repository, such as follows:
- Install Ansible using the following command:
- Validate Ansible using the following command:
The sample code provided with this book is divided as per the chapter numbers. A directory named after the chapter number contains the snapshot of the state of the code at the end of the respective chapter. Learners are advised to independently create their own code and use the sample code as a reference. Moreover, if the readers skip one or more chapters, they can use the sample code from the previous chapter as a base.
For example, while using Chapter 6, Iterative Control Structures – Loops, you can use the sample code from Chapter 5, Controlling Execution Flow – Conditionals, as a base.
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.