Ansible: Roles

Follow these steps to create an organized, reusable Ansible role that deploys WordPress on your webserver. This method uses best practices with handler, variable, and task separation for maintainability.


1. Create Roles Directory

Open terminal on your Ansible controller (10.10.10.127):

mkdir -p wordpress-ansible/roles
cd wordpress-ansible/roles

2. Initialize the Role Structure

Use Ansible Galaxy tool to scaffold a role named wordpress:

ansible-galaxy init wordpress

This creates the following structure:

wordpress/
├── defaults/
│   └── main.yml      # Default variables for the role
├── files/            # Static files
├── handlers/
│   └── main.yml      # Handler definitions (e.g., restart Apache)
├── meta/
│   └── main.yml      # Role metadata
├── tasks/
│   └── main.yml      # Main procedure steps
├── templates/        # Jinja2 template files (e.g., wp-config.php.j2)
├── vars/
│   └── main.yml      # Non-default variables for the role

You can remove the tests/ directory if not using it.[1][2][3]


3. Define Role Variables

Edit wordpress/vars/main.yml to specify important settings:

wp_db_name: wordpress
wp_db_user: wpuser
wp_db_password: yourpassword
wp_admin_email: [email protected]
wp_site_url: "http://example.com"
wp_install_dir: /var/www/wordpress

4. Create Role Tasks

Edit wordpress/tasks/main.yml (sample steps):

- name: Install prerequisites
  apt:
    name:
      - apache2
      - php
      - php-mysql
      - libapache2-mod-php
      - mysql-server
      - wget
      - unzip
    state: present
    update_cache: yes

- name: Download WordPress
  get_url:
    url: https://wordpress.org/latest.tar.gz
    dest: /tmp/wordpress.tar.gz

- name: Extract WordPress
  unarchive:
    src: /tmp/wordpress.tar.gz
    dest: "{{ wp_install_dir | dirname }}"
    remote_src: yes

- name: Set permissions
  file:
    path: "{{ wp_install_dir }}"
    owner: www-data
    group: www-data
    state: directory
    recurse: yes

- name: Configure wp-config.php
  template:
    src: wp-config.php.j2
    dest: "{{ wp_install_dir }}/wp-config.php"
  notify: restart apache

This installs the required packages, downloads WordPress, extracts files, sets permissions, and creates a config using a template.[4][5]


5. Create Role Handlers

Edit wordpress/handlers/main.yml to restart Apache:

- name: restart apache
  service:
    name: apache2
    state: restarted

6. Create Role Template for wp-config.php

In wordpress/templates/wp-config.php.j2, add lines like:

<?php
define('DB_NAME', '{{ wp_db_name }}');
define('DB_USER', '{{ wp_db_user }}');
define('DB_PASSWORD', '{{ wp_db_password }}');
define('DB_HOST', 'localhost');
define('WP_DEBUG', false);
?>

7. Use Your Role in a Playbook

Create your main playbook, e.g., site.yml:

- name: Install WordPress using role
  hosts: webservers
  become: yes
  roles:
    - wordpress

Your inventory file (hosts.ini):

[webservers]
10.10.10.128

8. Run Your Playbook

From the project root, run:

ansible-playbook -i hosts.ini site.yml

WordPress will be installed and configured on your webserver, leveraging a modular role-based approach.[3:1][6][5:1][4:1]


By building from scratch, each folder keeps logic clear (tasks, handlers, vars, templates) and the role is reusable for any host or group you specify.
[7][8][9][10][11][12][13]


  1. https://spacelift.io/blog/ansible-roles ↩︎

  2. https://www.pynetlabs.com/ansible-roles/ ↩︎

  3. https://www.cherryservers.com/blog/ansible-roles-tutorial ↩︎ ↩︎

  4. https://cafayate.net/es_AR/blog/tech-blog-4/automated-wordpress-installation-with-ansible-15289 ↩︎ ↩︎

  5. https://cyso.cloud/docs/cloud/extra/how-to-install-wordpress-using-ansible/ ↩︎ ↩︎

  6. https://www.makarenalabs.com/ansible-for-it-automation-wordpress-as-an-example/ ↩︎

  7. https://blog.devops.dev/wordpress-development-environment-with-ansible-c342ce467291 ↩︎

  8. https://docs.ansible.com/ansible/latest/tips_tricks/sample_setup.html ↩︎

  9. https://www.youtube.com/watch?v=svtSBkxB_wk ↩︎

  10. https://www.inmotionhosting.com/support/edu/ansible/end-to-end-guide-to-deploy-wordpress-ultrastack-using-ansible-and-git/ ↩︎

  11. image.jpg ↩︎

  12. https://www.redhat.com/en/blog/developing-ansible-role ↩︎

  13. https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html ↩︎