Updating a Ghost Blog with an Ansible Playbook

This site is running the Ghost Blog platform and at this time updating to a new version takes a little more than a click of a button. The steps for upgrading to a new version of ghost are here: Update Ghost

This is where Ansible comes in and turns the update process into a single command line instruction.

If you don't have ansible installed on your machine you can do so by following their guide. Install Ansible

Here is the playbook I put together to update this blog in a single command with:

$ ansible-playbook update-ghost.yml --ask-become-pass

The update-ghost.yml file contains this:

---
- hosts: dustinfisher
  remote_user: deploy
  tasks:
    - name: stop ghost service
      become: yes
      systemd:
        name: ghost
        state: stopped
    - name: start ghost service
      become: yes
      systemd:
        name: ghost
        state: started
    - name: backup current ghost blog
      command: cp -a /home/deploy/apps/ghost/. /home/deploy/apps/ghost_backup/
    - name: download latest ghost version
      get_url:
        url: https://ghost.org/zip/ghost-latest.zip
        dest: /home/deploy/downloads
    - name: unzip latest ghost release
      unarchive:
        src: /home/deploy/downloads/ghost-latest.zip
        dest: /home/deploy/downloads/ghost-latest/
        remote_src: True
    - name: delete old ghost release files
      shell: >
        cd /home/deploy/apps/ghost &&
        rm -rf core &&
        rm index.js &&
        rm PRIVACY.md &&
        rm README.md &&
        rm package.json &&
        rm npm-shrinkwrap.json
    - name: copy over new ghost release files
      shell: >
        cd /home/deploy/downloads/ghost-latest &&
        cp -a ./core /home/deploy/apps/ghost &&
        cp ./index.js /home/deploy/apps/ghost &&
        cp ./package.json /home/deploy/apps/ghost &&
        cp ./npm-shrinkwrap.json /home/deploy/apps/ghost &&
        cp ./README.md /home/deploy/apps/ghost &&
        cp ./PRIVACY.md /home/deploy/apps/ghost
    - name: update new ghost dependencies
      shell: >
        cd /home/deploy/apps/ghost &&
        rm -rf node_modules &&
        /home/deploy/.nvm/versions/node/v4.8.3/bin/npm cache clean &&
        /home/deploy/.nvm/versions/node/v4.8.3/bin/npm install --production
    - name: restart ghost service
      become: yes
      systemd:
        name: ghost
        state: restarted