Boost ansible playbook speed

Increase the ansible-playbook execution speed. One who is running the playbook against more than 100 hosts could witness the slowness if it’s not fine-tuned. Ansible has few configurable parameters which can reduce the playbook execution time significantly. As a result of the fine-tuning ansible, playbook execution time has been reduced from 2hours to 40mins

The default configuration file
#/etc/ansible/ansible.cfg

Fork
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. If you have a large number of hosts, higher values will make actions across all of those hosts complete faster. The default is very very conservative.
# cat /etc/ansible/ansible.cfg | grep -i fork
#forks = 5

Strategies
By default, ansible play happens using 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. Strategies are a way to control ansible play execution. “free” option allows each host to run until the end of the play as fast as it can. The following is the sample playbook which follows the strategy-free workflow. If I have 100 servers in my inventory and a few of those have an issue with repo or network issue, the Ansible engine doesn’t wait for those boxes to execute the tasks. It will just ignore those issue nodes and continue the task execution on others.
cat strategy_test.yaml
---
- hosts: all
strategy: free
tasks:
name: install nginx package
yum:
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.
---
- hosts: all
strategy: free
gather_facts: no
tasks:
name: install nginx package
yum:
name: nginx
state: present

Pipelining
Pipelining is a replacement for the older accelerated mode option in Ansible. Ansible engine sends modules over an SSH connection from the controller to the host machine. This means it does the following action.
Directory creation at the host machine
Transfer of module source
Execution of code

Pipelining just 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.
# cat /etc/ansible/ansible.cfg |grep -i pipe
# Enabling pipelining reduces the number of SSH operations required to
pipelining = True
# * piped = use 'dd' over SSH to transfer files

Without pipeline
# 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
192.168.1.4 : ok=1 changed=0 unreachable=0 failed=0

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

After enabling Pipeline
# 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
192.168.1.4 : ok=1 changed=0 unreachable=0 failed=0

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



Recent Comments

No comments

Leave a Comment