first commit

This commit is contained in:
2025-12-04 09:04:42 -05:00
commit 21bee026ff
29 changed files with 14170 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
[defaults]
inventory = ./inventory/hosts.yml
host_key_checking = False
retry_files_enabled = False
stdout_callback = default
bin_ansible_callbacks = True
[callback_default]
result_format = yaml
@@ -0,0 +1,4 @@
ansible_port: 22
ansible_user: automation
ansible_ssh_private_key_file: /app/ssh_keys/id_automation_ansible
+4
View File
@@ -0,0 +1,4 @@
ansible_port: 22
ansible_user: automation
ansible_ssh_private_key_file: /app/ssh_keys/id_automation_ansible
ansible_python_interpreter: /usr/bin/python3
@@ -0,0 +1,4 @@
ansible_port: 22
ansible_user: automation
ansible_ssh_private_key_file: /app/ssh_keys/id_automation_ansible
ansible_python_interpreter: /usr/bin/python3
@@ -0,0 +1,4 @@
ansible_port: 22
ansible_user: automation
ansible_ssh_private_key_file: /app/ssh_keys/id_automation_ansible
ansible_python_interpreter: /usr/bin/python3
@@ -0,0 +1,4 @@
ansible_port: 22
ansible_user: automation
ansible_ssh_private_key_file: /app/ssh_keys/id_automation_ansible
ansible_python_interpreter: /usr/bin/python3
@@ -0,0 +1 @@
ansible_python_interpreter: /usr/local/bin/python3
+52
View File
@@ -0,0 +1,52 @@
all:
children:
env_homelab:
hosts:
ali2v.xeon.home: null
hp.nas.home: null
hp2.i7.home: null
hp3.i5.home: null
mimi.pc.home: null
orangepi.pc.home: null
raspi.4gb.home: null
raspi.8gb.home: null
env_lab:
hosts:
media.labb.home: null
dev.lab.home: null
env_prod:
hosts:
hp.truenas.home: null
ali2v.truenas.home: null
jump.point.home: null
automate.prod.home: null
dev.prod.home: null
role_proxmox:
hosts:
ali2v.xeon.home: null
hp.nas.home: null
hp2.i7.home: null
hp3.i5.home: null
mimi.pc.home: null
role_lab_servers:
hosts:
media.labb.home: null
dev.lab.home: null
role_truenas:
hosts:
hp.truenas.home: null
ali2v.truenas.home:
ansible_python_interpreter: /usr/bin/python3
role_prod_servers:
hosts:
jump.point.home: null
automate.prod.home: null
dev.prod.home: null
role_sbc:
hosts:
orangepi.pc.home: null
raspi.4gb.home: null
raspi.8gb.home: null
role_docker:
hosts:
dev.lab.home: null
+55
View File
@@ -0,0 +1,55 @@
all:
children:
env_homelab:
hosts:
ali2v.xeon.home: null
hp.nas.home: null
hp2.i7.home: null
hp3.i5.home: null
mimi.pc.home: null
orangepi.pc.home: null
raspi.4gb.home: null
raspi.8gb.home: null
env_lab:
hosts:
media.labb.home: null
toto:
ansible_host: toto.home
dev.lab.home: null
env_prod:
hosts:
hp.truenas.home: null
ali2v.truenas.home: null
jump.point.home: null
automate.prod.home: null
dev.prod.home: null
role_proxmox:
hosts:
ali2v.xeon.home: null
hp.nas.home: null
hp2.i7.home: null
hp3.i5.home: null
mimi.pc.home: null
role_lab_servers:
hosts:
media.labb.home: null
toto: null
dev.lab.home: null
role_truenas:
hosts:
hp.truenas.home: null
ali2v.truenas.home:
ansible_python_interpreter: /usr/bin/python3
role_prod_servers:
hosts:
jump.point.home: null
automate.prod.home: null
dev.prod.home: null
role_sbc:
hosts:
orangepi.pc.home: null
raspi.4gb.home: null
raspi.8gb.home: null
role_docker:
hosts:
dev.lab.home: null
+47
View File
@@ -0,0 +1,47 @@
---
- name: Backup configuration files
hosts: all
become: true
gather_facts: true
vars:
category: backup
subcategory: configuration
backup_dir: /tmp/config_backup
timestamp: "{{ ansible_date_time.iso8601_basic_short }}"
tasks:
- name: Create backup directory
ansible.builtin.file:
path: "{{ backup_dir }}"
state: directory
mode: '0755'
- name: Backup /etc directory (essential configs)
ansible.builtin.archive:
path:
- /etc/hostname
- /etc/hosts
- /etc/passwd
- /etc/group
- /etc/shadow
- /etc/sudoers
- /etc/ssh/sshd_config
dest: "{{ backup_dir }}/etc_backup_{{ timestamp }}.tar.gz"
format: gz
ignore_errors: true
- name: Backup crontabs
ansible.builtin.shell: |
crontab -l > {{ backup_dir }}/crontab_{{ timestamp }}.txt 2>/dev/null || echo "No crontab"
changed_when: false
- name: List backup files
ansible.builtin.find:
paths: "{{ backup_dir }}"
patterns: "*{{ timestamp }}*"
register: backup_files
- name: Display backup summary
ansible.builtin.debug:
msg: |
Backup completed for {{ inventory_hostname }}
Files created: {{ backup_files.files | map(attribute='path') | list }}
+229
View File
@@ -0,0 +1,229 @@
---
- name: Bootstrap host for Ansible automation
hosts: all
become: true
gather_facts: false
vars:
category: maintenance
subcategory: bootstrap
automation_user: "{{ lookup('env', 'SSH_USER') | default('automation', true) }}"
ssh_public_key_path: "/app/ssh_keys/id_rsa.pub"
tasks:
- name: Detect OS type
raw: |
if command -v apk >/dev/null 2>&1; then
echo "alpine"
elif command -v pkg >/dev/null 2>&1 && [ -f /etc/freebsd-update.conf ]; then
echo "freebsd"
elif [ -f /etc/debian_version ]; then
echo "debian"
elif [ -f /etc/redhat-release ]; then
echo "redhat"
else
echo "unknown"
fi
register: os_type_raw
changed_when: false
- name: Detect OS variant (Armbian, Raspbian, etc.)
raw: |
if [ -f /etc/armbian-release ]; then
echo "armbian"
elif [ -f /etc/rpi-issue ] || grep -qi "raspberry" /proc/cpuinfo 2>/dev/null; then
echo "raspbian"
else
echo "standard"
fi
register: os_variant_raw
changed_when: false
- name: Set OS type fact
set_fact:
os_type: "{{ os_type_raw.stdout | trim }}"
os_variant: "{{ os_variant_raw.stdout | trim }}"
- name: Display detected OS
debug:
msg: "[1/7] OS détecté: {{ os_type }} ({{ os_variant }})"
- name: Check if automation user exists
raw: id {{ automation_user }} 2>/dev/null && echo "exists" || echo "not_exists"
register: user_check
changed_when: false
- name: Display user status
debug:
msg: "[2/7] Utilisateur {{ automation_user }}: {{ 'existant' if 'exists' in user_check.stdout else 'à créer' }}"
- name: Create automation user (Debian/Ubuntu)
raw: useradd -m -s /bin/bash {{ automation_user }} || useradd -m -s /bin/sh {{ automation_user }}
when:
- "'not_exists' in user_check.stdout"
- os_type == "debian"
- name: Create automation user (Alpine)
raw: adduser -D {{ automation_user }}
when:
- "'not_exists' in user_check.stdout"
- os_type == "alpine"
- name: Create automation user (FreeBSD)
raw: pw useradd {{ automation_user }} -m -s /bin/sh
when:
- "'not_exists' in user_check.stdout"
- os_type == "freebsd"
- name: Display user creation result
debug:
msg: "[3/7] Utilisateur {{ automation_user }} configuré"
- name: Unlock user account
raw: |
if command -v passwd >/dev/null 2>&1; then
passwd -u {{ automation_user }} 2>/dev/null || true
fi
if command -v usermod >/dev/null 2>&1; then
usermod -U {{ automation_user }} 2>/dev/null || true
fi
changed_when: false
- name: Get home directory
raw: "getent passwd {{ automation_user }} | cut -d: -f6 || echo /home/{{ automation_user }}"
register: home_dir_raw
changed_when: false
- name: Set home directory fact
set_fact:
home_dir: "{{ home_dir_raw.stdout | trim }}"
- name: Create .ssh directory
raw: |
mkdir -p {{ home_dir }}/.ssh
chown {{ automation_user }}:{{ automation_user }} {{ home_dir }}/.ssh
chmod 700 {{ home_dir }}/.ssh
changed_when: false
- name: Read local SSH public key
delegate_to: localhost
become: false
slurp:
src: "{{ ssh_public_key_path }}"
register: ssh_pub_key
ignore_errors: true
- name: Try alternate SSH key paths
delegate_to: localhost
become: false
slurp:
src: "{{ item }}"
register: ssh_pub_key_alt
loop:
- "/app/ssh_keys/id_ed25519.pub"
- "/app/ssh_keys/id_automation_ansible.pub"
- "{{ lookup('env', 'HOME') }}/.ssh/id_rsa.pub"
when: ssh_pub_key.failed | default(false)
ignore_errors: true
- name: Set SSH public key content
set_fact:
ssh_key_content: "{{ (ssh_pub_key.content | b64decode | trim) if not (ssh_pub_key.failed | default(false)) else (ssh_pub_key_alt.results | selectattr('content', 'defined') | map(attribute='content') | first | b64decode | trim) }}"
when: not (ssh_pub_key.failed | default(false)) or (ssh_pub_key_alt.results | selectattr('content', 'defined') | list | length > 0)
- name: Install SSH public key
raw: |
cat > {{ home_dir }}/.ssh/authorized_keys << 'SSHKEY_EOF'
{{ ssh_key_content }}
SSHKEY_EOF
chown {{ automation_user }}:{{ automation_user }} {{ home_dir }}/.ssh/authorized_keys
chmod 600 {{ home_dir }}/.ssh/authorized_keys
when: ssh_key_content is defined
- name: Verify authorized_keys
raw: test -s {{ home_dir }}/.ssh/authorized_keys && wc -l < {{ home_dir }}/.ssh/authorized_keys || echo "0"
register: auth_keys_count
changed_when: false
- name: Display SSH key status
debug:
msg: "[4/7] Clé SSH installée ({{ auth_keys_count.stdout | trim }} clé(s))"
- name: Install sudo (Debian/Ubuntu)
raw: |
if ! command -v sudo >/dev/null 2>&1; then
apt-get update -qq && apt-get install -y sudo
fi
when: os_type == "debian"
- name: Install sudo (Alpine)
raw: |
if ! command -v sudo >/dev/null 2>&1; then
apk add --no-cache sudo
fi
when: os_type == "alpine"
- name: Install sudo (FreeBSD)
raw: |
if ! command -v sudo >/dev/null 2>&1; then
pkg install -y sudo
fi
when: os_type == "freebsd"
- name: Display sudo status
debug:
msg: "[5/7] sudo installé/vérifié"
- name: Configure sudoers
raw: |
mkdir -p /etc/sudoers.d
echo "{{ automation_user }} ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/automation
chmod 440 /etc/sudoers.d/automation
changed_when: false
- name: Display sudoers status
debug:
msg: "[6/7] Sudoers configuré: /etc/sudoers.d/automation"
- name: Install Python3 (Debian/Ubuntu)
raw: |
if ! command -v python3 >/dev/null 2>&1; then
apt-get update -qq && apt-get install -y python3
fi
when: os_type == "debian"
- name: Install Python3 (Alpine)
raw: |
if ! command -v python3 >/dev/null 2>&1; then
apk add --no-cache python3
fi
when: os_type == "alpine"
- name: Install Python3 (FreeBSD)
raw: |
if ! command -v python3 >/dev/null 2>&1; then
pkg install -y python3
fi
when: os_type == "freebsd"
- name: Get Python version
raw: python3 --version 2>&1 || echo "N/A"
register: python_version
changed_when: false
- name: Display Python status
debug:
msg: "[7/7] Python3 installé: {{ python_version.stdout | trim }}"
- name: Display bootstrap summary
debug:
msg: |
═══════════════════════════════════════
Bootstrap terminé avec succès!
═══════════════════════════════════════
Host: {{ inventory_hostname }}
OS Type: {{ os_type }} ({{ os_variant }})
User: {{ automation_user }}
Home: {{ home_dir }}
SSH Keys: {{ auth_keys_count.stdout | trim }}
Python: {{ python_version.stdout | trim }}
═══════════════════════════════════════
+83
View File
@@ -0,0 +1,83 @@
---
- name: Health check on target host
hosts: all
become: false
gather_facts: false
vars:
category: monitoring
subcategory: health
tasks:
- name: Check if host is reachable (ping)
ansible.builtin.ping:
register: ping_result
- name: Gather minimal facts
ansible.builtin.setup:
gather_subset:
- '!all'
- '!min'
- distribution
ignore_errors: true
register: facts_result
- name: Get system uptime
ansible.builtin.command: uptime
register: uptime_result
changed_when: false
ignore_errors: true
- name: Get disk usage
ansible.builtin.shell: df -h / | tail -1 | awk '{print $5}'
register: disk_usage
changed_when: false
ignore_errors: true
- name: Get memory usage (Linux)
ansible.builtin.shell: |
if command -v free >/dev/null 2>&1; then
free -m | grep Mem | awk '{printf "%.1f%%", $3/$2 * 100}'
else
# Fallback for systems without free command
cat /proc/meminfo | awk '/MemTotal/{total=$2} /MemAvailable/{avail=$2} END{printf "%.1f%%", (total-avail)/total*100}'
fi
register: memory_usage
changed_when: false
ignore_errors: true
- name: Get CPU temperature (ARM/SBC)
ansible.builtin.shell: |
if [ -f /sys/class/thermal/thermal_zone0/temp ]; then
temp=$(cat /sys/class/thermal/thermal_zone0/temp)
# Use awk instead of bc for better compatibility
echo "${temp}" | awk '{printf "%.1f°C", $1/1000}'
else
echo "N/A"
fi
register: cpu_temp
changed_when: false
ignore_errors: true
- name: Get CPU load
ansible.builtin.shell: |
if [ -f /proc/loadavg ]; then
cat /proc/loadavg | awk '{print $1}'
else
uptime | awk -F'load average:' '{print $2}' | awk -F',' '{print $1}' | tr -d ' '
fi
register: cpu_load
changed_when: false
ignore_errors: true
- name: Display health status
ansible.builtin.debug:
msg: |
═══════════════════════════════════════
Host: {{ inventory_hostname }}
Status: {{ 'OK' if ping_result is success else 'UNREACHABLE' }}
═══════════════════════════════════════
Uptime: {{ uptime_result.stdout | default('N/A') }}
Disk Usage: {{ disk_usage.stdout | default('N/A') }}
Memory Usage: {{ memory_usage.stdout | default('N/A') }}
CPU Load: {{ cpu_load.stdout | default('N/A') }}
CPU Temp: {{ cpu_temp.stdout | default('N/A') }}
═══════════════════════════════════════
+15
View File
@@ -0,0 +1,15 @@
---
# mon-playbook.yml
# Créé le 03/12/2025
- name: mon playbook
hosts: all
become: yes
vars:
category: Test
subcategory: other
tasks:
- name: Exemple de tâche
ansible.builtin.debug:
msg: "Playbook mon-playbook exécuté avec succès!"
+44
View File
@@ -0,0 +1,44 @@
---
- name: Install jq on target host
hosts: all
become: true
gather_facts: true
vars:
category: maintenance
subcategory: system
tasks:
- name: Install jq
ansible.builtin.package:
name: jq
state: present
when: ansible_facts['os_family'] == 'Debian'
- name: Install jq on RedHat family
ansible.builtin.dnf:
name: jq
state: present
when: ansible_facts['os_family'] == 'RedHat'
- name: Install jq on Alpine
ansible.builtin.apk:
name: jq
state: present
when: ansible_facts['os_family'] == 'Alpine'
- name: Install jq on FreeBSD
ansible.builtin.pkg:
name: jq
state: present
when: ansible_facts['os_family'] == 'FreeBSD'
- name: Install jq on OpenBSD
ansible.builtin.pkg_add:
name: jq
state: present
when: ansible_facts['os_family'] == 'OpenBSD'
- name: Install jq on macOS
ansible.builtin.homebrew:
name: jq
state: present
when: ansible_facts['os_family'] == 'Darwin'
- name: Install jq on Windows
ansible.builtin.win_chocolatey:
name: jq
state: present
when: ansible_facts['os_family'] == 'Windows'
+23
View File
@@ -0,0 +1,23 @@
---
- name: Reboot target host
hosts: all
become: true
vars:
category: maintenance
subcategory: system
tasks:
- name: Detect distribution
ansible.builtin.setup:
gather_subset:
- os_family
- name: Reboot the machine (Linux)
ansible.builtin.reboot:
reboot_timeout: 600
when: ansible_facts['os_family'] != 'FreeBSD'
- name: Reboot the machine (FreeBSD)
ansible.builtin.reboot:
reboot_timeout: 600
boot_time_command: sysctl kern.boottime
when: ansible_facts['os_family'] == 'FreeBSD'
+34
View File
@@ -0,0 +1,34 @@
---
- name: Upgrade packages on target host
hosts: all
become: true
vars:
category: maintenance
subcategory: system
tasks:
- name: Detect distribution
ansible.builtin.setup:
gather_subset:
- os_family
- name: Upgrade on Debian/Ubuntu
ansible.builtin.apt:
update_cache: yes
upgrade: dist
when: ansible_facts['os_family'] == 'Debian'
- name: Upgrade on Alpine
ansible.builtin.shell: |
apk update && apk upgrade
when: ansible_facts['os_family'] == 'Alpine'
- name: Upgrade on RedHat family
ansible.builtin.dnf:
name: "*"
state: latest
when: ansible_facts['os_family'] == 'RedHat'
- name: Upgrade on FreeBSD
ansible.builtin.shell: |
pkg update && pkg upgrade -y
when: ansible_facts['os_family'] == 'FreeBSD'