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]
    18.206.151.166 ansible_ssh_user=ubuntu 
    
  2. File: client.my.cnf.j2
    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

    [client]
    user=root
    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": "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"
      }
    }
    
  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
     vars:
     mysql_root_password: yourpassword
     dir: /var/www/ghost
    
    • install/update required software, packages.
      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
    
    • install mysql and other dependent packages to use ansible module
      i.e.PyMySQL and some setuptools mentioned below.
     #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
    
    • 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
      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
    
    • install nodejs and ghost-cli
    - 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
    
    • create a directory for ghost and change its permission.
    #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
    
    • 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
      args:
        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
      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'
    
    • ghost is installed, do ghost start.
    - 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