portfolio_page-template-default,single,single-portfolio_page,postid-16015,ajax_fade,page_not_loaded,,qode-theme-ver-11.2,qode-theme-bridge,wpb-js-composer js-comp-ver-5.2.1,vc_responsive


When I first started working in the DevOps field, I was amazed how things can be automated. So much manual work as eliminated. That’s great, isn’t it?

This week I got a new client. I build WordPress sites for fun and profit and I had to spin a new instance.
That’s the same routine. Create the NGINX virtual host configuration file for the domain, download WordPress zip file, create the database…

I have been working with Ansible for a while now and I haven’t thought of automating this workflow. What the hell?! I could have saved a lot of time. I am going to do this now.

This article will cover setting up an WordPress installation.

  • Create the Nginx vhost config file for this site
  • Download wordpress zip file and extract it at /var/www/domain (domain refers to the domain name, not the word ‘domain’)
  • Create Mariadb user and database for this site
  • Create wp-config.php with the database details so the web install will be fast

This article will not cover:

  • Nginx installation
  • MariaDB installation
  • Other general stuff

They will be added as separate roles later. Subscribe to this blog to receive new posts via email.

I use this workflow so the Ansible role will be used for this kind of workflow only.
This article serves as a general tutorial. But the role can be updated later to support more automation workflows.

I know wp-cli can be used to install WordPress but I don’t want to use it. Mainly because with Ansible you have more control and it can be expanded more in the buildchain processes (ie. create an instance with just a click using Jenkins)

Let’s get started.

When writing roles, we will create necessary folders when there is a need for them. Now we will start with 4 directories:

  • tasks (tasks that will run by ansible)
  • vars (this is for variables that change frequently)
  • handlers (actions that run after a task, for example ‘restart nginx’)
  • templates (files that serve as templates which will be different depending on our vars)

From my point of view we need the following variables which will be added to the main.yml at vars folder.

Now we have all needed variables. Let’s start with the tasks. We create tasks/main.yml.

1. First we transfer the Nginx virtual host configuration template for WordPress.

2. We need to create the domain root directory for the new site.

3. At this point the WordPress zip file should be downloaded and unzipped at the site directory.

4. Unzipping the archive will create a folder named ‘wordpress’ at our directory. We don’t want this. So we should move the files to the parent directory and delete this folder.

5. The bellow tasks will fix the permissions for the files and directories.

6. Now it is time to copy the wp-config.php template to the host. It contains the new database name and credentials. Also we delete wp-config-sample.php which is not needed.

7. After that, we have to create the database and the new user for this site.

8. The last step is restart Nginx and show the finished message with the site domain name.

Now we should add the handler for restarting Nginx.

A sample for playbook.yml to run the above role

Written by Arjon Bujupi