Ansible for IT Automation: WordPress as an example


Content sharing in the web world is primarily provided by WordPress, an open-source Content Management System (CMS from now on) written in PHP language. For this reason, it is very important to master the software installation. So, the aid of IT automation and its tools (like Ansible) is really important, because in this way you can easily install and manage the software.

As we said before, one of these software is Ansible, which is an IT automation engine that automates configuration deployments, cloud provisioning, application deployment, and other IT needs. It offers a simple framework that doesn’t require specific software to be installed on your machine. It also provides a strong set of features and modules which facilitate writing automation scripts.

Once you have completed reading this article, you will be able to easily install WordPress with literally 1-click!

Prerequisites to install Ansible

To use Ansible to automate your WordPress installation, you need:

  • A control node: a machine with Debian/Ubuntu or Windows OS with Ansible installed and configured to connect to remote hosts using SSH keys. In this article, we provide a tutorial to install Ansible on Ubuntu 20.04 LTS. If you want to install it on Windows OS, you can follow the official documentation here;
  • At least one host: you must have at least one node where you can install WordPress. Here as well we have chosen an Ubuntu machine as Ansible host;

Before proceeding with the article, we have to show how to install Ansible in Ubuntu 20.04.

📍 TIP: You can use a Docker container as an Ansible host. Check out our Getting Started Guide with Docker article to know how to install it!

How to install and configure Ansible on Ubuntu 20.04 LTS

To begin using Ansible, you have to install it in your target control node. We proceed into 4 principal steps:

Step 1 - Installing Ansible

First of all, you have to include the Personal Package Archive (PPA) launching the following command:

sudo apt-add-repository ppa:ansible/ansible

After doing that, you must refresh the system’s package index with its specific command:

sudo apt update

so now you can finally install Ansible with command:

sudo apt install ansible

If all of the previous commands are executed without any errors, your control node has all it needs to fast deploy the WordPress installation.

Step 2 - Configuring Ansible

Configuring this IT Automation tool is very simple. First of all, there is a default configuration file (called ansible.cfg) in its default installation directory, generally located at /etc/ansible/. You can simply customize it with your preferences, such as your Python interpreter (with the keyword interpreter_python), your vault key file to encrypt and decrypt your secrets (with the keyword vault_password_file), etc. We provide an example of this configuration file below:

roles_path = galaxy_roles:roles
deprecation_warnings = False
vault_password_file = ./vault_file
interpreter_python = /usr/bin/python3

To use this custom configuration file, you have to set the ANSIBLE_CONFIG environment variable, with its path.

This configuration allows specifying our preferences. Following the order of the file:

  • you can configure where Ansible has to take the list of roles to use (with the keyword roles_path). In this case both galaxy_roles and roles directories;
  • with deprecation_warnings you can remove from the output all deprecation warning alarms;
  • you can set a password defined in the specified file (vault_file in this case) with the keyword vault_password_file;
  • the Python interpreter can be set with interpreter_python keyword;

At this point, you have successfully configurated Ansible and you can start to use it! Now, keep reading for the other steps!

Step 3 - Set Ansible playbook

Preliminary definitions

Now we are ready to set up the playbook to install WordPress. Before that, we have to provide some definitions:

  • playbook: a playbook is a .yml (or .yaml) file where you specify the roles used to configure your host;
  • role: the role is a directory with a defined structure (follow the official Ansible documentation), which contains all the tools to install in your host;
  • inventory: an inventory file is a plain text file (without extension), which contains the definition and some parameters.

Main playbook file

First of all, you must create a file called install-wordpress.yml and write the following lines:

- hosts: "{{ host | default('wordpress')}}"
  become: true
    wp_version: 5.7.1
    wp_webserver: nginx
    wp_mysql_db: 'database'
    wp_mysql_user: 'mysql_user'
    wp_mysql_password: 'mysql_pass'
    wp_admin_email: '[email protected]'
    wp_install_dir: "/var/www/"
    - wordpress

As you can see in the file, you can specify the host with host variable. By default, if Ansible does not see the definition of host variable, it takes wordpress as host name. You can set it in the inventory file (for example, you can choose production as the name of the file and put it in the root directory) as follows:

Here, we pretend to have a server as a host which has as IP address.

Ansible environment structure

In the main playbook .yml file you have specified a role called wordpress. Now, let’s create a directory for that and the structure as follow:

├── roles
│   ├── defaults
│   │   └── main.yml
│   ├── handlers
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   └── templates
│       ├── nginx-vhost.j2
│       └── wp-config.php.j2
├── production
└── install-wordpress.yml

In the directory tree, we can find some subdirectories, with their own role. All these subdirectories (except templates) must contain the main file (called main.yml), which is the entry point of the specific folder.

As best practice, you should use the defaults subdirectory to store some defaults values that you use in the playbook, the handlers subdirectory to group all the services you have to start (in this case, for example, nginx webserver), while the templates subdirectory contains some configuration file templates, which you can programmatically customize with variables defined in the playbook. In the end, we have the tasks subdirectory which contains all the configurations to install on the host.

File contents

In this case, the default values that we can put into the defaults subdirectory, are these ones (they are overwritten by the variables chosen in vars section in the main playbook file install-wordpress.yml):

wp_version: 5.0.3
wp_install_dir: '/var/www/html'
wp_db_name: "{{ wp_mysql_db }}"
wp_db_user: "{{ wp_mysql_user }}"
wp_db_password: "{{ wp_mysql_password }}"
wp_db_host: 'localhost'
wp_db_charset: 'utf8'
wp_db_collate: ''
wp_table_prefix: 'wp_'
wp_debug: false
wp_admin_email: '[email protected]'
wp_webserver: nginx
site_name: "{{ wp_sitename }}"

The handlers subdirectory main file is created as follow:

- name: restart nginx
    name: nginx
    state: restarted

Here we have only one service, which is the restart command of the Nginx webserver.

After that, you can create the main file in the tasks subdirectory. For the sake of simplicity (it’s a very long file), we do not provide the full code in this article, but you can find it in the zip folder below.

In this file, you can configure all elements in your Ansible host machine, such as WordPress.

Step 4 - Deploy configuration on Ansible host

Here, you should be able to deploy the configuration on your host for the first time. To do that, you have to go in the root directory of the playbook (the same root where the install-wordpress.yml playbook file is), open the terminal and launch this command:

$ ansible-playbook -i production install-wordpress.yml -v

Now, the command should end without any errors. After that, you can start to use your WordPress on your Ansible host!

NB: be sure to open port 80 (or 443 for HTTPS protocol) on the host firewall!

Ansible WordPress installation

As you can see above, now you can continue configuring your WordPress instance! 


In this article, we have shown how to simply install WordPress with one click by using Ansible. This allows us to configure more than one host at the same time, simply by increasing the number of IP addresses of the related host machines in our inventory file. By doing so, we are able to increase the degree of automation of our work.

You can find the full code in our Ansible Galaxy and in our GitHub repository.