Ansible Playbook to Install WordPress with LAMP

Are you trying to use Ansible Playbook to Install WordPress?

This guide is for you.

Suppose we have to install WordPress for testing, development, and main production website. Instead of repeating the installation three times, it is easy to write an Ansible playbook, which can be run from a different location to install WordPress on the target system(s).

Ansible is one of the configuration management tools that help to automate server setup like WordPress.

Using the Ansible playbook also reduces human errors which are prone to occur during manual server setups.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform server setups with the help of the Ansible playbook.

In this context, we shall look into how to use Ansible to automate server setups.

Steps for using Ansible Playbook to Install WordPress with LAMP on Ubuntu 18.04 ?

We will be using Ansible to automate the installation of WordPress with LAMP on Ubuntu 18.04.

The prerequisites for the setup is an Ubuntu 18.04 server with Ansible as a control node and one remote Ubuntu 18.04 server as a host.

The steps our Support Techs follow are given below:

1. Writing the Ansible Playbook

The playbook, in Ansible terminology, consists of a set of hosts on which we can perform automation.

Steps to follow are given below:

i. First we need to create a directory where we can store all of our configurations:

$ mkdir wordpress-playbook
$ cd wordpress-playbook
$ mkdir roles
$ touch hosts
$ touch playbook.yml

ii. Next, we will open up the hosts file and add the desired name along with the IP address(es) of the server(s) where WordPress will be installed in the following format:


iii. After this we will define the different roles in the roles sub-directory:

$ cd roles
$ ansible-galaxy init server
$ ansible-galaxy init php
$ ansible-galaxy init mysql
$ ansible-galaxy init wordpress

2. Creating Roles

Ansible instructions are written in .yml files which are quite human-readable.

We will write for each individual role of the LAMP stack and WordPress build on top of it.

i. Playbook.yml

First, add the following contents into the file wordpress-playbook/playbook.yml:

- hosts: all
gather_facts: False
- name: install python 2
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
- hosts: wordpress
- server
- php
- mysql
- wordpress

This installs Python 2.7 onto all the target servers and assigns four roles to the hosts.

ii. Server Role

We will add the following contents into the file wordpress-playbook/roles/server/tasks/main.yml :

# tasks file for server
- name: Update apt cache
apt: update_cache=yes cache_valid_time=3600
become: yes
- name: Install required software
apt: name={{ item }} state=present
become: yes
- apache2
- mysql-server
- php7.2-mysql
- php7.2
- libapache2-mod-php7.2
- python-mysqldb

iii. PHP

Here we will install additional PHP modules that write the following contents to the file wordpress-playbook/roles/php/tasks/main.yml :

# tasks file for php
- name: Install php extensions
apt: name={{ item }} state=present
become: yes
- php7.2-gd
- php7.2-ssh2

iv. MySQL

Next, we will add the following contents into the file  wordpress-playbook/roles/mysql/defaults/main.yml :

# defaults file for mysql
wp_mysql_db: wordpress
wp_mysql_user: wordpress
wp_mysql_password: randompassword

After this, we configure our main MySQL task of creating a MySQL user, database and granting that user access to the newly created database.

In the file, wordpress-playbook/roles/mysql/tasks/main.yml add the following:

- name: Create mysql database
mysql_db: name={{ wp_mysql_db }} state=present
become: yes
- name: Create mysql user
name={{ wp_mysql_user }}
password={{ wp_mysql_password }}
become: yes

v. WordPress

To get the WordPress tar file from the official site, extract it and modify the wp-config.php with appropriate data we will add the following contents into wordpress-playbook/roles/wordpress/tasks/main.yml :

- name: Download WordPress
- name: Extract WordPress
unarchive: src=/tmp/wordpress.tar.gz dest=/var/www/ copy=no
become: yes
- name: Update default Apache site
become: yes
regexp="(.)+DocumentRoot /var/www/html"
line="DocumentRoot /var/www/wordpress"
- restart apache
- name: Copy sample config file
command: mv /var/www/wordpress/wp-config-sample.php /var/www/wordpress/wp-config.php creates=/var/www/wordpress/wp-config.php
become: yes
- name: Update WordPress config file
regexp="{{ item.regexp }}"
line="{{ item.line }}"
- {'regexp': "define\\('DB_NAME', '(.)+'\\);", 'line': "define('DB_NAME', '{{wp_mysql_db}}');"}
- {'regexp': "define\\('DB_USER', '(.)+'\\);", 'line': "define('DB_USER', '{{wp_mysql_user}}');"}
- {'regexp': "define\\('DB_PASSWORD', '(.)+'\\);", 'line': "define('DB_PASSWORD', '{{wp_mysql_password}}');"}
become: yes

Finally, we will add the following snippet to wordpress-playbook/roles/wordpress/handlers/main.yml :

# handlers file for wordpress
- name: restart apache
service: name=apache2 state=restarted
become: yes

3. Running Ansible

We can use the following command:

$ ansible-playbook playbook.yml -i hosts -u username -K

[Need urgent assistance to setup WordPress Website? We are happy to help you!]


This article will guide you on the step to follow to use #Ansible Playbook to install #WordPress with #LAMP on #Ubuntu 18.04.

In Ansible, the role is the primary mechanism for breaking a #playbook into multiple files. This simplifies writing complex #playbooks, and it makes them easier to reuse. The breaking of playbook allows you to logically break the playbook into reusable components.

To start using Ansible:

1. Prerequisites.

2. Install Ansible.

3. Establish a manual connection to a managed node.

4. Run your first network Ansible command.

5. Create and run your first network Ansible Playbook.

6. Gathering facts from network devices.

For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on

Also for Tech related tips, Visit

Related Posts

Focus on your business, not your servers.

Click Here to Learn More

Recent Post