Site icon UnixArena

How to Boost Ansible playbook Speed ?

Improve Ansible Playbook Speed - Boost

How to 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 4hours to 40mins. Let’s tweak your Ansible settings to achieve faster throughput.

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

 

How to identify your ansible configuration file? Execute the ansible version check command.

[linadm@ansible-server .ssh]$ ansible --version
ansible 2.7.10
  config file = /home/linadm/automation/linadm-ansible.cfg
  configured module search path = [u'/home/linadm/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
[linadm@ansible-server .ssh]$

Once you have identified the configuration file, take a backup for rollback and try the following changes to boots the playbook speed.

 

1.  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.

I have set the number of forks to 15. You need to find out the exact value by re-running the job after changing the number of forks.

[linadm@ansible-server .ssh]$ cat /home/linadm/automation/linadm-ansible.cfg |grep -i fork
forks          = 15
[linadm@ansible-server .ssh]$

2. Strategies

By default, ansible play happens using 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 which 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 few of those have an issue with repo or network issue, Ansible engine doesn’t wait for those boxes to execute the tasks. It will just ignore those issue nodes and continuous the task execution on others.

[root@ansible-server ~]# cat strategy_test.yaml
---
- hosts: all
  strategy: free

  tasks:
    - name: Install Tomcat packages
      yum:
       name: tomcat
       state: present
[root@ansible-server ~]#

 

3. 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 ntp packa
      yum:
       name: tomcat
       state: present

 

4. 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. Which means, it does the following action.

  1. Directory creation at the host machine
  2. Transfer of module source
  3. 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.

[root@ansible-server ~]# cat /etc/ansible/ansible.cfg |grep -i pipe
# Enabling pipelining reduces the number of SSH operations required to
pipelining = True

 

Without Pipeline:

[root@ansible-server ~]# time ansible-playbook -i temp strategy_test.yaml --ask-pass
SSH password:

PLAY [all] ********************************************************************************

TASK [Install Tomcat packages] *******************************************************************************************
ok: [192.168.3.165]
ok: [192.168.3.151]

PLAY RECAP ********************************************************************************
192.168.3.151              : ok=1    changed=0    unreachable=0    failed=0
192.168.3.165              : ok=1    changed=0    unreachable=0    failed=0

real    0m11.200s
user    0m3.346s
sys     0m1.437s
[root@ansible-server ~]#

 

After enabling Pipeline:

[root@ansible-server ~]# time ansible-playbook -i temp strategy_test.yaml --ask-pass
SSH password:

PLAY [all] *************************************************************************************
TASK [Install Tomcat packages] ************************************************************************************************
ok: [192.168.3.165]
ok: [192.168.3.151]

PLAY RECAP *************************************************************************************
192.168.3.151              : ok=1    changed=0    unreachable=0    failed=0
192.168.3.165              : ok=1    changed=0    unreachable=0    failed=0

real    0m6.890s
user    0m1.940s
sys     0m0.404s
[root@ansible-server ~]#

 

I would recommend testing each parameter before implementing in real production. Increasing the number of fork value or gather_facts are depended on the ansible controller machine’s configuration. Hope this article is informative to you.

Exit mobile version