{"id":30703056,"url":"https://github.com/sajjadhz/ansiblebluegreendeploymentfastapi","last_synced_at":"2026-05-07T00:34:49.157Z","repository":{"id":310471421,"uuid":"1039974518","full_name":"Sajjadhz/AnsibleBlueGreenDeploymentFastAPI","owner":"Sajjadhz","description":"Deploying FastAPI app with Blue/Green deployment strategy using Ansible","archived":false,"fork":false,"pushed_at":"2025-08-18T09:19:08.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-18T11:24:21.792Z","etag":null,"topics":["ansible","ansible-playbook","ansible-role","blue-green-deployment","fastapi","load-balancer","zero-downtime","zero-downtime-deploy"],"latest_commit_sha":null,"homepage":"","language":"Jinja","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sajjadhz.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-08-18T09:12:33.000Z","updated_at":"2025-08-18T09:22:47.000Z","dependencies_parsed_at":"2025-08-18T11:36:06.335Z","dependency_job_id":null,"html_url":"https://github.com/Sajjadhz/AnsibleBlueGreenDeploymentFastAPI","commit_stats":null,"previous_names":["sajjadhz/ansiblebluegreendeploymentfastapi"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/Sajjadhz/AnsibleBlueGreenDeploymentFastAPI","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sajjadhz%2FAnsibleBlueGreenDeploymentFastAPI","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sajjadhz%2FAnsibleBlueGreenDeploymentFastAPI/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sajjadhz%2FAnsibleBlueGreenDeploymentFastAPI/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sajjadhz%2FAnsibleBlueGreenDeploymentFastAPI/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sajjadhz","download_url":"https://codeload.github.com/Sajjadhz/AnsibleBlueGreenDeploymentFastAPI/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sajjadhz%2FAnsibleBlueGreenDeploymentFastAPI/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273317766,"owners_count":25084037,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-02T02:00:09.530Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansible","ansible-playbook","ansible-role","blue-green-deployment","fastapi","load-balancer","zero-downtime","zero-downtime-deploy"],"created_at":"2025-09-02T16:54:24.954Z","updated_at":"2026-05-07T00:34:49.115Z","avatar_url":"https://github.com/Sajjadhz.png","language":"Jinja","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Blue/Green Deployment for FastAPI with Nginx and Ansible\n\nThis project implements zero-downtime deployments for FastAPI applications using Ansible and Nginx with a blue/green deployment strategy.\n\n## Prerequisites\n\n- Vagrant 2.2+\n- Ansible 2.9+\n- VirtualBox 6.0+\n- Python 3.6+\n\n## Infrastructure Overview\n\n- **Load Balancer**: `lb.example.com` (Nginx)\n- **Blue Environment**: `blue.example.com`\n- **Green Environment**: `green.example.com`\n\n## File Structure\n\n```\n.\n├── playbooks/\n│   ├── deploy.yml            # Deploys application\n│   ├── setup_loadbalancer.yml # Configures Nginx\n│   ├── switch.yml           # Switches traffic\n│   └── cleanup.yml          # Cleans old deployments\n├── templates/\n│   ├── fastapi.service.j2    # Systemd service template\n│   └── loadbalancer.conf.j2  # Nginx config template\n├── inventory/\n│   └── production           # Inventory file\n└── app/\n    ├── blue/                # Blue deployment files\n    └── green/               # Green deployment files\n```\n\n## Deployment Workflow\n\n### 1. Initial Setup\n\n```bash\n# Create all VMs\nvagrant up\n\n# Configure load balancer\nansible-playbook -i inventory/production playbooks/setup_loadbalancer.yml\n```\n\n### 2. First Deployment (Blue)\n\n```bash\nansible-playbook -i inventory/production playbooks/deploy.yml \\\n  -e \"target_group=blue deployment_color=blue\"\n```\n\n### 3. Deploy New Version (Green)\n\n```bash\nansible-playbook -i inventory/production playbooks/deploy.yml \\\n  -e \"target_group=green deployment_color=green\"\n```\n\n### 4. Test Green Environment\n\n```bash\n# Directly access green environment\ncurl http://green.example.com:8000\n\n# Or through load balancer with test header\ncurl -H \"X-Deployment: green\" http://lb.example.com\n```\n\n### 5. Switch Traffic to Green\n\n```bash\nansible-playbook -i inventory/production playbooks/switch.yml \\\n  -e \"deployment_color=green\"\n```\n\n### 6. (Optional) Clean Up Blue\n\n```bash\nansible-playbook -i inventory/production playbooks/cleanup.yml \\\n  -e \"old_deployment_group=blue old_deployment_color=blue\"\n```\n\n## Rollback Procedure\n\n```bash\n# Switch back to blue\nansible-playbook -i inventory/production playbooks/switch.yml \\\n  -e \"deployment_color=blue\"\n\n# Clean up green if needed\nansible-playbook -i inventory/production playbooks/cleanup.yml \\\n  -e \"old_deployment_group=green old_deployment_color=green\"\n```\n\n## Maintenance Commands\n\n**Check current traffic distribution:**\n```bash\nvagrant ssh lb\nsudo tail -f /var/log/nginx/access.log | awk '{print $6}' | sort | uniq -c\n```\n\n**Validate Nginx config:**\n```bash\nvagrant ssh lb\nsudo nginx -t\n```\n\n**View service status:**\n```bash\n# On blue/green nodes\nsystemctl status fastapi-blue\nsystemctl status fastapi-green\n```\n\n## Customization\n\n### Environment Variables\n\nSet in `inventory/production`:\n```ini\n[all:vars]\napp_port=8000\ndomain=example.com\n```\n\n### Weighted Traffic Distribution\n\nFor canary releases:\n```bash\nansible-playbook -i inventory/production playbooks/switch.yml \\\n  -e \"deployment_color=green blue_weight=90 green_weight=10\"\n```\n```bash\nansible-playbook -i inventory/production playbooks/switch.yml \\\n  -e \"deployment_color=green blue_weight=50 green_weight=50\"\n```\n```bash\nansible-playbook -i inventory/production playbooks/switch.yml \\\n  -e \"deployment_color=green blue_weight=10 green_weight=90\"\n```\n```bash\nansible-playbook -i inventory/production playbooks/switch.yml \\\n  -e \"deployment_color=green blue_weight=0 green_weight=100\"\n```\n\n## Best Practices\n\n1. Always test new deployments before switching traffic\n2. Monitor both environments during cutover\n3. Keep previous deployment until new one is verified stable\n4. Use low DNS TTL if using DNS-based switching\n5. Implement health checks in your application\n\n## Troubleshooting\n\n**Nginx fails to reload:**\n- Check syntax: `sudo nginx -t`\n- Verify ports are open\n- Ensure DNS resolution works\n\n**Service fails to start:**\n- Check logs: `journalctl -u fastapi-blue`\n- Verify Python dependencies\n- Check port availability\n\n```\n\nThis README provides:\n1. Clear step-by-step deployment instructions\n2. Rollback procedures\n3. Maintenance commands\n4. Customization options\n5. Troubleshooting tips\n6. Best practices\n\nThe workflow ensures zero-downtime deployments with proper testing and rollback capabilities.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsajjadhz%2Fansiblebluegreendeploymentfastapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsajjadhz%2Fansiblebluegreendeploymentfastapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsajjadhz%2Fansiblebluegreendeploymentfastapi/lists"}