Skip to content

Commit 3d2ff30

Browse files
authored
Merge pull request #50 from geotribu/tooling/add-ansible-linters
tooling(lint): add yaml and ansible linters
2 parents 4b51d14 + d376ffc commit 3d2ff30

File tree

19 files changed

+193
-81
lines changed

19 files changed

+193
-81
lines changed

.pre-commit-config.yaml

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,70 @@ repos:
77
rev: v5.0.0
88
hooks:
99
- id: check-added-large-files
10-
args: ["--maxkb=500"]
10+
args:
11+
- --maxkb=500
1112
- id: check-ast
13+
- id: check-builtin-literals
1214
- id: check-case-conflict
13-
- id: check-executables-have-shebangs
14-
- id: check-json
1515
- id: check-toml
1616
- id: check-yaml
17-
args: [--unsafe]
17+
args:
18+
- --unsafe
1819
- id: detect-private-key
1920
- id: end-of-file-fixer
2021
- id: fix-byte-order-marker
2122
- id: fix-encoding-pragma
22-
args: [--remove]
23+
args:
24+
- --remove
2325
- id: trailing-whitespace
24-
args: [--markdown-linebreak-ext=md]
26+
args:
27+
- --markdown-linebreak-ext=md
2528

26-
- repo: https://github.com/pycqa/isort
27-
rev: 6.0.1
29+
- repo: https://github.com/asottile/pyupgrade
30+
rev: v3.19.1
2831
hooks:
29-
- id: isort
30-
args: ["--profile", "black", "--filter-files"]
32+
- id: pyupgrade
33+
args:
34+
- "--py310-plus"
3135

3236
- repo: https://github.com/psf/black
3337
rev: 25.1.0
3438
hooks:
3539
- id: black
40+
args:
41+
- --target-version=py310
3642

37-
- repo: https://github.com/pycqa/flake8
38-
rev: 7.3.0
43+
- repo: https://github.com/astral-sh/ruff-pre-commit
44+
rev: "v0.11.4"
3945
hooks:
40-
- id: flake8
41-
language: python
42-
types: [python]
43-
args: ["--ignore=E501"]
46+
- id: ruff
47+
args:
48+
- --fix-only
4449

45-
# - repo: https://github.com/ansible-community/ansible-lint.git
46-
# rev: v6.8.6
47-
# hooks:
48-
# - id: ansible-lint
50+
- repo: https://github.com/pycqa/isort
51+
rev: 6.0.1
52+
hooks:
53+
- id: isort
54+
args:
55+
- --profile
56+
- black
57+
- --filter-files
58+
59+
- repo: https://github.com/adrienverge/yamllint
60+
rev: v1.37.1
61+
hooks:
62+
- id: yamllint
63+
64+
- repo: https://github.com/ansible/ansible-lint
65+
rev: v25.6.1
66+
hooks:
67+
- id: ansible-lint
68+
name: Ansible Lint
69+
description: Run yamllint with configurations in .ansible-lint file
70+
args:
71+
- -c
72+
- ./ansible/.ansible-lint.yaml
73+
- ansible
4974

5075
ci:
5176
autofix_prs: true

.yamllint.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
extends: default
2+
3+
rules:
4+
# 160 chars was the default used by old E204 rule, but
5+
# you can easily change it or disable in your .yamllint file.
6+
line-length:
7+
max: 160
8+
level: warning
9+
# Everything below is added to match ansible-lint rules
10+
# see https://ansible.readthedocs.io/projects/lint/rules/yaml/#yamllint-configuration
11+
comments:
12+
min-spaces-from-content: 1
13+
# https://github.com/adrienverge/yamllint/issues/384
14+
comments-indentation: false
15+
document-start: disable
16+
# We are adding an extra space inside braces as that's how prettier does it
17+
# and we are trying not to fight other linters.
18+
braces:
19+
min-spaces-inside: 0
20+
max-spaces-inside: 1
21+
octal-values:
22+
forbid-implicit-octal: true
23+
forbid-explicit-octal: true

ansible/.ansible-lint.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
profile: production
3+
4+
warn_list:
5+
- package-latest
6+
7+
skip_list:
8+
- var-naming[no-role-prefix]
9+
- yaml[line-length]

ansible/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ python -m pip install -U setuptools wheel
4343
python -m pip install -U -r ansible/requirements.txt
4444
```
4545

46-
Installer les rôles et collections Ansible :
46+
### Installer les rôles et collections Ansible
4747

4848
```sh
4949
ansible-galaxy install -r ansible/requirements.yml

