04.04.2020 Views

Mastering Ansible

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 1

Task performance

Doing the math from the above description, that's at least three SSH connections

per task, per host. In a small fleet with a small number of tasks, this may not be a

concern; however, as the task set grows and the fleet size grows, the time required to

create and tear down SSH connections increases. Thankfully, there are a couple ways

to mitigate this.

The first is an SSH feature, ControlPersist, which provides a mechanism to create

persistent sockets when first connecting to a remote host that can be reused in

subsequent connections to bypass some of the handshaking required when creating

a connection. This can drastically reduce the amount of time Ansible spends on

opening new connections. Ansible automatically utilizes this feature if the host

platform where Ansible is run from supports it. To check whether your platform

supports this feature, check the SSH main page for ControlPersist.

The second performance enhancement that can be utilized is an Ansible feature

called pipelining. Pipelining is available to SSH-based connection methods and is

configured in the Ansible configuration file within the ssh_connection section:

[ssh_connection]

pipelining=true

This setting changes how modules are transported. Instead of opening an SSH

connection to create a directory, another to write out the composed module, and a

third to execute and clean up, Ansible will instead open an SSH connection and start

the Python interpreter on the remote host. Then, over that live connection, Ansible

will pipe in the composed module code for execution. This reduces the connections

from three to one, which can really add up. By default, pipelining is disabled.

Utilizing the combination of these two performance tweaks can keep your playbooks

nice and fast even as you scale your fleet. However, keep in mind that Ansible will

only address as many hosts at once as the number of forks Ansible is configured

to run. Forks are the number of processes Ansible will split off as a worker to

communicate with remote hosts. The default is five forks, which will address up to

five hosts at once. Raise this number to address more hosts as your fleet grows by

adjusting the forks= parameter in an Ansible configuration file, or by using the –

forks (-f) argument with ansible or ansible-playbook.

[ 25 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!