Install Mailcatcher on Ubuntu 16.04 with Ansible

I’ve been using Ansible to provision my Vagrant boxes since much of the work for provisioning can be replicated in production and staging environments. One of the tools I find essential is Mailcatcher. The following Ansible tasks handle this for me:

- name: Install Mailcatcher Dependencies
  apt: name={{item}} state=installed
  with_items:
    - build-essential
    - ruby
    - ruby-dev
    - sqlite3
    - libsqlite3-dev
- name: Install mailcatcher
  gem: name=mailcatcher state=present
- name: Create mailcatcher service file
  template:
    src: templates/mailcatcher.service.j2
    dest: /lib/systemd/system/mailcatcher.service
- name: Enable mailcatcher service
  systemd: name=mailcatcher enabled=yes state=started
- name: Update php cli settings for mailcatcher
  ini_file: dest=/etc/php/7.0/cli/php.ini create=no option=sendmail_path value='/usr/local/bin/catchmail -f test@test.com' section=sendmail
- name: Update php fpm settings for mailcatcher
  ini_file: dest=/etc/php/7.0/fpm/php.ini create=no option=sendmail_path value='/usr/local/bin/catchmail -f test@test.com' section=sendmail

In my templates folder (under my Ansible playbook root where the above exists), I have a systemd Unit called mailcatcher.service that is as follows:

[Unit]
Description=Mailcatcher
After=network.service vagrant.mount
[Service]
Type=simple
ExecStart=/usr/local/bin/mailcatcher --foreground --ip 0.0.0.0
Restart=always
[Install]
WantedBy=multi-user.target

I’d like to update this to it’s own ansible role, and I may do that soon. If I do, I will try to remember to update this post :).