10 Proven Methods to Optimize Ansible Playbook Performance
By Luca Berton · Published 2024-01-01 · Category: installation
Discover 10 practical techniques to boost Ansible Playbook performance, from disabling fact gathering to optimizing SSH settings for faster execution.

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.See also: Maximize Ansible Efficiency with Callback Plugins for Resource Monitoring
Step by Step
Here are ten methods to enhance Ansible Playbook performance:
- Identify Slow Tasks with Callback Plugins in
ansible.cfg:
- Use callback plugins like
timer,profile_tasks, andprofile_rolesto assess task execution times. Configureansible.cfgto enable these plugins and analyze task performance. - https://docs.ansible.com/ansible/latest/collections/index_callback.html
- Disable Fact Gathering in Playbook:
- Unless
ansible_factsare utilized, disable fact gathering in playbooks withgather_facts: False. This improves performance, particularly in larger environments.
- https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_vars_facts.html
- Configure Parallelism in ansible.cfg:
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
- Configure SSH Optimization in ansible.cfg:
- Implement
ControlMasterandControlPersistsettings inansible.cfgto reduce SSH connection overhead, enhancing efficiency. - https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html
- Disable SSH Host Key Checking in ansible.cfg:
- In controlled environments, disable SSH host key checking by setting
host_key_checkingtoFalseinansible.cfg. Exercise caution outside such environments. - https://docs.ansible.com/ansible/latest/reference_appendices/config.html#host-key-checking
- Enable Pipelining in ansible.cfg:
- Reduce SSH connections by enabling pipelining in
ansible.cfgwithpipelining = True. - https://docs.ansible.com/ansible/latest/reference_appendices/config.html#ansible-pipelining
- 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
- Use Async Tasks in Playbook:
- Employ
asyncmode for long-running tasks, allowing Ansible to proceed with other tasks without waiting. - https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_async.html
- Setting the Batch Size with Serial in Playbook
- Use the
serialstatement 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. Theserialvalue can be configured with specific host counts, percentages, or a mix of both, providing flexibility in optimizing task execution. It allows users to fine-tune the scope of failures, ensuring that issues are isolated to individual batches rather than affecting the entire host list. This capability enhances playbook performance and control in diverse infrastructure automation scenarios. - https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_strategies.html#setting-the-batch-size-with-serial
- Use Paramiko in ansible.cfg
- Using Paramiko, the native Python SSH implementation, as a transport method in Ansible, especially on older platforms like RHEL6 and earlier without
ControlPersistsupport in SSH, offers compatibility and efficiency benefits. Paramiko ensures faster and more efficient connections for users on these platforms, enhancing Ansible’s performance, and is seamlessly integrated into Ansible’ssmartoption for thetransportoption in theansible.cfg, which automatically selects the appropriate transport method based on the controller’s OS and SSH versions, simplifying connection management in diverse environments. - https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-transport
Conclusion
Optimizing Ansible playbooks is a continuous journey. Experiment with various configuration parameters to find the ideal combination for your infrastructure needs. Additional parameters like serial, throttle, and run_once offer further optimization options. Refer to Ansible documentation for comprehensive guidance tailored to your environment.
Related Articles
Category: installation