Deploying Drupal 8 using Ansible Playbook: Part 3

By Liptan Biswas, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

In previous tutorials of this series, we have created our playbook file and two of the four roles. In the first part of the tutorial, we looked at creating our project and overriding the default Ansible behavior. In the second part of the tutorial, we have written the plays into roles.

In this final part of the tutorial series, we will create the two remaining roles. Once the playbook is created, we will run the playbook using Ansible.

Drupal Role

Create a new directory for “drupal” role and subdirectories for tasks.

Now, create a new YAML file to write the tasks of “drupal” role.

The Composer is used to manage the dependencies of a php-based project. Since we will be cloning the Git repository onto the server, we will require Composer to install the dependencies. The task below will run a shell command which will install the latest version of the Composer in the system.

Notice the “creates” keyword in the arguments. It simply verifies if the said file is created or not. The next task installs the Git. Installation can be done using both “package” and “apt” module.

Now that Git is installed, clone the Drush repository using the “git” module of the Ansible. Drush is a command line tool to install Drupal.

Install the Drush dependencies using Composer.

Create a soft link so that the Drush executable is called directly. The “file” module is used to create the soft links.

Install MySQL client. MySQL client is used by Drush to write the Drupal database.

Create the directory to install Drupal. The “file” module is also used to create the directory.

Clone the Drupal repository.

Install the Drupal dependencies with the Composer.

Use Drush executable to install Drupal. We have used Ansible variables for providing the website name and the administrator credentials in the variable file.

Finally, set the proper ownership and permissions on Drupal directories and files.

Save the file and exit from the editor. The role for installing the Drupal is also created. Since we have used some variable in the role we have created, let’s add them in the global variable file we have created.

Edit the common global variables file.

Append the following variables at the end of the line.


Our Drupal website is now installed, but we will need to install NGINX web-server to expose the Drupal site to the internet. The last role “nginx” in our Ansible playbook will install the NGINX web-server along with optional SSL support by let’s encrypt CA. The user can set the variable to true to generate the let’s encrypt certificate in the global variables file. If the SSL cert is generated, then we will configure Drupal to be accessed from the domain name. Else, the Drupal site will be accessed from the IP address of the “web-server” instance.

Create a new directory for “nginx” role and subdirectories for tasks, handlers, and templates.

Now, create a new YAML file to write the tasks of the “nginx” role.

Populate the file with the following tasks. The first task simply checks for the Apache web server. If it is installed, then Ansible removes it from the system. The second task installs the NGINX web server in the system.

The next task adds the Certbot repository in the system if letsencrypt_generate variable is set to true and the value of the domain and email variables are provided. This task is an example of using conditional statements in Ansible. Look at the clause “when”. If the conditions provided under the “when” clause satisfies, then only the task will be executed.

Similarly, install Certbot for NGINX if the let’s encrypt conditions satisfies.

Make sure that the NGINX web-server is started because to generate the certificates from Let’s Encrypt, NGINX must be accepting connections.

Now, generate the SSL certificates using the Certbot client.

Remove the default NGINX configuration file.

Generate a new configuration for Drupal. Notice that we are copying from a template. Later on this tutorial, we will write the NGINX configuration file for Drupal.

Finally, create the Cron Job to automatically renew the Let’s Encrypt SSL certificates.

Save the file and exit from the editor. Our role for NGINX is now created. Let’s add the variables we have used in this role in the variables file.

Edit the common global variables file.

Append the following variables at the end of the line.

Our variables are now in place, let’s proceed to create the handler which restarts the NGINX web-server when requested.

Create a new YAML file to store the Ansible code for the handler.

Populate the file with the following code.

Save the file and exit from the editor. The last thing we need to do in the “nginx” role is to create the configuration file as template “drupal.conf” in the template directory.

Create the new template named “drupal.conf”.

Populate the file with the following text.

Save the file and exit from the editor. In the configuration file above, you can see that we have used conditional statement with {% if letsencrypt_generate and letsencrypt_domain are defined and letsencrypt_email is defined %} line. Ansible will only process the code block if the condition turns out true. In the file above, if the Let's Encrypt certificates are generated than only it will process the first "if" block. If not, it will process the second "if" block.

Congratulations. You have written the Ansible playbook needed to install Drupal. The following file structure should have been created on your system.

Running the Playbook

Now that everything is created, run the playbook and let Ansible do the Drupal installation for you.

You should see following out. If you have followed the tutorial correctly, you should not get an error message.

Once the playbook completes all the tasks successfully, you can visit or according to your choice of Let’s Encrypt installation. You should see the default Drupal page. You can log in using the credentials supplied in the variables file. You should see that the Drupal is installed and is ready to create a site.


In this detailed tutorial, we have learned how to use Ansible by creating a playbook to install Drupal on Ubuntu 16.04. Ansible is a great tool for configuration management due to its simplicity. Adoption is very easy with Ansible, far easier than writing a bash script. It can be used in any use case where repetition is required. Ansible follows the concept of idempotency. Idempotency is doing only necessary changes without side effects. For example, when you install a package using a playbook. On the first run, Ansible will install the package for you. But on the second run of the same playbook, Ansible will just verify if the package is installed. If it is installed, Ansible will just skip the job without reinstalling the package.

Similarly, you can also write playbooks for installing various CMS such as WordPress. The Ansible playbook we have created in this tutorial can also be found on my Github repository.


Follow me to keep abreast with the latest technology news, industry insights, and developer trends.