This guide installs a Gatsby build environment with Node.js and serves static output with Nginx.
It supports Debian 10 to latest stable, Ubuntu LTS, and RHEL 9+ compatible systems.
- name: Install Gatsby
hosts: gatsby
become: true
vars:
gatsby_root: /opt/gatsby-site
gatsby_public_dir: /var/www/gatsby-site
tasks:
- name: Install dependencies on Debian/Ubuntu
apt:
name:
- nodejs
- npm
- nginx
- curl
- git
state: present
update_cache: true
when: ansible_os_family == "Debian"
- name: Install dependencies on RHEL family
dnf:
name:
- nodejs
- npm
- nginx
- curl
- git
state: present
when: ansible_os_family == "RedHat"
- name: Ensure Nginx is enabled and started
service:
name: nginx
state: started
enabled: true
- name: Create Gatsby project root
file:
path: "{{ gatsby_root }}"
state: directory
mode: "0755"
- name: Create Gatsby public directory
file:
path: "{{ gatsby_public_dir }}"
state: directory
mode: "0755"
- name: Install Gatsby CLI globally
npm:
name: gatsby-cli
global: true
- name: Check whether package.json exists
stat:
path: "{{ gatsby_root }}/package.json"
register: gatsby_package_json
- name: Install project dependencies if package.json exists
command: npm install
args:
chdir: "{{ gatsby_root }}"
when: gatsby_package_json.stat.exists
- name: Build Gatsby site if package.json exists
command: npm run build
args:
chdir: "{{ gatsby_root }}"
when: gatsby_package_json.stat.exists
public/.rsync from build output to web root) and Nginx site config for full production setup.We develop tailored automation solutions for:
Let’s discuss your requirements: office@linux-server-admin.com | Contact