Ansible Automated WordPress Installs

This site is the first site that I used my recently created Ansible script. This script downloads and installs the latest WordPress and get it all configured for me. All I have to do is login and get to work creating the next great site.

Yes I know that I could have written a bash script or something else to do this, but I have really started to get into automation and Ansible was something I looked at back in 2014 and just never had the time to use it. This will also be useful in a future project that I have planned.

The script I have created is mostly a wrapper around the excellent WP-CLI program/script. This script can be used to install WordPress, configure the basic settings, install themes and even content from and XML file if you desire.

The other parts of my script handle things like creating the MySQL database and database user and configuring the web server (Apache).

I will admit that I am not a fan of the YAML format of the config script, however given everything that Ansible does for you and the speed at which you can develop a working script, it is a small inconvenience.

What is really nice is that in about 1 minute (the WordPress download takes the longest), I have a full WordPress install ready for me to go.

#########################################################
#Playbooks: WordPress on local server
#########################################################
--
- hosts: all 
  become: true 
  vars_files: 
    - vars/default.yml
tasks:
# Apache Configuration
- name: Create document root
  file:
  path: "/var/www/{{ http_host }}"
  state: directory
  owner: "www-data"
  group: "www-data"
  mode: '0755'
  tags: [ apache ]

 - name: Set up Apache VirtualHost
   template:
   src: "files/apache.conf.j2"
   dest: "/etc/apache2/sites-available/{{ http_conf }}"
   notify: Reload Apache
   tags: [ apache ]

 - name: Enable new site
   shell: /usr/sbin/a2ensite {{ http_conf }}
   notify: Reload Apache
   tags: [ apache ]

#MySQL Configuration
- name: Creates database for WordPress 
  mysql_db: 
    name: "{{ mysql_db }}" 
    state: present
    login_user: root 
    login_password: "{{ mysql_root_password }}" 
    tags: [ mysql ] 

  - name: Create MySQL user for WordPress 
    mysql_user: 
       name: "{{ mysql_user }}" 
       password: "{{ mysql_password }}" 
       priv: "{{ mysql_db }}.*:ALL" 
       state: present 
       login_user: root 
       login_password: "{{ mysql_root_password }}" 
     tags: [ mysql ]


#WordPress Configuration
- name: Create WordPress directory 
  file: path="/var/www/{{ http_host }}/" 
  owner="www-data" 
  group="www-data" 
  mode=0755 state=directory 
  tags: [ wordpress ] 
- name: Download latest WordPress 
  command: wp core download 
  args: 
    chdir: "/var/www/{{ http_host }}" 
    become: yes 
    become_user: "www-data" 
  tags: [ wordpress ] 

- name: Configure WordPress 
  command: wp core config 
        --path="/var/www/{{ http_host }}/" 
        --dbname="{{ mysql_db }}" 
        --dbuser="{{ mysql_user }}" 
        --dbpass="{{ mysql_password }}" 
        --dbprefix="wp_" 
  args: 
    chdir: "/var/www/{{ http_host }}" 
    become: yes 
    become_user: "www-data" 

- name: Install WordPress tables 
  command: wp core install 
        --url="http://{{ http_host }}" 
        --title="{{sitename}}" 
        --admin_user="{{ wordpress_admin_user }}" 
        --admin_password="{{ wordpress_admin_user_pass }}" 
        --admin_email="{{ wordpress_admin_email }}" 
  args: 
     chdir: "/var/www/{{ http_host }}" 
  become: yes 
  become_user: "www-data" 

- name: Set Site Tagline to default 
  command: wp option update blogdescription "Another Great site being built by Edison Avenue Consulting" 
  args: 
    chdir: "/var/www/{{ http_host }}" 
  become: yes 
  become_user: "www-data" 

- name: Copy Starter Theme onto server 
  copy: src: files/eac_starter.zip 
  dest: /tmp/eac_starter.zip 
  owner: www-data 
  group: www-data 
  mode: u=rw,g=r,o=r - 
  
- name: Activate Starter Theme 
  command: "wp theme install /tmp/eac_starter.zip --activate" 
  args: 
     chdir: "/var/www/{{ http_host }}" 
  become: yes 
  become_user: "www-data"

handlers:
- name: Reload Apache

service:
 name: apache2
 state: reloaded

- name: Restart Apache service: name: apache2 state: r
estarted

 

Leave a Reply

Your email address will not be published. Required fields are marked *