Checking playbook syntax – Infrastructure as Code (IaC) with Terraform

It is a best practice to check playbook syntax before applying it to your inventory. To check your playbook’s syntax, run the following command:
$ ansible-playbook ping.yaml –syntax-check
playbook: ping.yaml

The syntax is correct, as we get a response with the playbook name. Now, let’s go ahead and apply the playbook.

Applying the first playbook

To apply the playbook, run the following command:
$ ansible-playbook ping.yaml
PLAY [all] *
TASK [Gathering Facts] * ok: [db] ok: [web] TASK [Ping all servers]
ok: [db]
ok: [web]
PLAY RECAP *
db : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

There are three elements of play execution:

• Gathering facts: Ansible checks for all hosts that are part of the role, logs in to each instance, and gathers information from each host it uses while executing the tasks from the plays.
• Run tasks: Then, it runs the tasks of each play, as defined in the playbook.
• Play recap: Ansible then provides a recap of the tasks it executed and the hosts it ran them on. This includes a list of all successful and failed responses.

As we’ve investigated an elementary example of playbooks, we must understand how to use Ansible playbooks effectively. In the next section, let’s look at Ansible playbooks in action with a better example.

Ansible playbooks in action

Let’s set up an Apache server for a custom website that connects with a MySQL backend—in short, a Linux, Apache, MySQL, and PHP (LAMP) stack using Ansible.

The following directory contains all resources for the exercises in this section:
$ cd ~/modern-devops/ch9/lamp-stack

We have created the following custom index.php page that tests the connection to the MySQL database and displays whether it can connect or not:

<?php

mysqli_connect(‘db’, ‘testuser’, ‘Password@1’) or die(‘Could not connect the database :
Username or password incorrect’);
echo ‘Database Connected successfully’;
?>

We create several Ansible playbooks according to the logical steps we follow with CM.
It is an excellent practice to update the packages and repositories at the start of every configuration.
Therefore, we need to start our playbook with this step.
Updating packages and repositories

As we’re using Ubuntu, we can use the apt module to update the packages. We must update packages and repositories to ensure the latest package index is available with all our apt repositories and avoid any untoward issues while installing packages. The following playbook, apt-update.yaml, performs the update:
hosts: webservers:dbservers

become: true tasks:
name: Update apt packages
apt: update_cache=yes cache_valid_time=3600

The YAML file begins with a list of plays and contains a single play in this case. The hosts attribute defines a colon-separated list of roles/hosts inventory to apply the playbook. In this case, we’ve specified webservers and dbservers. The become attribute specifies whether we want to execute the play as a root user. So, as we’ve set become to true, Ansible will perform all play tasks with sudo privileges. The play contains a single task—Update apt packages. The task uses the apt

module and consists of update_cache=yes. It will run an apt update operation on all nodes with the webservers and dbservers roles. The next step is to install packages and services.

Leave a Reply

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