Ansible serial feature usage and example - unix1998/technical_notes GitHub Wiki

In Ansible, the serial keyword is used to control the number of hosts that are managed at one time in a play. This is particularly useful for performing rolling updates or deployments, where you want to update a subset of hosts at a time to minimize downtime and reduce risk.

How serial Works

  • Rolling Updates: By using serial, you can specify how many hosts should be updated at once. This allows you to perform rolling updates, where a fixed number of hosts are updated in each batch until all hosts are updated.
  • Configuration Management: Similarly, you can use serial to apply configuration changes to a limited number of hosts at a time, ensuring that not all hosts are affected simultaneously if an issue occurs.

Example: Rolling Update Playbook

Below is an example Ansible playbook that demonstrates how to use serial for a rolling update. This playbook will update 2 hosts at a time until all hosts in the inventory are updated.

---
- name: Rolling update example
  hosts: webservers
  serial: 2
  tasks:
    - name: Ensure Apache is installed
      apt:
        name: apache2
        state: present
      become: yes

    - name: Deploy the latest version of the application
      copy:
        src: /path/to/your/app/
        dest: /var/www/html/
      become: yes

    - name: Restart Apache to apply changes
      service:
        name: apache2
        state: restarted
      become: yes

Explanation

  • Hosts: This playbook targets the webservers group defined in your inventory.
  • Serial: The serial: 2 line specifies that only 2 hosts should be processed at a time.
  • Tasks:
    • Ensure Apache is installed: This task installs the Apache web server.
    • Deploy the latest version of the application: This task copies the latest application files to the web server's document root.
    • Restart Apache: This task restarts the Apache service to apply the changes.

How It Works

  1. Batch Execution: Ansible processes the hosts in batches of 2. If you have 10 hosts, it will:
    • Update the first 2 hosts.
    • Then move on to the next 2 hosts.
    • Repeat until all hosts are updated.
  2. Minimizing Risk: By updating a small number of hosts at a time, you reduce the risk of widespread issues. If a problem occurs, it affects only the current batch, allowing you to troubleshoot and fix the issue before proceeding.

Benefits of Using serial

  • Reduced Downtime: Only a subset of your infrastructure is affected at any given time.
  • Incremental Rollout: Makes it easier to monitor and validate changes incrementally.
  • Fault Isolation: If something goes wrong, it's easier to isolate and fix the issue without impacting the entire infrastructure.

Conclusion

Using the serial keyword in Ansible playbooks is an effective way to manage rolling updates and incremental changes to your infrastructure. It helps ensure higher availability and reduces the risk associated with large-scale deployments.