Services DevOps DevSecOps Cloud Consulting Infrastructure Automation Managed Services AIOps MLOps DataOps Microservices 🔐 Private AINEW Solutions DevOps Transformation CI/CD Automation Platform Engineering Security Automation Zero Trust Security Compliance Automation Cloud Migration Kubernetes Migration Cloud Cost Optimisation AI-Powered Operations Data Platform Modernisation SRE & Observability Legacy Modernisation Managed IT Services 🔐 Private AI DeploymentNEW Products ✨ ZippyOPS AINEW 🛡️ ArmorPlane 🔒 DevSecOpsAsService 🖥️ LabAsService 🤝 Collab 🧪 SandboxAsService 🎬 DemoAsService Bootcamp 🔄 DevOps Bootcamp ☁️ Cloud Engineering 🔒 DevSecOps 🛡️ Cloud Security ⚙️ Infrastructure Automation 📡 SRE & Observability 🤖 AIOps & MLOps 🧠 AI Engineering 🎓 ZOLS — Free Learning Company About Us Projects Careers Get in Touch

Boost Ansible Playbook Execution Speed: Key Tips & Tricks

Boost Ansible Playbook Execution Speed: Key Tips & Tricks

If you’re running an Ansible playbook across more than 100 hosts, you might notice a significant slowdown without the right optimization. Fortunately, there are several tuning parameters in Ansible that can dramatically improve playbook execution speed. By fine-tuning these settings, one user reduced execution time from 2 hours to just 40 minutes.

In this guide, we’ll explore key techniques to accelerate your Ansible playbook performance, including adjusting the number of forks, switching strategies, disabling unnecessary tasks, and enabling pipelining. Whether you are working with a few servers or hundreds, these optimizations will help you complete tasks faster and more efficiently.

Ansible playbook execution optimization settings in configuration file

1. Adjust Forks for Faster Execution

In Ansible, “forks” refer to the number of parallel processes that the engine uses when communicating with remote nodes. By default, this number is set to 5. However, for larger deployments, increasing the number of forks can significantly speed up task execution across all nodes.

If you’re working with a large inventory, consider raising the fork count to a higher value, such as 50 or even 100, depending on your server resources. This ensures that tasks are distributed across nodes more quickly, reducing the overall execution time.

To adjust the forks in your configuration file (/etc/ansible/ansible.cfg), modify the following line:

# Increase the fork count
forks = 50

This change can lead to a much faster execution, especially when dealing with a large number of hosts.

2. Use the “Free” Strategy for Parallel Execution

By default, Ansible uses a linear strategy to execute playbooks. This means that tasks are executed one by one on each host, with no parallelism beyond the default fork limit. However, Ansible also offers the “free” strategy, which allows each host to proceed independently with its tasks, improving efficiency.

The “free” strategy is especially helpful when some hosts in your inventory are experiencing issues. Instead of waiting for a failing host to complete its task, the playbook will continue executing on the other hosts, speeding up the overall process.

Here’s an example of a playbook that uses the “free” strategy:

---
- hosts: all
  strategy: free
  gather_facts: no
  tasks:
    - name: Install Nginx package
      yum:
        name: nginx
        state: present

By using this strategy, your playbook can complete faster, even if some nodes encounter issues.

3. Disable Fact Gathering for Faster Performance

Fact gathering is an essential step in Ansible’s workflow, but it can also slow down the execution time, especially when running a playbook on many hosts. If you’re not using variables that depend on facts, you can disable this step to save time.

To disable fact gathering, add the following line to your playbook:

---
- hosts: all
  gather_facts: no
  strategy: free
  tasks:
    - name: Install Nginx package
      yum:
        name: nginx
        state: present

Disabling fact gathering is particularly beneficial for large-scale deployments, where the time spent gathering facts can add up quickly.

4. Enable Pipelining to Reduce SSH Operations in Ansible playbook

Pipelining is an advanced optimization technique that reduces the number of SSH connections required during playbook execution. By enabling pipelining, Ansible combines multiple SSH operations into one, reducing overhead and speeding up the process.

To enable pipelining, modify your Ansible configuration file (/etc/ansible/ansible.cfg) as follows:

# Enable pipelining
pipelining = True

With pipelining enabled, Ansible sends modules and executes tasks using fewer SSH connections, leading to faster playbook execution times.

Example: Comparing Execution Time Before and After Pipelining

Without pipelining:

$ ansible-playbook -i inventory tomcat.yaml
PLAY [all] ********************************************************************************

TASK [Install Tomcat packages] *******************************************************************************************
ok: [192.168.1.4]
ok: [192.168.1.4]

PLAY RECAP ********************************************************************************
192.168.1.4 : ok=1 changed=0 unreachable=0 failed=0

real 0m10.300s
user 0m4.246s
sys 0m2.536s

With pipelining:

$ ansible-playbook -i inventory strategy_test.yaml
PLAY [all] ********************************************************************************

TASK [Install Tomcat packages] *******************************************************************************************
ok: [192.168.1.4]
ok: [192.168.1.4]

PLAY RECAP ********************************************************************************
192.168.1.4 : ok=1 changed=0 unreachable=0 failed=0

real 0m5.390s
user 0m1.246s
sys 0m0.506s

As shown, enabling pipelining can significantly reduce the execution time by minimizing SSH overhead.

5. Implement ZippyOPS Solutions for Enhanced Automation

For organizations looking to further optimize their infrastructure, ZippyOPS offers a suite of DevOps, DataOps, and Cloud solutions that integrate seamlessly with Ansible. Whether you’re looking for consulting, implementation, or managed services, ZippyOPS can help streamline your automation processes and improve operational efficiency.

ZippyOPS specializes in areas like DevSecOps, Automated Ops, AIOps, MLOps, and microservices architecture. With their tailored solutions, businesses can accelerate deployment times and maintain higher operational uptime.

To explore how ZippyOPS can help optimize your automation processes, visit their services page or reach out directly at sales@zippyops.com.

Conclusion: Speed Up Your Ansible Playbook with These Tips

By adjusting key settings like forks, strategies, and pipelining, you can dramatically improve your Ansible playbook execution speed. These optimizations ensure that your playbooks run faster, even when working with large inventories.

For advanced solutions in automation and cloud infrastructure, ZippyOPS offers expert consulting, implementation, and managed services. Explore their solutions or get in touch to learn more about how they can enhance your operations.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top