The stack-install role

As you may have already guessed the bulk of this role, it is only tasks that call yum related modules and we have already mentioned that this is going to be changed.

The first part of the role we are going to look at is the content of roles/stack-install/tasks/main.yml. At the moment, this file contains the tasks that install our desired repositories and packages using the yum and yum_repository modules.

We need to update the file, but first, take the existing content and save it as a file called roles/stack-install/tasks/install-centos.yml. Once you have the copied the content, update roles/stack-install/tasks/main.yml so it has this content:

---

- name: include the operating system specific variables
include_vars: "{{ ansible_os_family }}.yml"

- name: install the stack on centos
import_tasks: install-centos.yml
when: ansible_os_family == 'RedHat'

- name: install the stack on ubuntu
import_tasks: install-ubuntu.yml
when: ansible_os_family == 'Debian'

As you can see, we are are using the ansible_os_family variable to include variables and also different tasks.

The task will include one of the following files, depending on which operating system the task is being executed on:

  • roles/stack-install/vars/RedHat.yml
  • roles/stack-install/vars/Debian.yml

It will then include one of the following two files, which contain the tasks for the operating system:

  • install-centos.yml
  • install-ubuntu.yml

We already know that install-centos.yml contains old content of our main.yml file; as the package name and repository URLs will also be changing, we should move the content of roles/stack-install/default/main.yml to roles/stack-install/vars/RedHat.yml, leaving roles/stack-install/default/main.yml empty.

Now that we have the CentOS portion of our role defined, we can look at the Ubuntu parts, starting with the content of roles/stack-install/vars/Debian.yml:

---

repo_packages:
- "deb [arch=amd64,i386] http://mirror.sax.uk.as61049.net/mariadb/repo/10.1/ubuntu {{ ansible_distribution_release }} main"
- "deb http://nginx.org/packages/mainline/ubuntu/ {{ ansible_distribution_release }} nginx"
- "deb-src http://nginx.org/packages/mainline/ubuntu/ {{ ansible_distribution_release }} nginx"

repo_keys:
- { key_server: "keyserver.ubuntu.com", key: "0xF1656F24C74CD1D8" }

repo_keys_url:
- "http://nginx.org/keys/nginx_signing.key"

system_packages:
- "software-properties-common"
- "python3-mysqldb"
- "acl"

stack_packages:
- "nginx"
- "mariadb-server"
- "php7.0"
- "php7.0-cli"
- "php7.0-fpm"
- "php7.0-gd"
- "php7.0-json"
- "php7.0-mbstring"
- "php7.0-mysqlnd"
- "php7.0-soap"
- "php7.0-xml"
- "php7.0-xmlrpc"

extra_packages:
- "vim"
- "git"
- "unzip"

As you can see, while we are keeping the system_packagesstack_packages, and extra_packages variables, we have different package names in there. We have a similar situation with repo_packages, where we have updated URLs, as the CentOS repositories will not work with Ubuntu. Finally, we have introduced two new variables, repo_keys, and repo_keys_urls; we will look at what these are used for shortly.

The final file we need to cover for our new role is roles/stack-install/tasks/install-ubuntu.yml. Like install-centos.yml, this contains the tasks to add the additional repositories we need and install the packages.

First of all, we need to install a few of the tools we need to continue with the rest of the tasks; these have been defined in the system_packages variable, so we simply have to add the following task:

- name: update cache and install the system packages
apt:
name: "{{ item }}"
update_cache: "yes"
with_items: "{{ system_packages }}"

Now that we have basic prerequisites installed, we can add the keys for the repositories we will be adding:

- name: add the apt keys from a key server
apt_key:
keyserver: "{{ item.key_server }}"
id: "{{ item.key }}"
with_items: "{{ repo_keys }}"

- name: add the apt keys from a URL
apt_key:
url: "{{ item }}"
state: present
with_items: "{{ repo_keys_url }}"

The first task adds keys from the official Ubuntu key store, and the second task downloads the keys from a URL. In our case, we are adding one key for the official MariaDB repository and one for the NGINX mainline repository; without these keys, we would not be able to add the repositories without generating an error about them not being trusted.

The task for adding repositories looks like the following; it cycles through the repository URLs in the repo_packages variable:

- name: install the repo packages
apt_repository:
repo: "{{ item }}"
state: "present"
update_cache: "yes"
with_items: "{{ repo_packages }}"

The final part of the playbook installs the remaining packages:

- name: install the stack packages
apt:
name: "{{ item }}"
state: "installed"
with_items: "{{ stack_packages + extra_packages }}"

Now we have updated our stack-install role, we need to do the same for the stack-config one.