Downloading, configuring, and installing WordPress

Now that we have everything in place to install WordPress, we can make a start, first by setting some default variables in roles/wordpress/defaults/main.yml:

wordpress:
domain: "http://{{ ansible_nodename }}/"
title: "WordPress installed by Ansible"
username: "ansible"
password: "password"
email: "[email protected]"
theme: "sydney"
plugins:
- "jetpack"
- "wp-super-cache"
- "wordpress-seo"
- "wordfence"
- "nginx-helper"

Now that we have our variables, we can start our download if we need to:

- name: are the wordpress files already there?
stat:
path: "{{ wordpress_system.home }}/index.php"
register: wp_installed

- name: download wordpresss
shell: "{{ wp_cli.path }} core download"
args:
chdir: "{{ wordpress_system.home }}"
become_user: "{{ wordpress_system.user }}"
become: true
when: wp_installed.stat.exists == False

As you can see, the first task uses the stat module to check for the presence of an index.php in our system user's home directory, which is also the webroot. The second task uses the shell module to issue the wp core download command.

There are a few arguments we should work through before moving on to the next task. These are:

  • args and chdir: You can pass additional arguments to the shell module using args. Here, we are passing chdir, which instructs Ansible to change to the directory we specify before running the shell command we provide.
  • become_user: The user we want to run the command as. If we do not use this, the command will run as the root user.
  • become: This instructs Ansible to execute the task as the defined user.

The next task in the playbook sets the correct permissions on the user's home directory:

- name: set the correct permissions on the homedir
file:
path: "{{ wordpress_system.home }}"
mode: "0755"
when: wp_installed.stat.exists == False

Now that WordPress is downloaded, we can start the installation. First, we need to check whether this has already been done:

- name: is wordpress already configured?
stat:
path: "{{ wordpress_system.home }}/wp-config.php"
register: wp_configured

If there is no wp-config.php file, then the following task will be executed:

- name: configure wordpress
shell: "{{ wp_cli.path }} core config --dbhost={{ mariadb.bind }} --dbname={{ wp_database.name }} --dbuser={{ wp_database.username }} --dbpass={{ wp_database.password }}"
args:
chdir: "{{ wordpress_system.home }}"
become_user: "{{ wordpress_system.user }}"
become: true
when: wp_configured.stat.exists == False

Now that we have our wp-config.php file created, with the database credentials in place, we can install WordPress. First, we need to check whether WordPress has already been installed:

- name: do we need to install wordpress?
shell: "{{ wp_cli.path }} core is-installed"
args:
chdir: "{{ wordpress_system.home }}"
become_user: "{{ wordpress_system.user }}"
become: true
ignore_errors: yes
register: wp_installed

As you can see from the presence of the ignore_errors option, if WordPress is not installed, this command will give us an error. We are then using this to our advantage when registering the results, as you can see from the following task:

- name: install wordpress if needed
shell: "{{ wp_cli.path }} core install --url='{{ wordpress.domain }}' --title='{{ wordpress.title }}' --admin_user={{ wordpress.username }} --admin_password={{ wordpress.password }} --admin_email={{ wordpress.email }}"
args:
chdir: "{{ wordpress_system.home }}"
become_user: "{{ wordpress_system.user }}"
become: true
when: wp_installed.rc == 1

Now we have a basic WordPress site installed, we can progress with installing the plugins and theme files.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.142.248