Ansible – install and configure WordPress for multiple nodes

For install and configure ansible with multiple nodes please read the “Ansible install configure and use” article. For understand playbooks please read the article “Ansible playbooks“. Playbooks can be very hardly on handling. And for that in files with right structure for all task groups we must use right folder structure.

This infrastructure give us possibilities use same configuration files with different projects. The name of this Ansible ecosystem role.

In our case we will create two role. One of them(wp-dependencies) will install dependency packages for WordPress(will install PHP and MariaDB – we don’t need the apache because already installed).

The other role(name is wp-install-config) will add all tasks for install and configure WordPress.

Create the Ansilbe Roles
By default ansible is coming with ansible-galaxy named utility which, help us create folder infrastructure in our roles. We will do our playbooks in our /etc/ansible/playbooks folder but by theory you can do this in different folder too.

We doing works in ansmaster.unixmen.com server:
root@ansmaster:/# cd /etc/ansible/playbooks
root@ansmaster:/etc/ansible/playbooks# ansible-galaxy init wp-dependencies
– wp-dependencies was created successfully
root@ansmaster:/etc/ansible/playbooks# ansible-galaxy init wp-install-config
– wp-install-config was created successfully

See the newly created roles:
root@ansmaster:/etc/ansible/playbooks# ls -R /etc/ansible/playbooks
/etc/ansible/playbooks:
apache.yml  wp-dependencies  wp-install-config

/etc/ansible/playbooks/wp-dependencies:
defaults  files  handlers  meta  README.md  tasks  templates  vars

/etc/ansible/playbooks/wp-dependencies/defaults:
main.yml

/etc/ansible/playbooks/wp-dependencies/files:

/etc/ansible/playbooks/wp-dependencies/handlers:
main.yml

/etc/ansible/playbooks/wp-dependencies/meta:
main.yml

/etc/ansible/playbooks/wp-dependencies/tasks:
main.yml

/etc/ansible/playbooks/wp-dependencies/templates:

/etc/ansible/playbooks/wp-dependencies/vars:
main.yml

In command below we saw two folders is created by ansible-galaxy command for role names which we set before. In the each of created folder will be created defaults, files, handlers, meta, tasks, templates, vars sub folders and README.md file.

And in the all subfolders already created file with name main.yml.

We will add the following lines to the /etc/ansible/playbooks/wp-dependencies/tasks/main.yml file. Please note, if you will not do each steps of documentation which I referred, for any situation I added httpd too.

# tasks file for wp-dependencies
– name: Update packages (this is equivalent to yum update -y)
yum: name=* state=latest

– name: Install dependencies for WordPress
yum: name={{ item }} state=present
with_items:
– httpd
– mariadb-server
– mariadb
– php
– php-mysql
– MySQL-python
– name: Ensure MariaDB is running (and enable it at boot)
service: name=mariadb state=started enabled=yes

– name: Copy ~/.my.cnf to nodes
copy: src=/root/.my.cnf dest=/root/.my.cnf

– name: Create MariaDB database
mysql_db: name={{ wp_mysql_db }} state=present

– name: Create MariaDB username and password
mysql_user:
login_user=root
login_password=passforansible
name={{ wp_mysql_user }}
password={{ wp_mysql_password }}
priv=*.*:ALL

Create the /etc/ansible/playbooks/wp-dependencies/defaults/main.yml file and add the following lines (db name, username and password is using for WordPress connect):

# defaults file for wp-dependencies
wp_mysql_db: MyWP
wp_mysql_user: wpUser
wp_mysql_password: wpP4ss

Add the following lines to the /etc/ansible/playbooks/wp-install-config/tasks/main.yml file:

# tasks file for wp-install-config
– name: Create directory to download WordPress
command: mkdir -p /opt/source/wordpress

– name: Download WordPress
get_url: url=https://www.wordpress.org/latest.tar.gz dest=/opt/source/wordpress/wordpress.tar.gz validate_certs=no

– name: Extract WordPress
command: “tar xzf /opt/source/wordpress/wordpress.tar.gz -C /var/www/html –strip-components 1”

– name: Send config file
copy: src=/root/wp-config-sample.php dest=/var/www/html/wp-config.php mode=0644

After that download wp-config-sample.php configuration file from the url to our ansmaster.unixmen.com server as /root/wp-config-sample.php. But don’t forget you must change DB_NAME, DB_USER and DB_PASSWORD variables as you already set in the /etc/ansible/playbooks/wp-dependencies/defaults/main.yml file.
<?php
define(‘DB_NAME’, ‘MyWP‘);
define(‘DB_USER’, ‘wpUser‘);
define(‘DB_PASSWORD’, ‘wpP4ss‘);
define(‘DB_HOST’, ‘localhost’);
define(‘DB_CHARSET’, ‘utf8’);
define(‘DB_COLLATE’, ”);
define(‘AUTH_KEY’,         ‘put your unique phrase here’);
define(‘SECURE_AUTH_KEY’,  ‘put your unique phrase here’);
define(‘LOGGED_IN_KEY’,    ‘put your unique phrase here’);
define(‘NONCE_KEY’,        ‘put your unique phrase here’);
define(‘AUTH_SALT’,        ‘put your unique phrase here’);
define(‘SECURE_AUTH_SALT’, ‘put your unique phrase here’);
define(‘LOGGED_IN_SALT’,   ‘put your unique phrase here’);
define(‘NONCE_SALT’,       ‘put your unique phrase here’);
$table_prefix  = ‘wp_’;
define(‘WP_DEBUG’, false);
if ( !defined(‘ABSPATH’) )
define(‘ABSPATH’, dirname(__FILE__) . ‘/’);
require_once(ABSPATH . ‘wp-settings.php’);

