Introduction

Ansible, an open-source automation tool, simplifies IT infrastructure operations. Whether tackling simple package installations or orchestrating complex clustered solutions, optimizing Ansible playbooks is key to faster execution.

Step by Step

Here are ten methods to enhance Ansible Playbook performance:

1. Identify Slow Tasks with Callback Plugins in ansible.cfg:

  • Use callback plugins like timer, profile_tasks, and profile_roles to assess task execution times. Configure ansible.cfg to enable these plugins and analyze task performance.
  • https://docs.ansible.com/ansible/latest/collections/index_callback.html

2. Disable Fact Gathering in Playbook:

  • Unless ansible_facts are utilized, disable fact gathering in playbooks with gather_facts: False. This improves performance, particularly in larger environments.
  • https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html

3. Configure Parallelism in ansible.cfg:

Adjust the forks setting in ansible.cfg to control task execution batches. Increasing forks from the default 5 allows more parallel task execution, speeding up playbooks.

  • https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_strategies.html#setting-the-number-of-forks

4. Configure SSH Optimization in ansible.cfg:

  • Implement ControlMaster and ControlPersist settings in ansible.cfg to reduce SSH connection overhead, enhancing efficiency.
  • https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html

5. Disable SSH Host Key Checking in ansible.cfg:

  • In controlled environments, disable SSH host key checking by setting host_key_checking to False in ansible.cfg. Exercise caution outside such environments.
  • https://docs.ansible.com/ansible/latest/reference_appendices/config.html#host-key-checking

6. Enable Pipelining in ansible.cfg:

  • Reduce SSH connections by enabling pipelining in ansible.cfg with pipelining = True.
  • https://docs.ansible.com/ansible/latest/reference_appendices/config.html#ansible-pipelining

7. Utilize Execution Strategies in Playbook:

  • Choose an appropriate strategy (e.g., free) to execute tasks without waiting for all hosts to complete their tasks, improving parallelism.
  • https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_strategies.html#selecting-a-strategy

8. Use Async Tasks in Playbook:

  • Employ async mode for long-running tasks, allowing Ansible to proceed with other tasks without waiting.
  • https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_async.html

9. Setting the Batch Size with Serial in Playbook

  • Use the serial statement to customize the batch size of hosts that are concurrently processed during playbook execution. This feature is particularly valuable in scenarios like rolling updates, where you want to limit the number of machines managed simultaneously. The serial value can be configured with specific host counts, percentages, or a mix