Introduction
Ansible is a powerful tool for automating IT tasks and one of its most versatile features is the playbook. Playbooks are used to define a series of tasks to be executed on a set of hosts. As your infrastructure grows so does the complexity of your playbooks. This blog will explore advanced playbook strategies and handlers allowing you to create more efficient, reliable and maintainable automation scripts. We will cover different playbook strategies, configuring handlers for service management, using tags to selectively run parts of playbooks and implementing rolling updates with the serial strategy.
Playbook Strategies
Ansible provides several strategies for running tasks within a playbook. The strategy dictates how tasks are executed across the hosts. The three main strategies are :-
1. Linear Strategy
The default strategy in Ansible is linear. With this strategy tasks are executed on all hosts in the order they are defined in the playbook one task at a time.
Example
---
- name: Linear Strategy Example
hosts: all
tasks:
- name: Update package list
apt:
update_cache: yes
- name: Install Apache
apt:
name: apache2
state: present
2. Free Strategy
The free strategy allows tasks to be executed as soon as possible without waiting for other hosts. This can speed up playbook execution but can be less predictable.
Example
---
- name: Free Strategy Example
hosts: all
strategy: free
tasks:
- name: Update package list
apt:
update_cache: yes
- name: Install Apache
apt:
name: apache2
state: present
3. Serial Strategy
The serial strategy allows you to control the number of hosts that execute a task at a time. This is useful for rolling updates where you want to update a few hosts at a time to minimize downtime.
Example
---
- name: Serial Strategy Example
hosts: webservers
strategy: serial
tasks:
- name: Update package list
apt:
update_cache: yes
- name: Install Apache
apt:
name: apache2
state: present
Configuring Handlers
Handlers in Ansible are used to respond to changes made by tasks. For example : you might want to restart a service if a configuration file is modified. Handlers are only executed when they are notified by a task.
Example Playbook with Handlers
---
- name: Install and Configure Apache
hosts: webservers
tasks:
- name: Install Apache
apt:
name: apache2
state: present
notify:
- Restart Apache
- name: Copy Apache configuration file
copy:
src: apache2.conf
dest: /etc/apache2/apache2.conf
notify:
- Restart Apache
handlers:
- name: Restart Apache
service:
name: apache2
state: restarted
In this example the Restart Apache
handler will be triggered if either the Apache package is installed or the configuration file is modified.
Using Tags
Tags allow you to run specific parts of your playbooks selectively. This is useful for testing or when you need to rerun only a subset of tasks.
Example Playbook with Tags
---
- name: Playbook with Tags
hosts: webservers
tasks:
- name: Update package list
apt:
update_cache: yes
tags: update
- name: Install Apache
apt:
name: apache2
state: present
tags: install
- name: Start Apache
service:
name: apache2
state: started
tags: start
Running Playbook with Tags
To run only the tasks tagged with install
:-
ansible-playbook -i inventory.ini playbook_with_tags.yml --tags "install"
Implementing Rolling Updates with Serial Strategy
Rolling updates are essential for minimizing downtime during updates or deployments. The serial strategy is particularly useful for rolling updates as it allows you to control how many hosts are updated at a time.
Example Playbook for Rolling Updates
---
- name: Rolling Update Web Servers
hosts: webservers
strategy: serial
serial: 2
tasks:
- name: Update package list
apt:
update_cache: yes
- name: Install latest Apache
apt:
name: apache2
state: latest
notify: Restart Apache
handlers:
- name: Restart Apache
service:
name: apache2
state: restarted
In this example the serial: 2
directive ensures that only two hosts are updated at a time. This can help reduce the impact on your infrastructure during updates.
Example Commands
Running a Playbook
To run the example playbook with the linear strategy :-
ansible-playbook -i inventory.ini linear_strategy_example.yml
Running a Playbook with Free Strategy
ansible-playbook -i inventory.ini free_strategy_example.yml
Running a Playbook with Serial Strategy
ansible-playbook -i inventory.ini serial_strategy_example.yml
Running a Playbook with Handlers
ansible-playbook -i inventory.ini install_configure_apache.yml
Running a Playbook with Tags
To run only the tasks tagged with install
:-
ansible-playbook -i inventory.ini playbook_with_tags.yml --tags "install"
Conclusion
Ansible playbooks are a powerful tool for automating the management of your infrastructure. By understanding and utilizing advanced playbook strategies and handlers you can create more efficient, reliable and maintainable automation scripts. The linear, free and serial strategies each offer unique advantages depending on your needs while handlers provide a way to manage services and other stateful resources. Tags offer flexibility in running specific parts of your playbooks and rolling updates ensure minimal downtime during updates.
By mastering these advanced features you can significantly improve your automation workflows, reduce downtime and maintain a more resilient and scalable infrastructure. Let's continue exploring the capabilities of Ansible in future posts diving deeper into advanced topics and real-world use cases to further enhance your automation skills.