Most teams don’t struggle with writing Ansible playbooks—they struggle with turning them into a reliable deployment workflow. Running ansible-playbook manually works for demos, but production needs repeatability, safety, and structure.
Let’s walk through a practical way to create a deploy Ansible workflow that holds up under real-world usage.
Start with a Real Deployment Scenario
Imagine you're deploying a Node.js API to multiple environments:
- Dev
- Staging
- Production
You need:
- Consistent server setup
- Application deployment
- Environment-specific configs
- Controlled rollouts
This is where a structured Ansible workflow shines.
Organizing Your Project (This Matters More Than You Think)
A common mistake developers make is dumping everything into a single playbook. Instead, structure your project like this:
1ansible/
2├── inventories/
3│ ├── dev/
4│ │ ├── hosts.ini
5│ │ └── group_vars/
6│ ├── staging/
7│ └── production/
8├── roles/
9│ ├── common/
10│ ├── node_app/
11│ └── nginx/
12├── playbooks/
13│ └── deploy.yml
14└── ansible.cfg
15This layout separates concerns:
- Inventories define environments
- Roles define reusable logic
- Playbooks orchestrate execution
Define Your Inventory Per Environment
Each environment should have its own inventory. Example:
1[web]
2web1.example.com
3web2.example.com
4
5[web:vars]
6app_env=production
7This allows your Ansible deployment workflow to stay environment-aware without hardcoding values.
Build Reusable Roles
Instead of writing one long script, break logic into roles.
Example: Node App Role
1# roles/node_app/tasks/main.yml
2- name: Pull latest code
3 git:
4 repo: 'https://github.com/example/app.git'
5 dest: /var/www/app
6
7- name: Install dependencies
8 npm:
9 path: /var/www/app
10
11- name: Restart app
12 systemd:
13 name: app
14 state: restarted
15This role can now be reused across environments.
The Deployment Playbook
This is the entry point of your workflow.
1# playbooks/deploy.yml
2- hosts: web
3 become: true
4 roles:
5 - common
6 - node_app
7 - nginx
8Clean, readable, and easy to extend.
Here’s Where Things Get Interesting: Workflow Execution
A proper deploy Ansible workflow isn’t just about code—it’s about how it runs.
Option 1: Manual Execution
1ansible-playbook -i inventories/production/hosts.ini playbooks/deploy.ymlGood for testing, not ideal for teams.
Option 2: Scripted Workflow
Create a simple deploy script:
1# deploy.sh
2ENV=$1
3
4ansible-playbook \
5 -i inventories/$ENV/hosts.ini \
6 playbooks/deploy.ymlNow deployment becomes:
1./deploy.sh stagingIntegrating with CI/CD
This is where your workflow becomes production-ready.
Example using GitHub Actions:
1name: Deploy
2
3on:
4 push:
5 branches:
6 - main
7
8jobs:
9 deploy:
10 runs-on: ubuntu-latest
11
12 steps:
13 - name: Checkout code
14 uses: actions/checkout@v3
15
16 - name: Run Ansible
17 run: |
18 ansible-playbook -i inventories/production/hosts.ini playbooks/deploy.yml
19This ensures every deployment is:
- Tracked
- Repeatable
- Automated
Safe Deployment Techniques
Deploying to all servers at once is risky. Ansible gives you better options.
Rolling Deployments
1- hosts: web
2 serial: 2
3 roles:
4 - node_app
5This updates two servers at a time, reducing downtime risk.
Check Mode (Dry Run)
1ansible-playbook playbooks/deploy.yml --checkUseful to validate changes before applying them.
Limit Hosts
1ansible-playbook -i inventories/prod/hosts.ini playbooks/deploy.yml --limit web1Great for testing on a single server first.
Handling Secrets the Right Way
Hardcoding secrets is a fast way to create problems.
Use Ansible Vault:
1ansible-vault encrypt group_vars/production/secrets.ymlThen reference securely in your playbooks.
Common Pitfalls to Avoid
- Mixing environment configs in one place
- Skipping roles and writing monolithic playbooks
- Deploying without testing (no check mode)
- Running everything manually instead of CI
- Ignoring idempotency (tasks should be safe to rerun)
Performance Considerations
If your infrastructure grows, your workflow needs to keep up:
- Use
forksinansible.cfgto increase parallelism - Cache facts to reduce overhead
- Limit unnecessary tasks per run
A Simple Mental Model
Inventory defines where, roles define what, and playbooks define how.
If your workflow respects this separation, it will scale naturally.
Wrapping It Up
A solid Ansible deployment workflow isn’t about complexity—it’s about consistency. Once you introduce structure, environment separation, and CI integration, deployments stop being stressful and start becoming predictable.
If you’re still running playbooks manually, the biggest upgrade isn’t new tooling—it’s designing the workflow around how your team actually ships code.