14-Day5-3-UnderstandingAndConfiguringExecutionStrategies
Day 5: Lab Series 3 - Understanding and Configuring Execution Strategies
Objective:
This lab introduces participants to execution strategies in Ansible. Participants will understand how forks
, serial
, and throttle
settings affect task execution and learn to configure these strategies for optimal resource usage and control.
Lab 3.1: Configuring Forks
Scenario:
Optimize parallel task execution by increasing the number of forks.
Steps:
Default Forks Setting: Run a playbook with the default
forks
setting.--- - name: Test Default Forks hosts: all tasks: - name: Ping all hosts ping:
Command to run:
ansible-playbook forks_example.yaml -i inventory
Observe the speed of execution for multiple hosts.
Increase Forks:
Set a higher
forks
value in the playbook execution command:ansible-playbook forks_example.yaml -i inventory --forks 10
Exercise:
Compare the execution time with different
forks
values (e.g., 1, 5, 10).Test with a larger inventory file to see the impact.
Lab 3.2: Using Serial Execution
Scenario:
Control task execution by running tasks on a limited number of hosts at a time.
Steps:
Playbook with
serial
:Save as
serial_example.yaml
:--- - name: Execute Tasks Serially hosts: all serial: 2 tasks: - name: Display Hostname debug: msg: "Task running on {{ inventory_hostname }}"
Run the Playbook:
ansible-playbook serial_example.yaml -i inventory
Exercise:
Modify the
serial
value to1
,2
, or5
, and observe the behavior.Identify scenarios where serial execution might be beneficial (e.g., rolling updates).
Lab 3.3: Using Throttle for Task Control
Scenario:
Limit concurrent execution of a specific task using the throttle
keyword.
Steps:
Playbook with Throttling:
Save as
throttle_example.yaml
:--- - name: Throttle Specific Task Execution hosts: all tasks: - name: Install Packages yum: name: httpd state: present throttle: 3
Run the Playbook:
ansible-playbook throttle_example.yaml -i inventory
Exercise:
Experiment with different throttle values (e.g., 1, 3, 5).
Compare how throttling differs from
serial
.
Lab 3.4: Combining Strategies
Scenario:
Use serial
, throttle
, and forks
together to configure complex execution strategies.
Steps:
Playbook with Multiple Strategies:
Save as
combined_example.yaml
:--- - name: Test Combined Strategies hosts: all serial: 2 tasks: - name: Ping Hosts ping: - name: Deploy Apache yum: name: httpd state: present throttle: 1
Run the Playbook:
ansible-playbook combined_example.yaml -i inventory --forks 5
Exercise:
Analyze how
serial
,throttle
, andforks
interact.Test with a larger inventory file for deeper insights.
Optional Lab: Debugging Execution Strategies
Scenario:
Test execution strategies in failure scenarios.
Modify a playbook to deliberately fail on a few hosts (e.g., by using incorrect package names or unreachable hosts).
Run the playbook with different strategies:
serial
throttle
forks
Observe the behavior and learn how each strategy handles errors.
Key Learning Points:
Forks: Determine how to optimize parallel execution for large inventories.
Serial Execution: Control rolling updates or phased deployments with
serial
.Throttling: Limit resource-intensive tasks to a subset of hosts.
Combining Strategies: Mix execution strategies to balance speed and control in complex deployments.
These exercises empower participants to effectively manage execution flow in diverse scenarios.
Last updated