Archived
1
0
This repository has been archived on 2022-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
www.ykn.fr/.ansible/run.yml

154 lines
4.0 KiB
YAML

---
- hosts: all
gather_facts: false
connection: local
tags:
- check_targets
- deploy
- clean_cache
- deploy_nginx
tasks:
- name: Vérifier qu'une limite est fournie
ansible.builtin.fail:
msg: Merci d'exécuter la commande \"ansible-playbook\" avec l'option \"--limit\"
when: play_hosts == groups.all
run_once: true
- hosts: all
gather_facts: false
connection: local
tags: check_targets
tasks:
- name: Attendre que le port SSH soit ouvert
ansible.builtin.wait_for:
host: "{{ inventory_hostname }}"
port: "{{ ssh_port }}"
search_regex: OpenSSH
delay: 3
- hosts: 127.0.0.1
gather_facts: false
connection: local
tags: install_hugo
roles:
- name: hugo
- hosts: 127.0.0.1
gather_facts: false
connection: local
tags: build
tasks:
- name: Générer les fichiers statiques
ansible.builtin.command:
cmd: >-
{{ playbook_dir }}/hugo --quiet --cleanDestinationDir
--destination '{{ playbook_dir }}/{{ app_name }}'
--baseURL '{{ app_url }}'
args:
chdir: ..
- hosts: 127.0.0.1
gather_facts: false
connection: local
tags: upload
tasks:
- name: Déterminer des faits
ansible.builtin.set_fact:
release_name: "{{ version }}-build{{ build }}"
archive_name: "{{ app_name }}.tar.gz"
vars:
build: "{{ lookup('env', 'DRONE_BUILD_NUMBER') }}"
version: "{{ lookup('pipe', 'date +%Y.%j') }}"
- name: Créer une archive des fichiers statiques
community.general.archive:
path: "{{ playbook_dir }}/{{ app_name }}"
dest: "{{ playbook_dir }}/{{ archive_name }}"
format: gz
mode: u=rw,g=r,o=r
register: make_archive
- name: Créer la version
ansible.builtin.uri:
url: "{{ gitea_api_base_url }}/repos/{{ gitea_api_repository }}/releases"
method: POST
headers:
Content-Type: application/json
body_format: json
body:
name: "{{ release_name }}"
body: Téléversé via *ansible*
tag_name: "{{ release_name }}"
draft: false
prerelease: true
status_code: 201
register: make_release
- name: Téléverser l'archive
ansible.builtin.command:
cmd: >-
curl -X 'POST'
"{{ gitea_api_base_url }}/repos/{{ gitea_api_repository }}/releases/{{ release_id }}/assets"
-H 'accept: application/json'
-H 'Content-Type: multipart/form-data'
-F 'attachment=@{{ make_archive.dest }};type=application/gzip'
vars:
release_id: "{{ make_release.json.id }}"
- name: Lister les pré-version
ansible.builtin.uri:
url: "{{ gitea_api_base_url }}/repos/{{ gitea_api_repository }}/releases?pre-release=true"
method: GET
headers:
Content-Type: application/json
register: get_releases
- name: Conserver uniquement les trois dernières pré-versions
ansible.builtin.uri:
url: "{{ gitea_api_base_url }}/repos/{{ gitea_api_repository }}/releases/{{ item.id }}"
method: DELETE
headers:
Content-Type: application/json
status_code: 204
when:
- get_releases.json | length > 0
- item | length > 0
loop_control:
label: "{{ item.name }}"
loop: "{{ get_releases.json[3:] }}"
- hosts: all
gather_facts: false
connection: local
tags: deploy_nginx
roles:
- name: nginx
- hosts: all
gather_facts: false
connection: local
tags: deploy
tasks:
- name: Déployer l'application
ansible.posix.synchronize:
src: "{{ playbook_dir }}/{{ app_name }}"
dest: /var/www
archive: true
delete: true
tags: deploy
become: true
diff: false
- hosts: all
gather_facts: false
connection: local
tags: clean_cache
tasks:
- name: Vider le cache de l'application
ansible.builtin.file:
path: /var/cache/nginx/{{ app_name }}
state: absent
become: true
diff: false