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
    - name: Upgrade aptitude packages
        upgrade: "full"
        update_cache: yes

    - name: Install aptitude packages
        name: "{{ item }}"
        state: present
        update_cache: yes
        cache_valid_time: 3600
        - 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
    - name: Grab Torch Repository
        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
    - name: Install luarocks dependencies
      shell: "torch/install/bin/luarocks install {{ item }}"
        # 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.