An Ansible project for automated deployment of Docker Compose stacks with Nginx reverse proxy.
.
├── inventories/ # Host and variable definitions
│ └── dev/
│ ├── hosts.ini # List of hosts
│ └── group_vars/ # Variables for each group
├── playbooks/
│ └── site.yml # Main playbook
└── roles/ # Ansible roles
├── common/ # Common packages
├── docker/ # Docker installation
├── app_compose/ # Deploy Docker Compose stacks
└── nginx/ # Install and configure Nginx
Installs common packages (curl, vim, git, ...)
Installs Docker and Docker Compose plugin
Deploys Docker Compose stacks from local files
Important variables:
stack_name: Name of the stackstack_base_dir: Base directory path (/opt/stack)compose_src: Path to docker-compose.yml fileenv_vars: Dictionary of environment variables
Installs and configures Nginx as a reverse proxy
Important variables:
nginx_site_name: Name of the site confignginx_listen_port: Port (default: 80)nginx_server_name: Server name (default: _)nginx_proxy_pass: Backend address
ansible-playbook -i inventories/dev/hosts.ini playbooks/site.ymlansible-playbook playbooks/site.yml --syntax-checkansible-playbook -i inventories/dev/hosts.ini playbooks/site.yml --checkansible-playbook -i inventories/dev/hosts.ini playbooks/site.yml --tags dockerThe project includes an automated workflow:
- Validate - Check syntax of all playbooks
- Lint - Check YAML standards and Ansible best practices
- Deploy to Dev - Auto-deploy to development (only on
developbranch) - Deploy to Prod - Auto-deploy to production (only on
mainbranch)
In GitHub repository settings, follow these steps:
In Settings > Secrets and variables > Actions:
- SSH_PRIVATE_KEY: SSH key for connecting to servers
In Settings > Environments:
development:
- Production branch:
develop - Deployment branches:
develop
production:
- Production branch:
main - Deployment branches:
main
# Generate SSH key (if you don't have one)
ssh-keygen -t ed25519 -f ansible_key -C "ansible@prod"
# Add public key to servers
# Add private key to GitHub Secretsdocker-compose.yml files should be located in the files/compose/ directory:
files/
└── compose/
├── app/
│ └── docker-compose.yml
└── db/
└── docker-compose.yml
[web]
web01 ansible_host=192.168.56.10
[db]
db01 ansible_host=192.168.56.20
[all:vars]
ansible_user=ubuntu
ansible_become=trueweb.yml:
stack_name: app
compose_src: "files/compose/app/docker-compose.yml"
env_vars:
PORT: "8080"
nginx_proxy_pass: "http://127.0.0.1:8080"db.yml:
stack_name: db
compose_src: "files/compose/db/docker-compose.yml"
env_vars:
POSTGRES_PASSWORD: "secure-password"- Ansible 2.9+
- Python 3.6+
- SSH access to target servers
- Ubuntu/Debian servers
For local development:
# Install yamllint and ansible-lint
pip install yamllint ansible-lint
# Check YAML
yamllint .
# Check Ansible
ansible-lint playbooks/site.yml