Ilya Ivanov

Ilya Ivanov

DevOps eXtraordinaire

© 2019

Speeding up Ansible

Ansible playbook execution may take quite a while when connection is not configured properly. Here’s a checklist to ensure your Ansible is as fast as possible:

  1. Manage machines from close distance. Latency matters. For example, if you manage an AWS fleet, run Ansible on an EC2 instance also.
  2. Use latest OpenSSH, on both master and slave. That specifically concerns EL6/CentOS6.
  3. In your ~/.ssh/config, enable
    ControlPersist yes
    
  4. Enable pipelining in /etc/ansible/ansible.cfg
    [ssh_connection] pipelining = True
    
  5. On managed machines, disable SSH DNS checks in /etc/ssh/sshd_config:
    UseDNS no
    
  6. Really-really disable SSH DNS checks
- name: set "UseDNS no"
lineinfile:
dest=/etc/ssh/sshd_config
regexp='.*UseDNS.*'
line='UseDNS no'
insertbefore=BOF
backup=yes
notify:
- restart sshd

#rhel
- name: set option "-u0" (el)
lineinfile: dest=/etc/sysconfig/sshd regexp="^OPTIONS" line="OPTIONS='-u0'" create=yes backup=yes
when: ansible_os_family == 'RedHat'
notify:
- restart sshd

#old ubuntu and debian
- name: set option "-u0" (debian /etc/ssh/default)
lineinfile: dest=/etc/default/ssh regexp="^SSHD_OPTS" line="SSHD_OPTS='-u0'" backup=yes
when: ansible_distribution == 'Debian' or (ansible_distribution=='Ubuntu' and ansible_lsb.major_release|int < 12)
notify:
- restart sshd

#new ubuntu
- name: set option "-u0" (ubuntu upstart)
lineinfile: dest=/etc/init/ssh.conf regexp="^exec /usr/sbin/sshd -D" line='exec /usr/sbin/sshd -D -u0' backup=yes
when: ansible_distribution == 'Ubuntu' and ansible_lsb.major_release|int >= 12
notify:
- restart sshd

Now, you should be all set. For playbooks with many small tasks, expected improvement is 2-5 times.