Automated Canary Deployment with Ansible

By: fyvo August 1, 2025 DevOps

Description

This Ansible playbook automates a canary deployment strategy. It deploys a new version to a small subset of servers, monitors performance, and then rolls out to the rest if successful.

Code Snippet

---
- hosts: canary_servers
  become: true
  tasks:
    - name: Deploy new version to canary servers
      copy: 
        src: /path/to/new/app
        dest: /opt/myapp
    - name: Restart application
      service: 
        name: myapp
        state: restarted
    - name: Monitor canary servers
      uri:
        url: http://canary_server_ip/healthcheck
        status_code: 200
        retries: 3
        delay: 10
      register: canary_check
    - name: Rollout to production if successful
      when: canary_check.status == 200
      include_role:
        name: production_deployment
      delegate_to: 0.0.0.0

- hosts: production_servers
  become: true
  roles:
    - role: production_deployment
  tags:
    - production_deployment

---

# production_deployment.yml
---
- hosts: all
  become: true
  tasks:
    - name: Deploy new version to production servers
      copy: 
        src: /path/to/new/app
        dest: /opt/myapp
    - name: Restart application
      service: 
        name: myapp
        state: restarted

Discussion (0)