ansible/playbook-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- name: Playbook de test
22
hosts: all
3-
gather_facts: yes
3+
gather_facts: true
44

55
tasks:
66
- name: "Ping l'hôte"

ansible/roles/backup/tasks/main.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020

2121
- name: Télécharge et ajoute la clé du dépôt dans le portefeuille
2222
ansible.builtin.shell: |
23+
set -o pipefail
2324
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
2425
sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
2526
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
2627
become: true
2728
when: not gh_ppa.stat.exists
29+
changed_when: true
2830

2931
- name: "Installe le CLI GitHub"
3032
become: true
@@ -36,7 +38,7 @@
3638
- name: MAJ les paquets installés
3739
become: true
3840
ansible.builtin.apt:
39-
update_cache: yes
41+
update_cache: true
4042
upgrade: true
4143

4244
# Script de sauvegarde
@@ -46,15 +48,19 @@
4648
state: directory
4749
owner: geotribu
4850
group: geotribu
51+
mode: "0755"
4952
become_user: geotribu
53+
become: true
5054

51-
- name: "Crée le dossier du script de sauvegarde {{ dir_backup_scripts }}cdn"
55+
- name: "Crée le dossier du script de sauvegarde CDN {{ dir_backup_scripts }}"
5256
ansible.builtin.file:
5357
path: "{{ dir_backup_scripts }}"
5458
state: directory
5559
owner: geotribu
5660
group: geotribu
61+
mode: "0755"
5762
become_user: geotribu
63+
become: true
5864

5965
- name: "Copie le script de sauvegarde du site + CDN dans {{ dir_backup_scripts }}"
6066
ansible.builtin.template:
@@ -63,6 +69,7 @@
6369
mode: "0775"
6470
backup: true
6571
become_user: geotribu
72+
become: true
6673

6774
- name: "Copie le fichier modèle de notification dans {{ dir_backup_scripts }}"
6875
ansible.builtin.copy:
@@ -71,8 +78,10 @@
7178
owner: geotribu
7279
group: geotribu
7380
# mode: "0775"
81+
mode: preserve
7482
backup: true
7583
become_user: geotribu
84+
become: true
7685

7786
# Cible de la sauvegarde
7887
- name: Crée le dossier de destination pour la sauvegarde des sources du site
@@ -82,16 +91,18 @@
8291
recurse: true
8392
owner: geotribu
8493
group: www-data
94+
mode: "0755"
8595
become: true
8696

8797
- name: Clone le dépôt des sources du site Geotribu
8898
ansible.builtin.git:
8999
repo: https://github.com/geotribu/website.git
90100
dest: "{{ dir_website_source }}"
91101
depth: 1
92-
single_branch: yes
102+
single_branch: true
93103
version: master
94104
become_user: geotribu
105+
become: true
95106

96107
- name: Configure le dépôt local - nom d'utilisateur
97108
community.general.git_config:
@@ -114,3 +125,4 @@
114125
job: "{{ dir_backup_scripts }}geotribu-backup-site-cdn.sh > {{ dir_backup_scripts }}`date +\\%Y\\%m\\%d\\%H\\%M\\%S`-cron.log 2>&1"
115126
special_time: monthly
116127
become_user: geotribu
128+
become: true

ansible/roles/cdn-indexer/files/search_indexer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from math import log as math_log
1010
from os.path import expanduser, expandvars
1111
from pathlib import Path
12-
from typing import List
1312

1413
# 3rd party
1514
import imagesize
@@ -19,7 +18,7 @@
1918
configuration_file: Path = Path(__file__).parent / "images-indexer.ini"
2019
start_folder: Path = Path.home() / "Images"
2120
extensions_to_index: tuple = (".gif", ".jpg", ".jpeg", ".png", ".svg", ".webp")
22-
images_list: List[dict] = []
21+
images_list: list[dict] = []
2322
images_dict: dict = {}
2423

2524
# -- FUNCTIONS ---------------------------------------------------------------
@@ -43,7 +42,7 @@ def convert_octets(octets: int) -> str:
4342
p = pow(1024, i)
4443
s = round(octets / p, 2)
4544

46-
return "%s %s" % (s, size_name[i])
45+
return f"{s} {size_name[i]}"
4746

4847

4948
# -- MAIN --------------------------------------------------------------------

ansible/roles/cdn-indexer/tasks/main.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,26 @@
1616
owner: geotribu
1717
group: geotribu
1818
become_user: geotribu
19+
become: true
1920

