Combining playbooks – Infrastructure as Code (IaC) with Terraform

As we’ve written multiple playbooks, we need to execute them in order. We cannot configure the services before installing packages and services, and there is no point in running an apt update after installing the packages. Therefore, we can create a playbook of playbooks.

To do so, we’ve created a YAML file, playbook.yaml, that has the following content:
  • import_playbook: apt-update.yaml
  • import_playbook: install-webserver.yaml
  • import_playbook: install-dbserver.yaml
  • import_playbook: setup-webservers.yaml
  • import_playbook: setup-dbservers.yaml

This YAML file contains a list of plays, and every play contains animport_playbook statement.
The plays are executed in order as specified in the file. Now, let’s go ahead and execute the playbook.

Executing playbooks

Executing the playbook is simple. We will use the ansible-playbook command followed by the playbook YAML file. As we’ve combined playbooks in a playbook.yaml file, the following command will run the playbook:

$ ansible-playbook playbook.yaml
PLAY [webservers:dbservers]
TASK [Gathering Facts]
ok: [web]
ok: [db]
TASK [Update apt packages]
****
ok: [web]
ok: [db]
PLAY [webservers] **
TASK [Gathering Facts]
ok: [web]
TASK [Install packages]

changed: [web]
TASK [Start and Enable Apache service]

ok: [web]
PLAY [dbservers] *
TASK [Gathering Facts] * ok: [db] TASK [Install packages]
changed: [db]
TASK [Start and enable MySQL service] **
ok: [db]
PLAY [webservers]
TASK [Gathering Facts] * ok: [web] TASK [Delete index.html file]
changed: [web]
TASK [Upload application file] *
changed: [web]
RUNNING HANDLER [Restart Apache]
changed: [web]
PLAY [dbservers]
**
TASK [Gathering Facts] * ok: [db] TASK [Set the root password] *
changed: [db]
TASK [Update the cnf file] *
changed: [db]
TASK [Create a test user]
changed: [db]
TASK [Remove all anonymous user accounts]
ok: [db]
TASK [Remove the MySQL test database] ***
ok: [db]
TASK [Change bind address] ****
changed: [db]
RUNNING HANDLER [Restart MySQL]
changed: [db]
PLAY RECAP *
db: ok=13 changed=6 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
web: ok=9 changed=4 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0

As we can see, the configuration is applied on both webservers and dbservers, so let’s run a curl command to the web server to see what we get:
$ curl web
PHP to MQSQL Database Connected successfully

As we can see, the database is connected successfully! That proves that the setup was successful.

There are several reasons why the way we approached the problem was not the best. First, there are several sections within the playbook where we’ve hardcoded values. While we have used variables in a few playbooks, we’ve also assigned values to variables within them. That does not make the playbooks a candidate for reuse. The best way to design software is to keep reusability in mind. Therefore, there are many ways in which we can redesign the playbooks to foster reusability.

Leave a Reply

Your email address will not be published. Required fields are marked *