init
This commit is contained in:
commit
39d06135c3
29 files changed
+6840
No files matched your search
+136
@@ -0,0 +1,136 @@
|
||||
---
|
||||
- name: Deploy Application with Auto-Scaling
|
||||
hosts: "{{ target_hosts | default('all') }}"
|
||||
become: yes
|
||||
become_method: sudo
|
||||
vars_files:
|
||||
- group_vars/default.yml
|
||||
- "group_vars/vault/{{ deploy_env }}.yml"
|
||||
|
||||
vars:
|
||||
# Deployment configuration
|
||||
deploy_strategy: "{{ deploy_strategy | default('docker') }}"
|
||||
docker_image_tag: "{{ docker_image_tag | default('latest') }}"
|
||||
|
||||
# Environment detection
|
||||
is_production: "{{ deploy_env == 'production' }}"
|
||||
is_staging: "{{ deploy_env == 'staging' }}"
|
||||
is_development: "{{ deploy_env == 'development' }}"
|
||||
|
||||
pre_tasks:
|
||||
- name: Validate required variables
|
||||
assert:
|
||||
that:
|
||||
- app_name is defined and app_name != ""
|
||||
- git_repo is defined and git_repo != ""
|
||||
- server_user is defined and server_user != ""
|
||||
- deploy_env is defined and deploy_env != ""
|
||||
fail_msg: "Missing required variables: app_name, git_repo, server_user, and deploy_env must be defined"
|
||||
|
||||
- name: Validate auto-scaling configuration
|
||||
assert:
|
||||
that:
|
||||
- replicas | int >= 1
|
||||
- replicas | int <= max_replicas | int
|
||||
- deployment_strategy in ['rolling', 'blue-green', 'recreate']
|
||||
fail_msg: "Invalid auto-scaling configuration. Replicas must be between 1 and {{ max_replicas }}, and deployment_strategy must be one of: rolling, blue-green, recreate"
|
||||
|
||||
- name: Load project-specific variables
|
||||
include_vars: "projects/{{ app_name }}/vars.yml"
|
||||
when:
|
||||
- lookup('file', 'projects/' + app_name + '/vars.yml', errors='ignore')
|
||||
|
||||
- name: Override deployment strategy from project vars
|
||||
set_fact:
|
||||
deploy_strategy: "{{ deploy_strategy | default('docker') }}"
|
||||
|
||||
- name: Ensure port registry exists
|
||||
file:
|
||||
path: "{{ port_registry_file }}"
|
||||
state: touch
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
|
||||
- name: Initialize port registry if empty
|
||||
copy:
|
||||
content: "projects: {}"
|
||||
dest: "{{ port_registry_file }}"
|
||||
force: no
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
|
||||
tasks:
|
||||
# ============================================================================
|
||||
# DOCKER DEPLOYMENT WITH AUTO-SCALING
|
||||
# ============================================================================
|
||||
- name: Include docker role with auto-scaling
|
||||
include_role:
|
||||
name: docker
|
||||
when: deploy_strategy in ['simple-docker', 'docker']
|
||||
|
||||
# ============================================================================
|
||||
# POST-DEPLOYMENT VERIFICATION
|
||||
# ============================================================================
|
||||
- name: Wait for deployment to settle
|
||||
pause:
|
||||
seconds: 10
|
||||
when: deploy_strategy in ['simple-docker', 'docker']
|
||||
|
||||
# ============================================================================
|
||||
# CLEANUP AND MAINTENANCE
|
||||
# ============================================================================
|
||||
- name: Cleanup old Docker images
|
||||
shell: |
|
||||
sudo /home/{{ server_user }}/scripts/manage-{{ app_name }}-{{ deploy_env }}{{ env_tag | default('') }}.sh clean
|
||||
become_user: "{{ server_user }}"
|
||||
when:
|
||||
- deploy_strategy in ['simple-docker', 'docker']
|
||||
- cleanup_old_images | default(true)
|
||||
ignore_errors: yes
|
||||
|
||||
# ============================================================================
|
||||
# DEPLOYMENT SUMMARY
|
||||
# ============================================================================
|
||||
- name: Generate deployment summary
|
||||
set_fact:
|
||||
deployment_summary:
|
||||
application: "{{ app_name }}"
|
||||
environment: "{{ deploy_env }}{{ env_tag | default('') }}"
|
||||
version: "{{ docker_image_tag }}"
|
||||
replicas: "{{ replicas }}"
|
||||
strategy: "{{ deployment_strategy }}"
|
||||
deploy_strategy: "{{ deploy_strategy }}"
|
||||
timestamp: "{{ ansible_date_time.iso8601 }}"
|
||||
duration: "{{ ansible_play_duration | default('unknown') }}"
|
||||
|
||||
- name: Display deployment summary
|
||||
debug:
|
||||
msg:
|
||||
- "🎉 DEPLOYMENT SUMMARY"
|
||||
- "===================="
|
||||
- "Application: {{ deployment_summary.application }}"
|
||||
- "Environment: {{ deployment_summary.environment }}"
|
||||
- "Version: {{ deployment_summary.version }}"
|
||||
- "Replicas: {{ deployment_summary.replicas }}"
|
||||
- "Strategy: {{ deployment_summary.strategy }}"
|
||||
- "Timestamp: {{ deployment_summary.timestamp }}"
|
||||
|
||||
- name: Save deployment record
|
||||
copy:
|
||||
content: "{{ deployment_summary | to_nice_yaml }}"
|
||||
dest: "/home/{{ server_user }}/ci/logs/{{ app_name }}/{{ deploy_env }}/deployment-{{ ansible_date_time.epoch }}.yml"
|
||||
when: save_deployment_record | default(true)
|
||||
|
||||
# ============================================================================
|
||||
# HANDLERS
|
||||
# ============================================================================
|
||||
handlers:
|
||||
- name: reload nginx
|
||||
service:
|
||||
name: nginx
|
||||
state: reloaded
|
||||
when: ansible_service_mgr == "systemd"
|
||||
|
||||
# ============================================================================
|
||||
# POST TASKS
|
||||
# ============================================================================
|
||||
Reference in new issue
Block a user