[INFO] Création du rôle (#1)

This commit is contained in:
pulsar89.5 2022-11-09 14:54:18 +00:00
parent a0a3d6f1d9
commit 74f9b1b429
9 changed files with 304 additions and 1 deletions

101
README.md
View File

@ -1,3 +1,102 @@
# role_hugo
Rôle d'installation de gohugo.io
Rôle de déploiement de gohugo.io
## Fonctionnement
Ce rôle prends en charge le serveur intégré ou l'utilisation de nginx pour servir le site.<br />
Afin de ne pas utiliser le serveur intégré, mettre la variable `hugo_nginx_enabled` à `true`.
## Variables
### hugo_os
Nécessaire pour télécharger le bon paquet depuis github.com.
<span style="text-decoration: underline">Valeur par défaut:</span> `linux`
### hugo_architecture
Nécessaire pour télécharger le bon paquet depuis github.com.<br />
Les architectures disponbiles sont *amd64* et *arm64*.
<span style="text-decoration: underline">Valeur par défaut:</span> `arm64`
### hugo_latest_url
URL pointant vers l'API de github.com fournissant le lien de téléchargement du paquet.
<span style="text-decoration: underline">Valeur par défaut:</span> `https://api.github.com/repos/gohugoio/hugo/releases/latest`
### hugo_version
Version qui sera installée.
<span style="text-decoration: underline">Valeur par défaut:</span> `latest`
### hugo_installation_type
Type d'installation souhaitée (`deb` ou `archive`).
<span style="text-decoration: underline">Valeur par défaut:</span> `archive`
### hugo_binary_path
Emplacement où sera déployer le binaire.
<span style="text-decoration: underline">Valeur par défaut:</span> `/opt/hugo`
### hugo_install_command
Ajouter la commande `hugo` dans le PATH.
<span style="text-decoration: underline">Valeur par défaut:</span> `false`
### hugo_data_path
Emplacement où seront stockées les données.
<span style="text-decoration: underline">Valeur par défaut:</span> `/srv/hugo`
### hugo_timer_enabled
Activer la génération automatique régulière des fichiers statiques.
<span style="text-decoration: underline">Valeur par défaut:</span> `true`
### hugo_base_url
URL utilisée dans la génération des fichiers statiques.
<span style="text-decoration: underline">Valeur par défaut:</span> aucune
### hugo_server_listen
Adresse IP d'écoute du serveur intégré.
<span style="text-decoration: underline">Valeur par défaut:</span> `0.0.0.0`
### hugo_server_port
Port d'écoute du serveur intégré.
<span style="text-decoration: underline">Valeur par défaut:</span> `8080`
### hugo_timer_enabled
Activer la génération automatique tous les jours (`daily`).
<span style="text-decoration: underline">Valeur par défaut:</span> `true`
### hugo_nginx_enabled
Ne pas utiliser le serveur intégré pour servir les fichiers statiques.
<span style="text-decoration: underline">Valeur par défaut:</span> `false`
### hugo_nginx_docroot
Emplacement où les fichiers statiques seront générés pour nginx.
<span style="text-decoration: underline">Valeur par défaut:</span> `/var/www/hugo`

20
defaults/main.yml Normal file
View File

@ -0,0 +1,20 @@
---
# defaults file for hugo
hugo_os: linux
hugo_architecture: arm64
hugo_latest_url: https://api.github.com/repos/gohugoio/hugo/releases/latest
hugo_version: latest
hugo_binary_path: /opt/hugo
hugo_install_command: false
hugo_data_path: /srv/hugo
hugo_base_url: ""
hugo_server_listen: 0.0.0.0
hugo_server_port: 8080
hugo_timer_enabled: true
hugo_nginx_enabled: false
hugo_nginx_docroot: /var/www/hugo

23
handlers/main.yml Normal file
View File

@ -0,0 +1,23 @@
---
# handlers file for hugo
- name: Installer la commande
ansible.builtin.command:
cmd: install -t /usr/local/bin /opt/hugo/hugo
become: true
- name: Redémarrer le service
ansible.builtin.systemd:
enabled: false
daemon_reload: true
state: restarted
name: hugo.service
become: true
- name: Activer et exécuter la planification
ansible.builtin.systemd:
enabled: "{{ hugo_timer_enabled }}"
daemon_reload: true
state: started
name: hugo.timer
become: true

19
meta/main.yml Normal file
View File

@ -0,0 +1,19 @@
galaxy_info:
author: pulsar89.5
description: Rôle de déploiement de gohugo.io
license: GPL-3.0-or-later
min_ansible_version: 2.1
platforms:
- name: Debian (LXD)
versions:
- 11
galaxy_tags:
- gohugo.io
- hugo
- web
dependencies: []

48
tasks/configuration.yml Normal file
View File

@ -0,0 +1,48 @@
---
# tasks file for hugo
- name: Récupérer les informations sur le dossier des données
ansible.builtin.stat:
path: "{{ hugo_data_path }}"
become: true
register: data_path_infos
- name: Créer l'emplacement des données
ansible.builtin.file:
state: directory
path: "{{ hugo_data_path }}"
owner: root
group: root
mode: u=rw,g=r,o=
when: not data_path_infos.stat.exists
become: true
- name: Déployer le service
ansible.builtin.template:
owner: root
group: root
mode: u=rw,g=r,o=r
src: hugo.service.j2
dest: /etc/systemd/system/hugo.service
become: true
notify: Redémarrer le service
- name: Activer le service
ansible.builtin.systemd:
enabled: true
daemon_reload: true
state: restarted
name: hugo.service
when: hugo_timer_enabled
become: true
- name: Déployer la planification
ansible.builtin.template:
owner: root
group: root
mode: u=rw,g=r,o=r
src: hugo.timer.j2
dest: /etc/systemd/system/hugo.timer
when: hugo_timer_enabled
become: true
notify: Activer et exécuter la planification

49
tasks/installation.yml Normal file
View File

@ -0,0 +1,49 @@
---
# tasks file for hugo
- name: Créer l'emplacement de stockage du binaire
ansible.builtin.file:
state: directory
path: "{{ hugo_binary_path }}"
mode: u=rwX,g=rX,o=rX
become: true
- name: Modifier l'URL pour pointer vers la version souhaitée
ansible.builtin.set_fact:
hugo_latest_url: "{{ hugo_latest_url | replace('latest', version) }}"
when: hugo_version != "latest"
vars:
version: "tags/v{{ hugo_version }}"
- name: Récupérer le contenu du fichier de version
ansible.builtin.uri:
url: "{{ hugo_latest_url }}"
return_content: true
follow_redirects: all
register: releases
- name: Extraire l'URL du paquet
ansible.builtin.set_fact:
browser_download_url: >
{{
releases.json.assets |
selectattr('browser_download_url', 'search', '.tar.gz') |
selectattr('browser_download_url', 'search', hugo_os) |
selectattr('browser_download_url', 'search', hugo_architecture) |
map(attribute='browser_download_url')
}}
- name: Extraire l'archive
ansible.builtin.unarchive:
src: "{{ browser_download_url | first }}"
dest: "{{ hugo_binary_path }}"
remote_src: true
list_files: true
mode: u=rwX,g=rX,o=rX
become: true
- name: Installer la commande
ansible.builtin.command:
cmd: install -t /usr/local/bin /opt/hugo/hugo
when: hugo_install_command
become: true

9
tasks/main.yml Normal file
View File

@ -0,0 +1,9 @@
---
# tasks file for hugo
- name: Importer les tâches d'installation
ansible.builtin.import_tasks: installation.yml
- name: Importer les tâches de configuration
ansible.builtin.import_tasks: configuration.yml
when: hugo_install_command

24
templates/hugo.service.j2 Normal file
View File

@ -0,0 +1,24 @@
# {{ ansible_managed }}
[Unit]
Description=HuGO project {{ hugo_base_url }}
After=syslog.target
After=network.target
ConditionFileNotEmpty={{ hugo_data_path }}/config.yaml
[Service]
User=root
WorkingDirectory={{ hugo_data_path }}
{% if hugo_nginx_enabled %}
Type=oneshot
ExecStart=/usr/local/bin/hugo --cleanDestinationDir --baseURL {{ hugo_base_url }}
ExecStartPost=/usr/bin/rsync --delete-after --recursive --partial {{ hugo_data_path }}/public/ {{ hugo_nginx_docroot }}/
{% else %}
Type=simple
ExecStart=/usr/local/bin/hugo server --cleanDestinationDir --appendPort=false --bind={{ hugo_server_listen }} --port={{ hugo_server_port }} --baseURL={{ hugo_base_url }}
Restart=always
RestartSec=2s
{% endif %}
[Install]
WantedBy=multi-user.target

12
templates/hugo.timer.j2 Normal file
View File

@ -0,0 +1,12 @@
# {{ ansible_managed }}
[Unit]
Description=HuGO to reload {{ hugo_base_url }}
ConditionFileNotEmpty={{ hugo_data_path }}/config.yaml
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target