A Torch-Installing Ansible Playbook

It's surprisingly difficult to wrap Ansible provisioning around Andrej Karpathy's very instructive and useful char-rnn package. Ansible has its quirks, which doesn't make everything straightforward. What's more, Torch has made a point of making it easy for researchers to get started with Lua (an amazing scriptable wrapper of C), and their installation scripts consequently assume an interactive environment rather than a production-ready system-wide one. Though there are Docker solutions available, I wanted to see how things would play in the Vagrant/Ansible universe.

Happily, it's not too difficult to guess-test-revise your way into a fragile but workable Ansible playbook. So with a quick cat !$ | pbcopy, here's what I came up with:

---
# Provision a box to do the computation.
#
# This is meant to be useful for local Vagrant virtual machines as well as for
# spot instances in the AWS cloud, for instance.

- name: Provision the box
  hosts: all
  become: yes
  tasks:
    - name: Upgrade aptitude packages
      apt:
        upgrade: "full"
        update_cache: yes

    - name: Install aptitude packages
      apt:
        name: "{{ item }}"
        state: present
        update_cache: yes
        cache_valid_time: 3600
      with_items:
        - git # Needed to download Torch in the first place.
        - libssl-dev # For luacrypto dependencies.

# Torch automates installation in a straightforward way; might as well use it.
# This is straight from http://torch.ch/docs/getting-started.html
- name: Install torch within the user account
  hosts: all
  tasks:
    - name: Grab Torch Repository
      git:
        repo: "https://github.com/torch/distro.git"
        dest: "~/torch"
        recursive: yes
        force: yes
        accept_hostkey: yes

    - name: Install Torch Dependencies
      shell: "cd ~/torch; bash install-deps"

    - name: Install Torch with default options
      shell: "cd ~/torch; yes | ./install.sh"

# Install the LuaRocks dependencies for char-rnn. This command's path is
# a bit fragile, depending on changes of torch's installer.
- name: Install project dependencies
  hosts: all
  tasks:
    - name: Install luarocks dependencies
      shell: "torch/install/bin/luarocks install {{ item }}"
      with_items:
        # Listed in https://github.com/karpathy/char-rnn README document.
        - nngraph
        - optim
        - nn

While I'm working toward a series of musical experiments using this as a launching-point, hopefully others will find this to be helpful.