Implementing WordPress with IT Automation: Utilizing Ansible

Mastering Content Management System with Mklabs

#content management system #tutorial #techtutorial #itautomation
December 05, 2023

Overview:

WordPress, the leading open-source Content Management System (CMS) built in PHP, dominates web content sharing. Mastering software installation becomes crucial, and here IT automation tools like Ansible play a vital role. They simplify the installation and management process of such software.

Ansible, a prominent IT automation tool, automates tasks including configuration deployments, cloud provisioning, and application deployment, catering to various IT needs. Its user-friendly framework doesn't demand specific software on your machine and offers a robust suite of features and modules for crafting automation scripts.

By the end of this article, you'll have the know-how to install WordPress effortlessly with just a single click!

Getting Started with Ansible for WordPress Automation

To effectively use Ansible for automating your WordPress setup, certain prerequisites are required:

1. A Control Node: This is a machine where Ansible is installed and configured. It can be a Debian/Ubuntu or Windows OS. For this guide, we focus on installing Ansible on Ubuntu 20.04 LTS. Windows users can refer to the official Ansible documentation for installation instructions.

2. At Least One Host Machine: You need a minimum of one host node where WordPress will be installed. In this instance, we recommend using an Ubuntu machine as your Ansible host.

Before diving into the main content, we'll guide you through installing Ansible on Ubuntu 20.04.

TIP: Consider using a Docker container as your Ansible host for added convenience. For more information on this, check out our 'Getting Started Guide with Docker' for installation details!  
Installation and Configuration of Ansible on Ubuntu 20.04 LTS

To kickstart your journey with Ansible, it's essential to install it on your designated control node. The process is broken down into four primary steps:

STEP 1 - Installing Ansible

Initially, you need to add the Personal Package Archive (PPA) to your system. This can be done by executing the following command:


sudo apt-add-repository ppa:ansible/ansible


Following the addition of the PPA, the next step is to refresh your system’s package index. This is achieved by running its specific command:


sudo apt update

Now, you're ready to proceed with the final step of actually installing Ansible. This can be accomplished by executing the command:


sudo apt install ansible


If you've successfully executed all the previous commands without encountering any errors, your control node is now fully equipped to swiftly deploy WordPress.

STEP 2 - Configuring Ansible

Setting up this IT automation tool is quite straightforward. Initially, you'll find a default configuration file named `ansible.cfg` in its default installation directory, typically located at `/etc/ansible/`. This file can be easily tailored to fit your needs. You might want to set your preferences for aspects like the Python interpreter (using the `interpreter_python` keyword) or specify your vault key file for encrypting and decrypting secrets (using the `vault_password_file` keyword), among others. Below is an example of what this configuration file might look like:


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


To activate your custom configuration file, it's necessary to set the `ANSIBLE_CONFIG` environment variable to its path.

This configuration process allows you to specify your preferences in the following ways, as per the order in the file:

- Roles Path (roles_path keyword): Configure where Ansible should look for the list of roles to use. In our example, both the `galaxy_roles` and `roles` directories are specified.
- Deprecation Warnings (deprecation_warnings keyword): If you wish to eliminate all deprecation warning alerts from your output, this setting can be adjusted accordingly.
- Vault Password File (vault_password_file keyword): Set a password from the specified file (`vault_file` in this example) to manage encrypted data.
- Python Interpreter (interpreter_python keyword): Define the Python interpreter to be used by Ansible.

With these settings adjusted, you have successfully configured Ansible and are now ready to start using it! Continue reading for the subsequent steps!

STEP 3 - PRELIMINARY DEFINITIONS

We're now poised to create a playbook for installing WordPress. But first, let’s define some key components:

- Playbook: A playbook is a .yml (or .yaml) file in which you specify the roles to configure your host.
- Role: A role is a directory with a specific structure (as detailed in the official Ansible documentation ). It contains all the necessary tools for installation on your host.
- Inventory: An inventory file is a simple text file (without an extension) that includes the definition and some parameters of your hosts.

MAIN PLAYBOOK FILE
To begin, create a file named `install-wordpress.yml` and write the following lines:


- hosts: "{{ host | default('wordpress')}}"
become: true
vars:
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: 'admin@example.com'
wp_sitename: example.com
wp_install_dir: "/var/www/example.com"
roles:
- wordpress


In the file, you'll notice the ability to specify the host using the `host` variable. By default, if Ansible doesn't find a defined `host` variable, it assumes 'wordpress' as the host name. This can be set in the inventory file. For instance, you might choose 'production' as the name of the file and place it in the root directory. Here’s how you can do it:


[wordpress]
192.168.33.11


In this example, we'll assume you have a server set up as a host with the IP address 192.168.33.11.

ANSIBLE ENVIRONMENT STRUCTURE

In your main playbook `.yml` file, you have specified a role named 'wordpress'. Now, let's create a directory for this role and structure it as follows:

.
├── 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 for our 'wordpress' role, you will find several subdirectories, each with its specific role. All these subdirectories, except for 'templates', should contain a primary file named `main.yml`, which acts as the entry point for the respective folder.

As a best practice, as outlined in the [Ansible User Guide], it's recommended to use:

- The `defaults` subdirectory to store default values that are used in the playbook. These are the default settings for the variables used in the roles, which can be overridden by variables specified in the playbook.
- The `handlers` subdirectory to group all the services that need to be started or restarted after a change (for instance, an nginx web server).
- The `templates` subdirectory for storing configuration file templates, which can be dynamically customized using variables defined in the playbook.
- The `tasks` subdirectory, which contains all the configurations and instructions to install and configure the software on the host.
FILE CONTENTS

For our 'wordpress' role, the default values that we might include in the `defaults` subdirectory are as follows (keep in mind these are overwritten by the variables chosen in the `vars` section of 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: 'admin@example.com'
wp_webserver: nginx
site_name: "{{ wp_sitename }}"


The handlers subdirectory main file is created as follow:

- name: restart nginx
service:
name: nginx
state: restarted

In our setup, we only have one service defined under handlers, which is the command to restart the Nginx web server.

Next, you'll want to create the main.yml file in the tasks subdirectory. To keep things concise (as it's a lengthy file), we haven't included the full code in this article, but it's available upon request. This file is where you configure all the elements on your Ansible host machine, including WordPress.

STEP 4 - Deploy Configuration On Ansible Host

At this stage, you're ready to deploy the configuration on your host for the first time. To do this, navigate to the root directory of the playbook (where your install-wordpress.yml file is located), open the terminal, and execute the following 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!
As demonstrated, you now have the capability to further configure your WordPress instance with ease!

Conclusions

This article has guided you through the process of installing WordPress with just one click by utilizing Ansible. This approach not only simplifies installation but also enables the configuration of multiple hosts simultaneously. Simply expand the list of IP addresses for the relevant host machines in your inventory file, and you effectively enhance the automation level of your operations.

For the complete code, contact us directly or be sure to visit our Ansible Galaxy page and our GitHub repository.
Mastering Content Management System with Mklabs

Contact us to learn more