Playbook structure

In the previous chapters, the playbooks we have been running have typically been as basic as possible. They have all been in a single file, which is accompanied by a host inventory file. In this chapter, as we are going to be greatly expanding the amount of work our playbook is doing, so we are going to be using the directory structure recommended by Ansible.

As you can see from the following layout, there are several folders and files:

Let's work on creating the structure and discuss each item as we create it. The first folder we need to create is our top-level folder. This is the folder that will contain our playbook folders and files:

$ mkdir lamp
$ cd lamp

The next folder we are going to create is one called group_vars. This will contain the variable files used in our playbook. For now, we are going to be creating a single variable file call common.yml:

$ mkdir group_vars
$
touch group_vars/common.yml

Next, we are going to be creating two files: our host inventory file, which we will name production, and also our master playbook, which is typically called site.yml:

$ touch production
$ touch site.yml

The final folder we are going to create manually is called roles. In here, we are going to use the ansible-galaxy command to create a role called common. To do this, we use the following commands:

$ mkdir roles
$ ansible-galaxy init roles/common

As you may have noticed from the initial structure at the start of this section, the common role has several files and folders itself; all of these are created for us when we run the ansible-galaxy init command. We will discuss what each of these does in the next section where we will be using the common role to configure our base Linux server.

The only other file that isn't part of the default Ansible structure is our Vagrantfile. This contains the following content:

# -*- 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

While we will be working through each of the files inpidually in this and the following sections, a complete copy of the playbook is available in the accompanying GitHub repository.