Now that we have our LAMP stack we want to use it for something.  The first thing we think of is to get our web files in there ready for launching web pages.  For this purpose I was not able to find a ready made script online so this time had to put my head down and learn to write ansible yaml files myself.  I did find a helpful post here, and used some of that for a starting point.

I created a new Ansible Playbook to do the following:

  • Install ssh connection with bitbucket, where I have my git repos
  • Create necessary folders and clone my three repos
  • Create databases for all the websites
The key part of the clone is the ansible git module which can be used as follows:
- name: clone root git repo
  git:
    repo: 'git@bitbucket.org:Your/repo.git'
    dest: '/remote/path/to/repo'
    update: no
    accept_hostkey: yes
    version: master

The greatest problem is getting the ssh keys correct.  These are installed as follows:

  - name: Create the SSH directory.
    file: state=directory path=/root/.ssh/
  - name: Upload SSH known hosts.
    template: src=known_hosts dest=/root/.ssh/known_hosts mode=0600
  - name: Upload SSH private key.
    template: src=id_rsa dest=/root/.ssh/id_rsa mode=0600
  - name: Upload SSH public key.
    template: src=id_rsa.pub dest=/root/.ssh/id_rsa.pub

This configuration fits with the ssh configuration we did earlier.  When ansible fails on a clone it just hangs.  There are no error messages or anything.  Adding -vvvv to the command line helps as in:

ansible-playbook -i ansible_lamp/hosts sites/site.yml -vvvv

That gives extended printout, however when ansible hangs it just tells us that we are making the connection.  To understand why this might be happening, try cloning the repo on the server the old fashioned (manual) way.  If you get a permissions error then you'll know you haven't uploaded the right ssh keys.

Because we might be running this script repeatedly we can add a condition to this item to only clone when the repo is absent, because as we know git will fail if repo exists:

name: set repo directory
  stat: path=/remote/path/to/repo
  register: myrepo

- name: clone root git repo
  git:
    repo: 'git@bitbucket.org:Your/repo.git'
    dest: '/remote/path/to/repo'
    update: no
    accept_hostkey: yes
    version: master
when: myrepo.stat.isdir is not defined

Which uses the stat module to provide us with some details about the repo directory.
As part of this playbook I also installed all my databases:

  - name: Create databases.
    mysql_db: name={{ item }} state=present
    with_items: "{{ databases }}"
    - name: Copy database data to remote server
    template: src={{ item }}.sql dest=/tmp/{{ item }}.sql
    with_items: "{{ databases }}"
  - name: Import database data
    mysql_db: name={{ item }} state=import target=/tmp/{{ item }}.sql login_password={{ password }} login_host=127.0.0.1
    with_items: "{{ databases }}"

Using a list of databases stored in a variable called databases and .sql template files exported from the old database.  This is one task that you do not want to repeat after the new site has gone live because of database changes that will occur.

Next our plan is to install all the necessary configurations to make all this stuff work!