Note: Be sure, you already configured mysql root password for each of node1ans and node2ans nodes. You can do this with mysql_secure_installation command.

Be sure you are using the same username/password for all nodes and write this username with password on the ansmaster.unixmen.com server /root/.my.cnf file. Be carefully, the username and password on this file must be same as /etc/ansible/playbooks/wp-dependencies/tasks/main.yml file.
root@ansmaster:/# cat /root/.my.cnf
[client]
user=root
pass=passforansible

After we will add the following lines to our playbook /etc/ansible/playbooks/playbook.yml file:
– hosts: webservers
roles:
– wp-dependencies
– wp-install-config

Execute the playbook(As we see, in the node2ans and node3ans servers MysQL connection was not successfully):
root@ansmaster:/# ansible-playbook /etc/ansible/playbooks/playbook.yml

PLAY [webservers] *************************************************************

GATHERING FACTS ***************************************************************
ok: [node3ans.unixmen.com]
ok: [node2ans.unixmen.com]

TASK: [wp-dependencies | Update packages (this is equivalent to yum update -y)] ***
ok: [node3ans.unixmen.com]
ok: [node2ans.unixmen.com]

TASK: [wp-dependencies | Install dependencies for WordPress] ******************
changed: [node3ans.unixmen.com] => (item=httpd,mariadb-server,mariadb,php,php-mysql,MySQL-python)
changed: [node2ans.unixmen.com] => (item=httpd,mariadb-server,mariadb,php,php-mysql,MySQL-python)

TASK: [wp-dependencies | Ensure MariaDB is running (and enable it at boot)] ***
changed: [node3ans.unixmen.com]
changed: [node2ans.unixmen.com]

TASK: [wp-dependencies | Copy ~/.my.cnf to nodes] *****************************
changed: [node2ans.unixmen.com]
changed: [node3ans.unixmen.com]

TASK: [wp-dependencies | Create MariaDB database] *****************************
failed: [node2ans.unixmen.com] => {“failed”: true}
msg: unable to connect, check login credentials (login_user, and login_password, which can be defined in ~/.my.cnf), check that mysql socket exists and mysql server is running
failed: [node3ans.unixmen.com] => {“failed”: true}
msg: unable to connect, check login credentials (login_user, and login_password, which can be defined in ~/.my.cnf), check that mysql socket exists and mysql server is running

FATAL: all hosts have already failed — aborting

PLAY RECAP ********************************************************************
to retry, use: –limit @/home/jamal/playbook.retry

node2ans.unixmen.com     : ok=5    changed=3    unreachable=0    failed=1
node3ans.unixmen.com     : ok=5    changed=3    unreachable=0    failed=1

For that we will use the mysql_secure_installation command in our node1ans and node2ans nodes and will set mysql root password as passforansible.

Then we again will execute playbook in our ansmaster.unixmen.com server:
root@ansmaster:/# ansible-playbook /etc/ansible/playbooks/playbook.yml

PLAY [webservers] *************************************************************

GATHERING FACTS ***************************************************************
ok: [node3ans.unixmen.com]
ok: [node2ans.unixmen.com]

TASK: [wp-dependencies | Update packages (this is equivalent to yum update -y)] ***
ok: [node2ans.unixmen.com]
ok: [node3ans.unixmen.com]

TASK: [wp-dependencies | Install dependencies for WordPress] ******************
ok: [node3ans.unixmen.com] => (item=httpd,mariadb-server,mariadb,php,php-mysql,MySQL-python)
ok: [node2ans.unixmen.com] => (item=httpd,mariadb-server,mariadb,php,php-mysql,MySQL-python)

TASK: [wp-dependencies | Ensure MariaDB is running (and enable it at boot)] ***
ok: [node2ans.unixmen.com]
ok: [node3ans.unixmen.com]

TASK: [wp-dependencies | Copy ~/.my.cnf to nodes] *****************************
ok: [node3ans.unixmen.com]
ok: [node2ans.unixmen.com]

TASK: [wp-dependencies | Create MariaDB database] *****************************
changed: [node3ans.unixmen.com]
changed: [node2ans.unixmen.com]

TASK: [wp-dependencies | Create MariaDB username and password] ****************
changed: [node3ans.unixmen.com]
changed: [node2ans.unixmen.com]

TASK: [wp-install-config | Create directory to download WordPress] ************
changed: [node3ans.unixmen.com]
changed: [node2ans.unixmen.com]

TASK: [wp-install-config | Download WordPress] ********************************
changed: [node3ans.unixmen.com]
changed: [node2ans.unixmen.com]

TASK: [wp-install-config | Extract WordPress] *********************************
changed: [node3ans.unixmen.com]
changed: [node2ans.unixmen.com]

TASK: [wp-install-config | Send config file] **********************************
changed: [node3ans.unixmen.com]
changed: [node2ans.unixmen.com]

PLAY RECAP ********************************************************************
node2ans.unixmen.com     : ok=11   changed=6    unreachable=0    failed=0
node3ans.unixmen.com     : ok=11   changed=6    unreachable=0    failed=0

At the end we can go to web browser and open the IP address for node1ans and node2ans servers for installation WordPress. You must see the following pages for each of nodes:
Wordpress-From-Ansible

NO COMMENTS