Skip to main content

Implementing Strategy Plugins

1. Understanding Strategy Plugins

Plugin Type Description Default Behavior Use Case
strategy Controls how Ansible executes tasks across hosts linear Sequential execution of tasks per host
strategy_plugins Directory where custom strategy plugins are stored ansible/plugins/strategy Extending or overriding default execution logic

Example: Setting Strategy in Playbook

---
- hosts: all
  strategy: debug
  tasks:
    - name: Debug task
      debug:
        msg: "Running with debug strategy"

Key Concepts

  • Linear Strategy: Default, executes tasks sequentially per host
  • Free Strategy: Executes tasks in parallel across all hosts
  • Debug Strategy: Executes tasks one at a time, useful for debugging
Note: Strategy plugins can significantly impact performance and execution order. Choose based on your infrastructure needs.

2. Using Linear Strategy

Option Values Notes
strategy linear Default strategy for Ansible playbooks

Example: Using Linear Strategy in a Playbook

---
- name: Example playbook with linear strategy
  hosts: all
  strategy: linear
  tasks:
    - name: Print hostname
      command: hostname
Note: The linear strategy processes hosts sequentially, one after another, in the order they appear in the inventory.

3. Using Free Strategy

Option Values Notes
strategy free Execute tasks on hosts as soon as possible, without waiting for other hosts

Example: Using Free Strategy in Playbook

---
- hosts: all
  strategy: free
  tasks:
    - name: Print hostname
      command: hostname
Note: The free strategy is the default for most Ansible executions, allowing tasks to run as soon as a host becomes available.

4. Using Debug Strategy

Option Values Notes
strategy debug Strategy plugin for debugging playbooks

Example: Using Debug Strategy in Playbook

- name: Debug strategy example
  hosts: all
  strategy: debug
  tasks:
    - name: Debug task
      debug:
        msg: "This is a debug message"
Note: The debug strategy is primarily used for development and testing purposes to inspect playbook execution flow.

5. Using Host Pinned Strategy

Option Values Notes
strategy host_pinned Strategy plugin to execute tasks on the controlling host

Example: Using host_pinned strategy in a playbook

---
- hosts: all
  strategy: host_pinned
  tasks:
    - name: Display host info
      debug:
        msg: "Running on {{ ansible_hostname }}"
Note: The host_pinned strategy ensures that all tasks are executed on the controlling host, which can be useful for managing local operations or when specific host-level control is required.

6. Configuring Strategy Options

Option Values Description
strategy linear, free, host_pinned Determines how tasks are executed across hosts
forks 1 to 1000 Number of parallel processes to use for task execution
serial 1 to 1000 Number of hosts to process in serial, useful for rolling updates

Example: Setting Strategy in Playbook

---
- hosts: all
  strategy: free
  forks: 5
  serial: 2
  tasks:
    - name: "Install nginx"
      apt:
        name: nginx
        state: present
Note: The strategy option affects how Ansible distributes tasks across hosts. linear processes hosts sequentially, free allows tasks to run on any host as soon as it's available, and host_pinned ensures tasks are pinned to the same host for consistent state handling.

7. Setting Serial Execution

Option Values Description
serial 1 Number of hosts to execute on in parallel. Default is 0 (all hosts).
serial 5 Execute on 5 hosts at a time.
serial 25% Execute on 25% of hosts at a time.

Example: Serial Execution in Playbook

---
- name: Deploy application with serial execution
  hosts: webservers
  serial: 2
  tasks:
    - name: Update application
      command: systemctl restart app.service
Note: The serial parameter is useful for rolling updates or when you want to avoid downtime during deployments.

8. Using Forks

Option Values Description
forks 5 Number of parallel processes to use for task execution

Example: Setting forks in playbook

---
- hosts: all
  forks: 10
  tasks:
    - name: Example task
      debug:
        msg: "Running on {{ inventory_hostname }}"
Note: The forks setting controls how many parallel processes Ansible uses to execute tasks. Increasing this value can speed up execution on large inventories, but may also increase system load.

9. Combining Strategy and Serial

Strategy Description Usage
free Executes tasks as soon as possible on any available host Default strategy; good for parallel execution
host-pinned Assigns tasks to specific hosts and keeps them pinned Useful for stateful operations or when host affinity is required
linear Executes tasks sequentially on each host in order Best for ordered execution or when dependencies exist between tasks
debug Displays detailed execution information for debugging Useful during development or troubleshooting

Example: Using linear strategy with serial

---
- name: Linear execution with serial
  hosts: all
  strategy: linear
  serial: 2
  tasks:
    - name: Task 1
      debug:
        msg: "Running task 1"
    - name: Task 2
      debug:
        msg: "Running task 2"
Note: The serial parameter limits how many hosts are processed at once, even when using a strategy like free. When combined with linear, it ensures tasks are executed in order, one host at a time.

10. Creating Custom Strategies

Example: Basic Custom Strategy Plugin

from ansible.plugins.strategy import StrategyBase
from ansible.executor.play_iterator import PlayIterator

class StrategyModule(StrategyBase):
    name = 'custom_strategy'

    def run(self, play_context, iterator, result):
        # Custom logic here
        return result
Property Description Default Value
name Unique identifier for the strategy plugin strategy
run Main execution method for the strategy Required
Note: Strategy plugins must inherit from StrategyBase and implement the run method.
Warning: Custom strategies can significantly alter playbook execution flow and should be tested thoroughly.

Was this article helpful?