Ansible is a very powerful automation tool. Many people use it to automate server configuration, cloud provisioning (such as openstack), network (routers/switches) configuration, application deployment, intra-service orchestration, and many other IT needs. Ansible uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs in a way that approaches plain English.
One of the Ansible’s key strength is that it is agentless. You don’t have to install anything on the remote host. Ansible connects to the remote host using ssh, and then executes the required tasks/playbooks. This makes Anisble very easy to use and very popular with many IT engineers. However, using ssh connection can make Ansible slow compared to other automation devops tools like puppet and chef.
In this blog, I will give some tips on how to improve your Ansible performance and execution speed.
Here are 5 things to do that you make Ansible look like it is on steroids:
1) Disable gather_facts functions:
When you run an Ansible playbook, by default it will gather facts of operating system first and then execute tasks listed in it. Its always not required to gather facts & if we have too many hosts it will take more time to execute a playbook. To mitigate this, you may need to disable gather facts with “gather_facts” attribute in ansible playbook set to False. Here is an example:
– hosts: all
– name: Hello
shell: “echo hello”
2) Enable SSH pipelining
Enabling pipelining reduces the number of SSH operations required to execute a module on the remote server, by executing many ansible modules without actual file transfer. This can result in a very significant performance improvement when enabled; at least 4 connections for each module. However, this is compatible with sudo commands. So use this step with cautions. If you plan to use sudo operations, then you must first disable ‘requiretty’ in /etc/sudoers on all managed hosts.
By default, this option is disabled (set to False) to preserve compatibility with sudoers configurations. To enable it, just change the pipelining setting to True
pipelining = True
3) Increase the fork number
Ansible works by spinning off forks of itself and talking to many remote systems independently.
The forks parameter controls how many hosts are configured by Ansible in parallel. By default, the forks parameter in Ansible is a very conservative 5. This means that only 5 hosts will be configured at the same time, and it’s expected that every user will change this parameter to something more suitable for their environment. A good value might be 25 or even 100.
Note: Ansible is smart – for example, if you have 50 systems and set forks to 500, Ansible will only spin up 50 forks because it knows it doesn’t need all 500.
4) Upgrade ssh
Always make sure that your ssh is upgraded to the latest version.
5) Enable ControlPersist
The default connection for Ansible is native OpenSSH. This supports “ControlPersist” which allows for keeping ansible connections open subject to a configurable timeout. This timeout is configurable in ansible.cfg. You will probably want to adjust the control persist timeout, 30 minutes may, for example, be a good value.
By doing all the steps above, your Ansible will much faster especially you run a large scale environment.