This guide installs a Hugo build environment and Nginx to serve generated static files.
It supports Debian 10 to latest stable, Ubuntu LTS, and RHEL 9+ compatible systems.
- name: Install Hugo
hosts: hugo
become: true
vars:
hugo_version: "0.158.0"
hugo_root: /opt/hugo-site
hugo_public_root: /var/www/hugo-site
tasks:
- name: Install dependencies on Debian/Ubuntu
apt:
name:
- curl
- git
- nginx
state: present
update_cache: true
when: ansible_os_family == "Debian"
- name: Install dependencies on RHEL family
dnf:
name:
- curl
- git
- nginx
state: present
when: ansible_os_family == "RedHat"
- name: Ensure Nginx is enabled and started
service:
name: nginx
state: started
enabled: true
- name: Download Hugo binary
get_url:
url: "https://github.com/gohugoio/hugo/releases/download/v{{ hugo_version }}/hugo_extended_{{ hugo_version }}_Linux-64bit.tar.gz"
dest: /tmp/hugo.tar.gz
mode: "0644"
- name: Extract Hugo
unarchive:
src: /tmp/hugo.tar.gz
dest: /usr/local/bin
remote_src: true
- name: Verify Hugo installation
command: hugo version
register: hugo_version_check
changed_when: false
- name: Create Hugo project root
file:
path: "{{ hugo_root }}"
state: directory
mode: "0755"
- name: Create Hugo public root
file:
path: "{{ hugo_public_root }}"
state: directory
mode: "0755"
- name: Check whether config file exists
stat:
path: "{{ hugo_root }}/hugo.toml"
register: hugo_config
- name: Build Hugo site when config exists
command: hugo --minify
args:
chdir: "{{ hugo_root }}"
when: hugo_config.stat.exists
- name: Copy public folder to web root
synchronize:
src: "{{ hugo_root }}/public/"
dest: "{{ hugo_public_root }}/"
when: hugo_config.stat.exists
public/ to your web root.If you need to build Hugo from source or develop themes:
- name: Install Go
apt:
name: golang-go
state: present
when: ansible_os_family == "Debian"
To add a theme via git submodule:
- name: Initialize git repo
command: git init
args:
chdir: "{{ hugo_root }}"
when: hugo_config.stat.exists
- name: Add theme as submodule
command: git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke.git themes/ananke
args:
chdir: "{{ hugo_root }}"
when: hugo_config.stat.exists
Beyond this playbook, we offer:
Contact our automation team: office@linux-server-admin.com