2021
- name: "Copie les fichiers liés au script dans {{ dir_cdn_indexer_scripts }}"
2122
ansible.builtin.copy:
2223
src: "{{ item }}"
2324
dest: "{{ dir_cdn_indexer_scripts }}"
2425
owner: geotribu
2526
group: geotribu
27+
mode: preserve
2628
with_items:
2729
- files/requirements.txt
2830
- files/search_indexer.py
2931
become_user: geotribu
32+
become: true
3033

3134
- name: Copie la configuration du script
3235
ansible.builtin.template:
3336
src: "templates/images-indexer.ini.j2"
3437
dest: "{{ dir_cdn_indexer_scripts }}images-indexer.ini"
38+
mode: preserve
3539
become: true
3640

3741
- name: Crée un environnement virtuel et installe les paquets Python de base
@@ -41,17 +45,20 @@
4145
- pip
4246
- setuptools
4347
- wheel
44-
state: latest
48+
args:
49+
state: present
4550
virtualenv: "{{ dir_cdn_indexer_scripts }}.venv"
4651
virtualenv_command: /usr/bin/python3 -m venv
4752
become_user: geotribu
53+
become: true
4854

4955
- name: Installe les paquets Python pour le script
5056
ansible.builtin.pip:
5157
chdir: "{{ dir_cdn_indexer_scripts }}"
5258
requirements: requirements.txt
5359
virtualenv: "{{ dir_cdn_indexer_scripts }}.venv"
5460
become_user: geotribu
61+
become: true
5562

5663
# Planification
5764
- name: Planifie l'exécution du script d'indexation des images
@@ -61,3 +68,4 @@
6168
job: "cd {{ dir_cdn_indexer_scripts }} && {{ dir_cdn_indexer_scripts }}.venv/bin/python {{ dir_cdn_indexer_scripts }}search_indexer.py > {{ dir_cdn_indexer_scripts }}`date +\\%Y\\%m\\%d\\%H\\%M\\%S`-cron.log 2>&1"
6269
special_time: hourly
6370
become_user: geotribu
71+
become: true

ansible/roles/cdn.geotribu.fr/tasks/main.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
- php8.1-iconv
3030
- php8.1-mbstring
3131
- php8.1-zip
32-
update_cache: yes
32+
update_cache: true
3333

3434
# Apache modules
3535
- name: Désactive les modules Apache
@@ -58,19 +58,21 @@
5858

5959
- name: Active la configuration pour php8 FPM
6060
become: true
61-
command: >
61+
ansible.builtin.command: >
6262
a2enconf php8.1-fpm
6363
args:
6464
creates: "/etc/apache2/conf-enabled/php8.1-fpm.conf"
6565
when: item.state is not defined or item.state != 'absent'
6666
notify: Restart Apache
67+
changed_when: true
6768

6869
# Installe TinyFileManager
6970
- name: Crée le dossier de destination
7071
become: true
71-
file:
72+
ansible.builtin.file:
7273
path: "/var/www/{{ geotribu_www }}/{{ cdn_subdomain }}/images/"
7374
state: directory
75+
mode: "0755"
7476

7577
- name: Copie les fichiers de TinyFileManager
7678
become: true
@@ -91,19 +93,22 @@
9193
ansible.builtin.template:
9294
src: "templates/apache_cdn.conf.j2"
9395
dest: "/etc/apache2/sites-available/{{ geotribu_www }}-{{ cdn_subdomain }}.conf"
96+
mode: preserve
9497

9598
- name: Active le site
9699
become: true
97100
ansible.builtin.command: "/usr/sbin/a2ensite {{ geotribu_www }}-{{ cdn_subdomain }}"
98101
# notify: Restart Apache
102+
changed_when: true
99103

100104
- name: Désactive le site par défaut d'Apache
101105
become: true
102106
ansible.builtin.command: "/usr/sbin/a2dissite 000-default.conf"
103107
notify: Restart Apache
108+
changed_when: true
104109

105110
- name: Crée un certificat SSL pour {{ cdn_subdomain }}
106-
shell: >-
111+
ansible.builtin.command: >-
107112
certbot --apache
108113
--noninteractive
109114
-d {{ cdn_subdomain }}.{{ hostname }}
@@ -112,3 +117,4 @@
112117
become: true
113118
register: cert_created
114119
notify: Reload Apache
120+
changed_when: true

0 commit comments

Comments
 (0)