Files
nvhi-atsila-microservice/ansible/setup-ansible.sh
2025-07-16 01:11:58 +00:00

220 lines
6.1 KiB
Bash

#!/bin/bash
# Enterprise Ansible Setup and Test Script
# This script sets up the Ansible environment and runs tests
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if we're in the right directory
if [ ! -d "ansible" ]; then
print_error "ansible directory not found. Please run this script from your project root."
exit 1
fi
cd ansible
print_status "Setting up Enterprise Ansible environment..."
# Create necessary directories
print_status "Creating directory structure..."
mkdir -p group_vars
mkdir -p templates
mkdir -p roles
mkdir -p inventories/production
mkdir -p inventories/staging
# Install Python dependencies
print_status "Installing Python dependencies..."
pip3 install --user boto3 botocore jmespath > /dev/null 2>&1 || {
print_warning "Could not install Python dependencies. Install manually: pip3 install boto3 botocore jmespath"
}
# Check Ansible installation
if ! command -v ansible &> /dev/null; then
print_error "Ansible not found. Please install Ansible first:"
echo " Ubuntu/Debian: sudo apt update && sudo apt install ansible"
echo " RHEL/CentOS: sudo yum install ansible"
echo " macOS: brew install ansible"
exit 1
fi
ANSIBLE_VERSION=$(ansible --version | head -n1)
print_success "Found: $ANSIBLE_VERSION"
# Check AWS CLI
if ! command -v aws &> /dev/null; then
print_error "AWS CLI not found. Please install AWS CLI first."
exit 1
fi
AWS_VERSION=$(aws --version)
print_success "Found: $AWS_VERSION"
# Validate configuration files
print_status "Validating Ansible configuration files..."
# Check if main playbook exists
if [ ! -f "configure_ecs.yml" ]; then
print_error "configure_ecs.yml not found!"
exit 1
fi
# Validate YAML syntax
if command -v yamllint &> /dev/null; then
print_status "Checking YAML syntax..."
yamllint configure_ecs.yml || print_warning "YAML syntax issues found (non-critical)"
else
print_warning "yamllint not found. Install with: pip3 install yamllint"
fi
# Validate Ansible playbook syntax
print_status "Validating Ansible playbook syntax..."
ansible-playbook configure_ecs.yml --syntax-check || {
print_error "Ansible syntax validation failed!"
exit 1
}
print_success "Ansible syntax validation passed"
# Test functions
test_connectivity() {
local ip=$1
if [ -z "$ip" ]; then
print_error "No IP address provided for connectivity test"
return 1
fi
print_status "Testing connectivity to $ip..."
# Test SSH connectivity
if timeout 10 bash -c "nc -z $ip 22" &>/dev/null; then
print_success "SSH port (22) is reachable"
else
print_error "SSH port (22) is not reachable"
return 1
fi
# Test Ansible ping
if ansible inventory_hosts -m ping -i hosts &>/dev/null; then
print_success "Ansible connectivity test passed"
else
print_error "Ansible connectivity test failed"
return 1
fi
return 0
}
# Create a test inventory for validation
create_test_inventory() {
local ip=${1:-"127.0.0.1"}
print_status "Creating test inventory with IP: $ip"
cat > hosts_test << EOF
[inventory_hosts]
test-instance ansible_host=$ip ansible_user=ec2-user
[inventory_hosts:vars]
ansible_ssh_private_key_file=~/.ssh/id_rsa
ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10'
ansible_python_interpreter=/usr/bin/python3
ansible_connection=ssh
aws_region=us-east-2
EOF
}
# Main execution
print_status "Ansible Enterprise Setup Complete!"
echo
echo "Available operations:"
echo " 1. Test connectivity (requires EC2 IP)"
echo " 2. Run simple deployment test"
echo " 3. Validate all playbooks"
echo " 4. Show configuration summary"
echo
# Interactive mode
if [ "$1" == "--interactive" ]; then
echo -n "Enter operation number (1-4): "
read -r operation
case $operation in
1)
echo -n "Enter EC2 instance IP: "
read -r ec2_ip
create_test_inventory "$ec2_ip"
if test_connectivity "$ec2_ip"; then
print_success "Connectivity test passed!"
else
print_error "Connectivity test failed!"
fi
;;
2)
echo -n "Enter EC2 instance IP: "
read -r ec2_ip
create_test_inventory "$ec2_ip"
print_status "Running simple deployment test..."
ansible-playbook simple-deploy.yml -i hosts_test -v
;;
3)
print_status "Validating all playbooks..."
for playbook in *.yml; do
if [ -f "$playbook" ]; then
print_status "Validating $playbook..."
ansible-playbook "$playbook" --syntax-check
fi
done
print_success "All playbooks validated!"
;;
4)
print_status "Configuration Summary:"
echo " - Working Directory: $(pwd)"
echo " - Ansible Version: $(ansible --version | head -n1)"
echo " - AWS CLI Version: $(aws --version 2>&1)"
echo " - Available Playbooks:"
ls -la *.yml 2>/dev/null | awk '{print " - " $9}' || echo " - None found"
echo " - Python Dependencies:"
python3 -c "import boto3, botocore; print(' - boto3: ' + boto3.__version__); print(' - botocore: ' + botocore.__version__)" 2>/dev/null || echo " - Not installed"
;;
*)
print_error "Invalid operation number"
;;
esac
fi
# Cleanup
if [ -f "hosts_test" ]; then
rm -f hosts_test
fi
print_success "Setup script completed!"
echo
echo "Next steps:"
echo " 1. Update your Jenkins pipeline with the new Ansible integration"
echo " 2. Test with: ./setup-ansible.sh --interactive"
echo " 3. Run deployment: ansible-playbook configure_ecs.yml -i hosts -v"
echo