Install and Setup Ghostjs with Ansible

Ansible is fun but need to be intuitive too !

Objective: Install and setup Ghostjs using ansible-playbook

What does the code do:

    • calling host file
    • setup variables
    • install/update required software, packages.
    • install mysql and other dependent packages to use ansible modulei.e.PyMySQL and some setuptools mentioned below.
    • Set root user password.firstly, if my.cnf file exists, mysql-root-password updateelse, replace my.cnf file with the jinja template created before.
    • install nodejs and ghost-cli
    • create a directory for ghost and change its permission.
    • Run ghost installinteractive comments are generated for active inputs from user tosetup ghost config file i.e. config.production.jsonsilent the errors and let run ghost install process.proceed to next step.
    • Now manually set up config.production.jsoncreate a file, add the contents and set the permission.
    • ghost is installed, do ghost start.

File: ghostinstall.ymlAnsible playbook to install/update required package, software and setupthe environment to run ghostjs.

- hosts: all
  become: true #become sudo globally

 vars:
 mysql_root_password: yourpassword
 dir: /var/www/ghost

  tasks:
#upgrades
- name: Update all packages to the latest version
  apt:
    upgrade: dist
# Nginx
- name: Nginx | install
  apt:
    name: nginx
    state: latest
    update_cache: yes

 #mysql
- name: Install mysql-server
  apt:
    name: mysql-server
    state: present
#PyMySQL
- name: pymsqli install
  apt:
    name: python-pip
    state: present
- name: install setuptools
  apt:
    name: '{{ packages }}'
    state: present
  vars:
    packages:
      - python-setuptools
      - python-dev
      - build-essential
- name: pip
  pip:
    name: PyMySQL

  - name: Set root user password
  mysql_user:
    name: root
    password: "{{ mysql_root_password}}"
    check_implicit_admin: true
- name: Create .my.cnf
  template:
    src: "client.my.cnf.j2"
    dest: "/root/.my.cnf"
    owner: root
    group: root
    mode: 0600

- name: Install nodejs
  shell: curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -; sudo apt-get install -y nodejs
#ghostcli
- name: Install ghost-cli
  shell: npm i -g ghost-cli

#ghost install process
- name: Create ghost directory
  shell: mkdir -p '{{ dir }}'
- name: Changes permissions
  file:
    path: '{{ dir }}'
    state: directory
    owner: ubuntu
    group: ubuntu
    mode:  '775'
    recurse: yes

- name: finally installing ghostjs
  become: false
  ignore_errors: true
  no_log: true
  shell:  ghost install
  args:
    chdir: '{{ dir }}'

# ghost config
- name: create json file
  file:
    path: "/var/www/ghost/config.production.json"
    state: touch
- name:  config.production.json - contents
  copy:
   src: "config.production.json"
   dest: "/var/www/ghost/config.production.json"
- name: set file permissions
  file:
   path: "/var/www/ghost/config.production.json"
   state: file
   owner: ubuntu
   group: ubuntu
   mode: '664'

- name: setup systemd
  become: false
  shell: ghost setup systemd
  args:
   chdir: '{{ dir }}'
  tags:
    - system
# ghost start
- name: ghost start
  shell: ghost start
  args:
    chdir: '{{ dir }}'

Link to my github repository

File: config.production.jsonIt is ghostjs config file, created upon running ghost setup.Instead of running setup and configuring manually, json file is createdwith required details.This file is added directly at destination using ansible.

{
"url": " Blog URL ",
  "server": {
    "port": 2368,
    "host": "127.0.0.1"
  },
  "database": {
    "client": "mysql",
    "connection": {
      "host": "localhost",
      "user": "root",
      "database": "ghost_prod",
      "password": "your password"
    }
  },
  "mail": {
    "transport": "Direct"
  },
  "logging": {
    "transports": [
      "file",
      "stdout"
    ]
  },
  "process": "systemd",
  "paths": {
    "contentPath": "/var/www/ghost/content"
  },
  "bootstrap-socket": {
    "port": 8000,
    "host": "localhost"
  }
}

File: client.my.cnf.j2jinja template concept used..that's what the extension ".j2"A simple text file, varibles defined here is replaced when the templateis rendered.Here,using to replace my.cnf at destination with provided password.Variables are defined in .yml fileMake sure no spaces are present it took me a day to figure out the error

[client]
user=root
password={{ mysql_root_password }}

File: hostslist out server's IP to run ansible playbook

[all]
18.206.151.166 ansible_ssh_user=ubuntu