Skip to content

Instantly share code, notes, and snippets.

@lenosi
Last active May 12, 2021 11:41
Show Gist options
  • Save lenosi/53a8139d43f2f218e88e05cc715ec91a to your computer and use it in GitHub Desktop.
Save lenosi/53a8139d43f2f218e88e05cc715ec91a to your computer and use it in GitHub Desktop.
How to speed up Ansible a lot

Introduction

I'm working with Ansible and many of servers every day, so let me show you, how can waste less of your time! :)

What Ansible do? 

  • Connects, runs, check, sends back, processes it, runs, checks, sends back and voila!
  • Connects... again... and again... and again...

What you can do to fix on node/remote server

Edit sshd config and disable UseDNS, it's mainly used only for logging and authentication. But.. only when you have set IgnoreRhosts in your config. So who doesn't know, should use it with clean mind. Because this has truly so far with security. :)

/etc/ssh/sshd_config

UseDNS no

Configure your ssh

Create needed directory:

mkdir ~/.ssh/sockets

~/.ssh/config

 Host *
  ControlMaster auto
  ControlPath ~/.ssh/sockets/%r@%h-%p
  ControlPersist 600
  PreferredAuthentications=publickey

Solve it on Ansbile side

Every additional connection will cost a lot of additional time.. :) The main feature in latest releases of Ansible is pipelining. Try to use this configuration in ansible.cfg.

Follow Ansible documentation (put it into ansbile.cfg)

  • ANSIBLE_CONFIG (an environment variable)
  • ansible.cfg (in the current directory)
  • ~/.ansible.cfg (in the home directory)
  • /etc/ansible/ansible.cfg
[paramiko_connection]
record_host_keys=False
[ssh_connection]
# Tuned ssh args
ssh_args=-C -o ForwardAgent=yes -o StrictHostKeyChecking=no -o ControlMaster=auto -o ControlPersist=600s -o PreferredAuthentications=publickey
control_path = /tmp/ansible-ssh-%%h-%%p-%%r
# By enabling pipelining you get realy better performance
pipelining = True
# And the best what you can do if you have too many nodes... increase forks to 50 or 100+
forks = 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment