Ansible: Boost Task Execution Speed

Running Ansible tasks takes some time to complete. You can greatly reduce this time by using SSH multiplexing and pipelining, and by stopping or caching the fact gathering steps. Let’s discuss these optimizations in detail.

This article originally appeared at my blog.

Improve SSH

  1. Gather facts
  2. Copy zipped playbook code to the node
  3. Execute the playbook code

Enter the following code in the ansible.cfg to do all these steps with one SSH connection:

[ssh_config]
pipelining = true

The second option is to use SSH multiplexing: You open one outgoing TCP connection from which different SSH-connection to other nodes are formed. Ansible uses SSH multiplexing by default, but you may need to make changes to your SSH config, see this multiplexing cookbook.

Run more tasks parallel

[defaults]
forks = 20

You can also run tasks asynchronously, which means Ansible does not wait for a task to finish before the next one. This can be helpful if there is a task which lasts for some time, but is not required for subsequent tasks. Setup and usage of asynchronous task requires some thoughts, see the official documentation.

Use the “free” strategy

[defaults]
strategy = free

Disable or cache fact gathering

- name: playbook
gather_facts: false

To disable it globally, write this in your ansible.cfg :

[defaults]
gathering = explicit

If you need the facts, but are sure that they don’t change too often, you can cache them. To cache facts for 24 hours in a local JSON file, write this in your ansible.cfg.

[defaults]
gathering = smart
fact_caching_timeout = 86400
fact_caching = jsonfile
fact_caching_connection = ./ansible_fact_cache

You can also use other caching plugins like MongoDb, Redis or Memched.

Conclusion

[defaults]
strategy = free
forks = 20
[ssh_config]
pipelining = true

And if you want to skip the time-consuming fact gathering step, then turn on smart gathering:

[defaults]
gathering = smart
fact_caching_timeout = 86400
fact_caching = jsonfile
fact_caching_connection = ./ansible_fact_cache

Have fun with these improvements.

IT Project Manager & Developer