Using Drush (Drupal) with Ansible, new role

Jan 15, 2016 min read

Drush is a good solution to automatise installation and configuration in Drupal from the command line, if we combine It with Ansible It makes Drupal environment replication a charm.

I needed a role to manage Drupal modules and themes, and I haven’t found something like It, so I made a new role, javaguirre.drupal_deps.

There are some roles regarding installation of Drupal and Drush (geerlingguy’s), others providing everything in the same role (vlad).

I prefer to have smaller roles usually controlling a package application or service than having an all-in-one role. In my opinion roles should manage only one service/application so It’s easy to exchange or combine with others, easier to maintain too.

That’s how I made the role, It manages and installs modules and themes through Drush, It also sets the default theme, The role is in Github. :-)

You can install It using ansible-galaxy:

ansible-galaxy install javaguirre.drupal_deps

or you could create the requirements.yml file and install It using git if you like:

# requirements.yml

---

- name: javaguirre.drupal_deps
  src: git+https://github.com/javaguirre/ansible-drupal-dependencies-role.git

and run ansible-galaxy with the -r option:

ansible-galaxy install -r requirements.yml

The role defines for now these variables:

---

drush_bin_path: "/usr/local/bin/drush"
drupal_path: "/var/www/drupal"
drupal_user: apache
drupal_core_modules: []
drupal_themes: []
drupal_modules: []
drupal_other_modules: []
drupal_default_theme: bartik

A playbook example on how to use It would be like this:

- hosts: all
  remote_user: vagrant
  become: no

  vars:
    drush_bin_path: "/usr/bin/drush"
    drupal_user: http
    drupal_core_modules:
      - contact

    drupal_themes:
      - bootstrap

    drupal_modules:
      - ctools
      - entity
      - ldap

    drupal_other_modules:
      - simpletest

    drupal_default_theme: bootstrap

  role:
    - javaguirre.drupal_deps

This playbook will enable the core module contact if It’s not enabled, It will install the bootstrap theme and ctools, entity and ldap modules. It will install the simpletest module on /modules/ (the installation path is different, that’s why It’s a different module list) and will enable the bootstrap theme as the default for the current Drupal installation.

In the following days I will work on fixing the tests in Travis CI and adding some tasks regarding the cache or modules and themes uninstall.

If you have more suggestions, please, let me know!