ansible rework

This commit is contained in:
Sergei Poljanski 2025-11-04 11:06:35 +02:00
commit eecff4345a
47 changed files with 1995 additions and 538 deletions

View file

@ -0,0 +1,34 @@
---
# Default variables for docker_service role
# These can be overridden when including this role
# Service configuration
service_name: "" # REQUIRED: Name of the service (e.g., "traefik", "forgejo")
service_base_path: "" # REQUIRED: Base installation path (e.g., "/opt/traefik")
# Directories to create under service_base_path
# Format: list of dicts with 'path' and 'mode' keys
service_directories: []
# Example:
# - { path: "data", mode: "0755" }
# - { path: "config", mode: "0644" }
# Template files to deploy
# Format: list of dicts with 'src', 'dest', and optional 'mode' keys
service_templates: []
# Example:
# - { src: "docker-compose.yml.j2", dest: "docker-compose.yml", mode: "0644" }
# - { src: "config.yaml.j2", dest: "config/config.yaml", mode: "0600" }
# Docker compose configuration
docker_compose_file: "docker-compose.yml" # Name of the compose file
docker_compose_project_name: "{{ service_name }}" # Docker compose project name
# Health check configuration
health_check_enabled: true # Whether to perform health check after deployment
health_check_url: "" # URL to check (empty = skip HTTP check)
health_check_host: "localhost" # Host to check port on (default: localhost)
health_check_port: "" # Port to check (empty = skip port check)
health_check_delay: 10 # Seconds to wait before health check
health_check_retries: 30 # Number of retries for health check
health_check_retry_delay: 2 # Seconds between retries

View file

@ -0,0 +1,81 @@
---
# Shared tasks for deploying Docker-based services
# This role provides a standardized pattern for service deployment
- name: Validate required variables
ansible.builtin.assert:
that:
- service_name is defined
- service_name | length > 0
- service_base_path is defined
- service_base_path | length > 0
fail_msg: "service_name and service_base_path are required variables"
success_msg: "Required variables validated for {{ service_name }}"
- name: Create base service directory
ansible.builtin.file:
path: "{{ service_base_path }}"
state: directory
mode: '0755'
owner: root
group: root
- name: Create service subdirectories
ansible.builtin.file:
path: "{{ service_base_path }}/{{ item.path }}"
state: directory
mode: "{{ item.mode | default('0755') }}"
owner: root
group: root
loop: "{{ service_directories }}"
when: service_directories | length > 0
- name: Deploy service template files
ansible.builtin.template:
src: "{{ item.src }}"
dest: "{{ service_base_path }}/{{ item.dest }}"
mode: "{{ item.mode | default('0644') }}"
owner: root
group: root
loop: "{{ service_templates }}"
when: service_templates | length > 0
register: template_result
- name: Start service with Docker Compose
community.docker.docker_compose_v2:
project_src: "{{ service_base_path }}"
project_name: "{{ docker_compose_project_name }}"
files:
- "{{ docker_compose_file }}"
state: present
register: compose_result
- name: Wait before health check
ansible.builtin.pause:
seconds: "{{ health_check_delay }}"
when: health_check_enabled and (health_check_url != "" or health_check_port != "")
- name: Wait for service HTTP endpoint
ansible.builtin.uri:
url: "{{ health_check_url }}"
status_code: [200, 301, 302, 401, 404] # Accept various codes (service-dependent)
timeout: 5
register: health_result
until: health_result is not failed
retries: "{{ health_check_retries }}"
delay: "{{ health_check_retry_delay }}"
when: health_check_enabled and health_check_url != ""
failed_when: false
- name: Wait for service port
ansible.builtin.wait_for:
host: "{{ health_check_host }}"
port: "{{ health_check_port }}"
delay: 0
timeout: "{{ health_check_retries * health_check_retry_delay }}"
state: started
when: health_check_enabled and health_check_port != ""
- name: Display deployment status
ansible.builtin.debug:
msg: "{{ service_name }} deployment completed successfully"