How to Accelerate Ansible playbook Speed: Anytime faced or observed, the ansible playbook executing for long time? Does that slowing your productivity or kept to wait for the result? Then it could be there is some configuration issue, or you may using default configuration. If you able to tweak the ansible configuration, it may reduce the time sometime ~10x, again please note some case we may should accept the timing, that may be intentional or actual time for execution. Let’s check how to How to increase the ansible playbook execution Speed.
Before we are starting, we should know ansible configuration file, By Default ansible configuration path:
/etc/ansible/ansible.cfg
If you think the default configuration was not used, you can identify the ansible configuration path via,
ansible@motoskia:~# ansible --version
ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0]
as a best practice’s recommendation, always take a backup if the config file, before you may the changes, this will be important if we need to roll-back the changes to any reason.
Fork:
You may see the number node execution may limited, that also one of the key reasons for slow. If you have more than 100 nodes, number the patch of 5 may take time to complete all the nodes. To improve that, fork is the one we should check in ansible configuration. The fork is the default parallel number of the process while communicating to remote nodes. The default value is 5. This number can be increased from 5 to 500 depends on the ansible node configuration.
ansible@motoskia:~# cat /etc/ansible/ansible.cfg | grep forks
forks = 25
I have set the number of forks to 25. You need to find out the exact value by re-running the job after changing the number of forks.
Strategies
Strategies are a way to control play execution. By default, plays run with a linear strategy, in which all hosts will run each task before any host starts the next task, using the number of forks (default 5) to parallelize.
The serial directive can ‘batch’ this behavior to a subset of the hosts, which then run to completion of the play before the next ‘batch’ starts.
Check: How to write a Ansible Playbook – Explained – FoxuTech
Let’s assume, you have ~100 servers in your inventory and few of those have an issue with repo or network issue or something, in that case ansible doesn’t wait for those VMs to execute the tasks. It will just ignore those issue nodes and continuous the task execution on others.
root@motoskia:~# cat test.yaml
---
- hosts: all
strategy: free
tasks:
- name: Install Tomcat packages
apt:
name: nginx
state: present
Disable Gather Facts:
Disabling facts could save a lot of time when you run the playbook against the group of servers. But if you have used ansible variables in the playbook, you can’t disable the facts.
Here is the sample playbook in which facts are disabled.
---
- hosts: all
strategy: free
gather_facts: no
tasks:
- name: Install nginx
apt:
name: nginx
state: present
Pipelining:
Pipelining is the modern Ansible method of speeding up your ssh connections across the network to the managed hosts. It replaces the former Accelerated Mode. It reduces the number of ssh operations required to execute a module by executing many Ansible modules without an actual file transfer.
SSH pipelining is used to reduce the number of SSH connections to the host machine to one per task. The module execution is done by passing the instructions to the host via SSH. The instructions are written directly onto the STDIN channel.
Pipelining can be enabled in ansible.cfg.
root@motoskia:~# cat /etc/ansible/ansible.cfg |grep -i pipe
# Enabling pipelining reduces the number of SSH operations required to
pipelining = True
It is always recommended to test all this suggestion in non-prod or testing hosts before using in production. Please note, Increasing the number of forks or disabling gather_facts are depended on the ansible controller machine’s configuration.
Check: How to Run a ansible playbook using jenkins – FoxuTech