Ansible is fun but need to be intuitive too !

Objective: Install and setup Ghostjs using ansible-playbook
What does the code do:

  1. File: hosts
    list out server's IP to run ansible playbook

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

    password={{ mysql_root_password }}
  3. File: config.production.json
    It is ghostjs config file, created upon running ghost setup.
    Instead of running setup and configuring manually, json file is created
    with required details.
    This file is added directly at destination using ansible.

    "url": " Blog URL ",
      "server": {
        "port": 2368,
        "host": ""
      "database": {
        "client": "mysql",
        "connection": {
          "host": "localhost",
          "user": "root",
          "database": "ghost_prod",
          "password": "your password"
      "mail": {
        "transport": "Direct"
      "logging": {
        "transports": [
      "process": "systemd",
      "paths": {
        "contentPath": "/var/www/ghost/content"
      "bootstrap-socket": {
        "port": 8000,
        "host": "localhost"
  4. File: ghostinstall.yml
    Ansible playbook to install/update required package, software and setup
    the environment to run ghostjs.

    • calling host file
    - hosts: all 
      become: true #become sudo globally
    • setup variables
     mysql_root_password: yourpassword
     dir: /var/www/ghost
    • install/update required software, packages.
    - name: Update all packages to the latest version
        upgrade: dist
    # Nginx
    - name: Nginx | install
        name: nginx
        state: latest
        update_cache: yes
    • install mysql and other dependent packages to use ansible module
      i.e.PyMySQL and some setuptools mentioned below.
    - name: Install mysql-server
        name: mysql-server
        state: present
    - name: pymsqli install
        name: python-pip
        state: present
    - name: install setuptools
        name: '{{ packages }}'
        state: present
          - python-setuptools
          - python-dev
          - build-essential
    - name: pip
        name: PyMySQL
    • Set root user password.
      firstly, if my.cnf file exists, mysql-root-password update
      else, replace my.cnf file with the jinja template created before.
      - name: Set root user password
        name: root
        password: "{{ mysql_root_password}}"
        check_implicit_admin: true
    - name: Create .my.cnf
        src: ""
        dest: "/root/.my.cnf"
        owner: root
        group: root
        mode: 0600
    • install nodejs and ghost-cli
    - name: Install nodejs
      shell: curl -sL | sudo -E bash -; sudo apt-get install -y nodejs
    - name: Install ghost-cli
      shell: npm i -g ghost-cli
    • create a directory for ghost and change its permission.
    #ghost install process
    - name: Create ghost directory
      shell: mkdir -p '{{ dir }}'
    - name: Changes permissions
        path: '{{ dir }}'
        state: directory
        owner: ubuntu
        group: ubuntu
        mode:  '775'
        recurse: yes
    • Run ghost install
      interactive comments are generated for active inputs from user to
      setup ghost config file i.e. config.production.json
      silent the errors and let run ghost install process.
      proceed to next step.
    - name: finally installing ghostjs
      become: false
      ignore_errors: true
      no_log: true
      shell:  ghost install
        chdir: '{{ dir }}'
    • Now manually set up config.production.json
      create a file, add the contents and set the permission.
    # ghost config
    - name: create json file
        path: "/var/www/ghost/config.production.json"
        state: touch
    - name:  config.production.json - contents
       src: "config.production.json"
       dest: "/var/www/ghost/config.production.json"
    - name: set file permissions
       path: "/var/www/ghost/config.production.json"
       state: file
       owner: ubuntu
       group: ubuntu
       mode: '664'
    • ghost is installed, do ghost start.
    - name: setup systemd
      become: false
      shell: ghost setup systemd
       chdir: '{{ dir }}'
        - system
    # ghost start
    - name: ghost start
      shell: ghost start
        chdir: '{{ dir }}'

    Link to my github repository