A comprehensive Ansible-based infrastructure automation solution for provisioning secure, containerized application deployment environments with blue-green deployment capabilities, Cloudflare integration, and hardened security configurations.
This project automates the complete setup of production-ready Linux servers optimized for containerized application deployments. It creates a secure, multi-layered environment with:
- Blue-Green Deployment Infrastructure: Seamless zero-downtime deployments using Nginx load balancing
- Cloudflare Integration: Automatic IP range updates and Origin Certificate management
- Container Orchestration: Podman-based rootless container management with systemd integration
- Security Hardening: Multi-layer security with restricted users, nftables firewall, and SSH hardening
- Automated Monitoring: Dynamic Cloudflare IP updates and system health monitoring
βββββββββββββββ ββββββββββββββ βββββββββββββββββββ
β Cloudflare ββββ>β nftables ββββ>β Nginx β
β (SSL/Proxy) β β (Firewall) β β (Load Balancer) β
βββββββββββββββ ββββββββββββββ βββββββββββββββββββ
β
βββββββββββ΄ββββββββββ
β β
βββββββββΌβββββββ ββββββββΌββββββββ
β Blue Pool β β Green Pool β
β :2020, :2021 β β :4040, :4041 β
ββββββββββββββββ ββββββββββββββββ
β β
βββββββΌβββββββ βββββββΌβββββββ
β Podman β β Podman β
β Containers β β Containers β
ββββββββββββββ ββββββββββββββ
Environment Variables (group_vars/all.yml)
# Blue/Green Deployment Pools
blue_upstreams:
- { host: "127.0.0.1", port: 2020 }
- { host: "127.0.0.1", port: 2021 }
green_upstreams:
- { host: "127.0.0.1", port: 4040 }
- { host: "127.0.0.1", port: 4041 }
# Security Configuration
deploy_user: "deployer"
admin_user: "admin"
# SSL Configuration
server_name: "*.example.com"
ssl_cert_path: /etc/nginx/ssl/cf_origin.pem
ssl_key_path: /etc/nginx/ssl/cf_origin.keySSH_LOGIN_PRIV_KEY: Private SSH key for server accessDEPLOY_SSH_PUB_KEY: Public key for deployment userADMIN_SSH_PUB_KEY: Public key for admin userCF_CERT: Cloudflare Origin CertificateCF_KEY: Cloudflare Origin Certificate private keyDEPLOY_PWD&ADMIN_PWD: Respective users' passwordsOBSCURED_DIR: Directory on hosts where scripts for deploy group residesAWS_*: AWS credentials for Terraform state backendAZURE_*: Azure credentials for infrastructure provisioningSEC_SSH_PORT: Custom SSH port for server accessTF_VARS_B64: Base64 encoded Terraform variable file content
- Configure Secrets: Add required secrets to your GitHub repository
- Run Workflow: Trigger the "Initialize Infrastructure" workflow
- Select Environment: Choose Dev or Prod environment
- Monitor Progress: Watch the automated provisioning process
Use the deploy.sh script for blue-green deployments:
# Deploy new version
./deploy.sh -a myapp -p 8080 -g 4040 -b 2020 -v v1.2.3 -r true
# Switch traffic to new deployment
./deploy.sh -a myapp -f true- Cloud Provisioning: Automated cloud infrastructure provisioning
- Multi-Cloud Support: Resource provisioning across DigitalOcean & Cloudflare
- State Management: Cloudflare R2 for S3 based state backend
- Configure Variables: Copy
terraform\(dev,prod).auto.tfvars.exampleto create your environment-specific variable files (e.g.,dev.auto.tfvarsorprod.auto.tfvars). - Configure Backend: Copy
terraform\backend-env.(ps1,sh).exampleto eitherterraform\backend-env.ps1orterraform\backend-env.shand run specified commands to set up your backend configuration for state management. - Initialize Terraform: Run
terraform initto initialize the working directory. - Plan Changes: Execute
terraform planto preview changes before applying. - Apply Changes: Use
terraform applyto create or update infrastructure. - Manage State: Rely on Cloudflare R2 for storing and managing Terraform state files.
- Firewall: nftables configuration allowing only Cloudflare IP ranges for HTTPS
- SSH Security: Custom port, key-only authentication with connection limits
- IP Whitelisting: Automatic Cloudflare IP range updates every 6 hours
- Principle of Least Privilege: Deployment user with minimal required permissions
- Restricted Shell: Custom rbash implementation with command filtering and path restrictions
- Sudo Restrictions: Whitelist of specific commands for deployment operations only
- Directory ACLs: Restricted access to sensitive system directories
- Rootless Containers: All containers run without root privileges
- Read-Only Filesystems: Containers mounted with read-only root filesystem
- Capability Dropping: All Linux capabilities removed from containers
- No New Privileges: Prevents privilege escalation within containers
- Secret Management: Environment variables stored as Podman secrets
- Cloudflare Origin Certificates: End-to-end encryption with Cloudflare
- Modern TLS: TLS 1.2+ only with secure cipher suites
- HSTS Ready: Prepared for HTTP Strict Transport Security implementation
- GitHub Actions: Automated infrastructure provisioning
- Environment Management: Separate Dev/Prod configurations
- Secret Management: Secure handling of sensitive configuration data
- IP Range Updates: Automated Cloudflare IP synchronization with nftables and Nginx
- Container Management: Systemd integration for automatic container restart and management
- Blue-Green Deployments: Automated zero-downtime deployment switching
- Image Management: Automatic container image pulling and version management
- Service Integration: Seamless systemd service management for containers with Podman Quadlets
- Caching Layer: High-performance Redis-compatible caching solution
- Session Management: Distributed session storage for scalable applications
- Rate Limiting: Advanced rate limiting and throttling capabilities
- Metrics Collection: Prometheus/Grafana integration for comprehensive monitoring
- Alerting: Automated alerting for security events and system issues
- Log Aggregation: Centralized logging with ELK stack integration
- Target Servers: Ubuntu/Debian-based Linux servers
- Ansible: Version 2.18+ with required collections
- SSH Access: Root or sudo access to target servers
- Cloudflare: Account with Origin Certificate
- GitHub: Repository with GH-Actions (for automated deployment)
We welcome contributions! Please feel free to submit issues, feature requests, or pull requests.
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
Copyright (c) 2024 Hack Nocturne Team
This project is licensed under the MIT License.
- β Permitted: Use, modify, distribute, and sell for any purpose (personal or commercial)
- β Freedom: Create derivative works and distribute them under any license
- β Open Source: Full open source compliance with minimal restrictions
- π Attribution: Simply include the original copyright notice and license text
See the full license terms in the LICENSE file.
Rishabh Kumar
β‘ Powered by Ansible, Podman, Cloudflare & Terraform