feat: CI/CD pipeline setup complete - Ansible playbooks updated, secrets configured, workflow ready
This commit is contained in:
160
.deployment-archive-20251030-111806/scripts/lib/ansible.sh
Executable file
160
.deployment-archive-20251030-111806/scripts/lib/ansible.sh
Executable file
@@ -0,0 +1,160 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Ansible Integration Library
|
||||
# Provides helpers for Ansible operations
|
||||
#
|
||||
|
||||
# Source common library
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
# shellcheck source=./common.sh
|
||||
source "${SCRIPT_DIR}/common.sh"
|
||||
|
||||
# Default Ansible paths
|
||||
readonly ANSIBLE_DIR="${ANSIBLE_DIR:-${SCRIPT_DIR}/../../ansible}"
|
||||
readonly ANSIBLE_INVENTORY="${ANSIBLE_INVENTORY:-${ANSIBLE_DIR}/inventory/production.yml}"
|
||||
readonly ANSIBLE_PLAYBOOK_DIR="${ANSIBLE_PLAYBOOK_DIR:-${ANSIBLE_DIR}/playbooks}"
|
||||
|
||||
# Check Ansible installation
|
||||
check_ansible() {
|
||||
log_step "Checking Ansible installation..."
|
||||
|
||||
require_command "ansible" "sudo apt install ansible" || return 1
|
||||
require_command "ansible-playbook" || return 1
|
||||
|
||||
local version
|
||||
version=$(ansible --version | head -1)
|
||||
log_success "Ansible installed: $version"
|
||||
}
|
||||
|
||||
# Test Ansible connectivity
|
||||
test_ansible_connectivity() {
|
||||
local inventory="${1:-$ANSIBLE_INVENTORY}"
|
||||
|
||||
log_step "Testing Ansible connectivity..."
|
||||
|
||||
if ! ansible all -i "$inventory" -m ping &> /dev/null; then
|
||||
log_error "Cannot connect to production server"
|
||||
log_info "Check:"
|
||||
log_info " - SSH key: ~/.ssh/production"
|
||||
log_info " - Network connectivity"
|
||||
log_info " - Server availability"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_success "Connection successful"
|
||||
return 0
|
||||
}
|
||||
|
||||
# Run Ansible playbook
|
||||
run_ansible_playbook() {
|
||||
local playbook="$1"
|
||||
shift
|
||||
local extra_args=("$@")
|
||||
|
||||
log_step "Running Ansible playbook: $(basename "$playbook")"
|
||||
|
||||
# Build command
|
||||
local cmd="ansible-playbook -i ${ANSIBLE_INVENTORY} ${playbook}"
|
||||
|
||||
# Add extra args
|
||||
if [[ ${#extra_args[@]} -gt 0 ]]; then
|
||||
cmd="${cmd} ${extra_args[*]}"
|
||||
fi
|
||||
|
||||
log_debug "Command: $cmd"
|
||||
|
||||
# Execute with proper error handling
|
||||
if eval "$cmd"; then
|
||||
log_success "Playbook completed successfully"
|
||||
return 0
|
||||
else
|
||||
local exit_code=$?
|
||||
log_error "Playbook failed with exit code $exit_code"
|
||||
return $exit_code
|
||||
fi
|
||||
}
|
||||
|
||||
# Run deployment playbook
|
||||
run_deployment() {
|
||||
local git_repo_url="${1:-}"
|
||||
local playbook="${ANSIBLE_PLAYBOOK_DIR}/deploy.yml"
|
||||
|
||||
if [[ ! -f "$playbook" ]]; then
|
||||
log_error "Deployment playbook not found: $playbook"
|
||||
return 1
|
||||
fi
|
||||
|
||||
log_step "Starting deployment..."
|
||||
|
||||
local extra_args=()
|
||||
if [[ -n "$git_repo_url" ]]; then
|
||||
extra_args+=("-e" "git_repo_url=${git_repo_url}")
|
||||
log_info "Git repository: $git_repo_url"
|
||||
else
|
||||
log_info "Using existing code on server"
|
||||
fi
|
||||
|
||||
run_ansible_playbook "$playbook" "${extra_args[@]}"
|
||||
}
|
||||
|
||||
# Get Ansible facts
|
||||
get_ansible_facts() {
|
||||
local inventory="${1:-$ANSIBLE_INVENTORY}"
|
||||
local host="${2:-production_server}"
|
||||
|
||||
ansible "$host" -i "$inventory" -m setup
|
||||
}
|
||||
|
||||
# Ansible dry-run
|
||||
ansible_dry_run() {
|
||||
local playbook="$1"
|
||||
shift
|
||||
local extra_args=("$@")
|
||||
|
||||
log_step "Running dry-run (check mode)..."
|
||||
|
||||
extra_args+=("--check" "--diff")
|
||||
|
||||
run_ansible_playbook "$playbook" "${extra_args[@]}"
|
||||
}
|
||||
|
||||
# List Ansible hosts
|
||||
list_ansible_hosts() {
|
||||
local inventory="${1:-$ANSIBLE_INVENTORY}"
|
||||
|
||||
log_step "Listing Ansible hosts..."
|
||||
|
||||
ansible-inventory -i "$inventory" --list
|
||||
}
|
||||
|
||||
# Check playbook syntax
|
||||
check_playbook_syntax() {
|
||||
local playbook="$1"
|
||||
|
||||
log_step "Checking playbook syntax..."
|
||||
|
||||
if ansible-playbook --syntax-check "$playbook" &> /dev/null; then
|
||||
log_success "Syntax check passed"
|
||||
return 0
|
||||
else
|
||||
log_error "Syntax check failed"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Execute Ansible ad-hoc command
|
||||
ansible_adhoc() {
|
||||
local host="$1"
|
||||
local module="$2"
|
||||
shift 2
|
||||
local args=("$@")
|
||||
|
||||
log_step "Running ad-hoc command on $host..."
|
||||
|
||||
ansible "$host" -i "$ANSIBLE_INVENTORY" -m "$module" -a "${args[*]}"
|
||||
}
|
||||
|
||||
# Export functions
|
||||
export -f check_ansible test_ansible_connectivity run_ansible_playbook
|
||||
export -f run_deployment get_ansible_facts ansible_dry_run
|
||||
export -f list_ansible_hosts check_playbook_syntax ansible_adhoc
|
||||
Reference in New Issue
Block a user