Community Blog Deploying Drupal 8 with Ansible playbook

Deploying Drupal 8 with Ansible playbook

In this article, you will get some information on the deployment of Drupal 8 using Ansible Playbook on an Alibaba Cloud Elastic Compute Service (ECS) instance.

How to Create Ansible playbook for Deploying Drupal 8


  1. Two Alibaba Cloud ECS instances with Ubuntu 16.04 64-bit installed. You can follow this Quick Start Guide to create the instances. Let's call the first instance as "web-server" and assume the public IP assigned to the server is "". We will use this server to install Drupal using Drush on NGINX with PHP 7.2. Similarly, we will call our second server as "db-server" having public IP "". We will install MariaDB server on this instance to host the Drupal database. Make sure to use SSH Key based authentication rather than password authentication and use the same key for both the instance.
  2. Important: Firewall or Security group rules configured to allow the port "80", "443" for everyone from the "web-server" instance. For the database server, you will only need to allow access to the private IP address of the "web-server" instance on port "3306".

This tutorial assumes that you have already created both the instance and configured SSH key-based authentication on the instances with the same key. Here are the goals which we will accomplish using Ansible:

  1. Install the MariaDB server and create the database for Drupal on the instance named "db-server".
  2. Install the PHP 7.2 on the instance "web-server".
  3. Clone the Drupal 8 and Drush 9 Github repository and install Drupal on the instance "web-server".
  4. Install the NGINX Web server to host the Drupal site with optional Let's Encrypt SSL on the instance "web-server".

How to Deploy Drupal 8 with Ansible Playbook

By default, MariaDB listens to the localhost addresses only. In our scenario, we are using one server to host the database server and the other one will be used for installing Drupal. We will need to modify the MariaDB configuration so that it can listen to all address. In our case, the ECS server running Drupal will make queries to the database server instance. The below task will find the line starting with bind-address in the MySQL configuration file /etc/mysql/my.cnf. Upon matching, it will take the backup of the file and will replace the whole line with bind-address = It will then notify the handler to run the job named with restart mariadb.

Finally, create the database for Drupal and also create a new database user having all the privileges over the database we have created.

- name: create a new database for Drupal
  mysql_db: name={{ drupal_db_name }} state=present

- name: create a new database user for Drupal
  mysql_user: name={{ drupal_db_user }}  password={{ drupal_db_pass }}
                priv="{{ drupal_db_name }}.*:ALL" state=present host={{ hostvars['web-server']['ansible_default_ipv4']['address'] }}

Notice the variable hostvars'web-server'['address'] in the last task. The variable will result in the private IP address of instance named "web-server" which is the instance on which we will install Drupal. This will make sure that our database can be used only from that instance.

How to Create Drupal Role with Ansible Playbook

In this article, you will get some information on the creation of Drupal role for further running of Ansible playbook.

Drupal Role

Create a new directory for "drupal" role and subdirectories for tasks.

mkdir -p roles/drupal/tasks

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

nano roles/drupal/tasks/main.yaml

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. Notice the "creates" keyword in the arguments. It simply verifies if the said file is created or not. The next task installs the Git.

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.

Related Market Product

Drupal on LAMP CentOS 7.4 Lite

Drupal is an open source content-management framework written in PHP and distributed under the GNU General Public License. Drupal supports from personal blogs to corporate, political, and government sites. Systems also use Drupal for knowledge management and for business collaboration.

Related Products

Elastic Compute Service

Alibaba Cloud Elastic Compute Service (ECS) provides fast memory and the latest Intel CPUs to help you to power your cloud applications and achieve faster results with low latency. All ECS instances come with Anti-DDoS protection to safeguard your data and applications from DDoS and Trojan attacks.

Alibaba Cloud SSL Certificates Service

Alibaba Cloud SSL Certificates Service allows customers to directly apply, purchase and manage SSL certificates on Alibaba Cloud. This service is offered in cooperation with qualified certificate authorities. From this platform, customers can select the expected certificate authority and its certificate products to enjoy full-site HTTPS security solutions.

Related Course

Construct a Dynamic Website (Cantonese)

How to construct a dynamic website that can actively update its content? In this Clouder lesson, you will learn how to build a WordPress website on Alibaba Cloud quickly using ECS marketplace image. The language of instruction in the video course is Cantonese. All presentation materials and exam questions are in English.

0 0 0
Share on

Alibaba Clouder

2,600 posts | 751 followers

You may also like