Skip to content

Performance Tuning

Overview

Learn how to optimize your Ansible playbooks for maximum performance across large-scale deployments.

Key Performance Strategies

Parallel Execution

# Example of parallel execution configuration
- hosts: all
  strategy: free
  serial: 20%  # Run on 20% of hosts at a time
  tasks:
    - name: Update packages
      apt:
        update_cache: yes

SSH Multiplexing

Add to your ansible.cfg:

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
pipelining = True

Fact Caching

[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /path/to/cache
fact_caching_timeout = 86400

Optimization Techniques

1. Task Optimization

  • Use loop instead of with_items
  • Minimize unnecessary tasks
  • Use async for long-running operations
- name: Long running operation with async
  command: /usr/bin/long_operation
  async: 3600
  poll: 0
  register: long_operation_result

2. Inventory Optimization

  • Group hosts effectively
  • Use dynamic inventory when possible
  • Implement fact caching

3. Module Performance

# Efficient module usage
- name: Bulk package installation
  apt:
    name: "{{ packages }}"
    state: present
  vars:
    packages:
      - nginx
      - postgresql
      - redis-server

4. Network Optimization

  • Enable SSH pipelining
  • Use connection pooling
  • Optimize fact gathering

Monitoring Performance

Ansible Callback Plugins

# ansible.cfg
[defaults]
callback_plugins = timer, profile_tasks

Profiling Tools

  • ansible-profile
  • ansible-playbook --timing

Common Bottlenecks

  1. Network Latency
  2. Fact Gathering
  3. Task Execution
  4. Template Rendering

Best Practices Checklist

  • [ ] Enable SSH multiplexing
  • [ ] Configure fact caching
  • [ ] Use bulk operations
  • [ ] Implement parallel execution
  • [ ] Monitor task timing
  • [ ] Optimize inventory structure
  • [ ] Regular performance testing