How to increase the number of connection attempts when using ansible

Increase the number of connection attempts when using ansible utility.

How it works

By default only one connection attempt is made.

$ ansible-playbook -i hosts playbook.yml -vv
ansible-playbook 2.9.6
  config file = /home/milosz/Projekty/ansible/ansible.cfg
  configured module search path = ['/home/milosz/Projekty/ansible/library']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
Using /home/milosz/Projekty/ansible/ansible.cfg as config file

PLAYBOOK: playbook.yml ******************************************************************************************************************
1 plays in playbook.yml

PLAY [all] ******************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************
task path: /home/milosz/Projekty/ansible/playbook.yml:2
 remaining tries: 1
 attempt: 1
 cmd: [b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b'/bin/sh -c \'echo PLATFORM; uname; echo FOUND; command -v \'"\'"\'/usr/bin/python\'"\'"\'; command -v \'"\'"\'python3.7\'"\'"\'; command -v \'"\'"\'python3.6\'"\'"\'; command -v \'"\'"\'python3.5\'"\'"\'; command -v \'"\'"\'python2.7\'"\'"\'; command -v \'"\'"\'python2.6\'"\'"\'; command -v \'"\'"\'/usr/libexec/platform-python\'"\'"\'; command -v \'"\'"\'/usr/bin/python3\'"\'"\'; command -v \'"\'"\'python\'"\'"\'; echo ENDFOUND && sleep 0\'']
[WARNING]: Unhandled error in Python interpreter discovery for host 127.0.0.1: Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 22: Connection refused
 remaining tries: 1
 attempt: 1
 cmd: [b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b"/bin/sh -c '/usr/bin/python && sleep 0'"]
fatal: [127.0.0.1]: UNREACHABLE! => {"changed": false, "msg": "Data could not be sent to remote host \"127.0.0.1\". Make sure this host can be reached over ssh: ssh: connect to host 127.0.0.1 port 22: Connection refused\r\n", "unreachable": true}

PLAY RECAP ******************************************************************************************************************************
127.0.0.1                  : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0   

Use ANSIBLE_SSH_RETRIES environment variable to perform an additional connection attempt.

$ ANSIBLE_SSH_RETRIES=1 ansible-playbook -i hosts playbook.yml -vv
ansible-playbook 2.9.6
  config file = /home/milosz/Projekty/ansible/ansible.cfg
  configured module search path = ['/home/milosz/Projekty/ansible/library']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
Using /home/milosz/Projekty/ansible/ansible.cfg as config file

PLAYBOOK: playbook.yml ******************************************************************************************************************
1 plays in playbook.yml

PLAY [all] ******************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************
task path: /home/milosz/Projekty/ansible/playbook.yml:2
 remaining tries: 2
 attempt: 1, pause: 0
<127.0.0.1> ssh_retry: attempt: 1, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b'/bin/sh -c \'echo PLATFORM; uname; echo FOUND; command -v \'"\'"\'/usr/bin/python\'"\'"\'; command -v \'"\'"\'python3.7\'"\'"\'; command -v \'"\'"\'python3.6\'"\'"\'; command -v \'"\'"\'python3.5\'"\'"\'; command -v \'"\'"\'python2.7\'"\'"\'; command -v \'"\'"\'python2.6\'"\'"\'; command -v \'"\'"\'/usr/libexec/platform-python\'"\'"\'; command -v \'"\'"\'/usr/bin/python3\'"\'"\'; command -v \'"\'"\'python\'"\'"\'; echo ENDFOUND && sleep 0\'']...), pausing for 0 seconds
 attempt: 2
[WARNING]: Unhandled error in Python interpreter discovery for host 127.0.0.1: Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 22: Connection refused
 remaining tries: 2
 attempt: 1, pause: 0
<127.0.0.1> ssh_retry: attempt: 1, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b"/bin/sh -c '/usr/bin/python && sleep 0'"]...), pausing for 0 seconds
 attempt: 2
fatal: [127.0.0.1]: UNREACHABLE! => {"changed": false, "msg": "Data could not be sent to remote host \"127.0.0.1\". Make sure this host can be reached over ssh: ssh: connect to host 127.0.0.1 port 22: Connection refused\r\n", "unreachable": true}

PLAY RECAP ******************************************************************************************************************************
127.0.0.1                  : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0   

Increasing ANSIBLE_SSH_RETRIES environment variable further will introduce an additional pause between each connection attempt. The algorithm that determines pause time is quite simple.

pause = 2 ** attempt - 1
if pause > 30:
    pause = 30

This table illustrates it further.

Connection attempt 1 2 3 4 5 6 7 ...
ANSIBLE_SSH_RETRIES 0 (default) 1 2 3 4 5 6 ...
Pause after connection attempt 0 1 3 7 15 30 30 30

Execution example.

$ ANSIBLE_SSH_RETRIES=7 ansible-playbook -i hosts playbook.yml -vv
ansible-playbook 2.9.6
  config file = /home/milosz/Projekty/ansible/ansible.cfg
  configured module search path = ['/home/milosz/Projekty/ansible/library']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]
Using /home/milosz/Projekty/ansible/ansible.cfg as config file

PLAYBOOK: playbook.yml ******************************************************************************************************************
1 plays in playbook.yml

PLAY [all] ******************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************
task path: /home/milosz/Projekty/ansible/playbook.yml:2
 remaining tries: 8
 attempt: 1, pause: 0
<127.0.0.1> ssh_retry: attempt: 1, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b'/bin/sh -c \'echo PLATFORM; uname; echo FOUND; command -v \'"\'"\'/usr/bin/python\'"\'"\'; command -v \'"\'"\'python3.7\'"\'"\'; command -v \'"\'"\'python3.6\'"\'"\'; command -v \'"\'"\'python3.5\'"\'"\'; command -v \'"\'"\'python2.7\'"\'"\'; command -v \'"\'"\'python2.6\'"\'"\'; command -v \'"\'"\'/usr/libexec/platform-python\'"\'"\'; command -v \'"\'"\'/usr/bin/python3\'"\'"\'; command -v \'"\'"\'python\'"\'"\'; echo ENDFOUND && sleep 0\'']...), pausing for 0 seconds
 attempt: 2, pause: 1
<127.0.0.1> ssh_retry: attempt: 2, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b'/bin/sh -c \'echo PLATFORM; uname; echo FOUND; command -v \'"\'"\'/usr/bin/python\'"\'"\'; command -v \'"\'"\'python3.7\'"\'"\'; command -v \'"\'"\'python3.6\'"\'"\'; command -v \'"\'"\'python3.5\'"\'"\'; command -v \'"\'"\'python2.7\'"\'"\'; command -v \'"\'"\'python2.6\'"\'"\'; command -v \'"\'"\'/usr/libexec/platform-python\'"\'"\'; command -v \'"\'"\'/usr/bin/python3\'"\'"\'; command -v \'"\'"\'python\'"\'"\'; echo ENDFOUND && sleep 0\'']...), pausing for 1 seconds
 attempt: 3, pause: 3
<127.0.0.1> ssh_retry: attempt: 3, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b'/bin/sh -c \'echo PLATFORM; uname; echo FOUND; command -v \'"\'"\'/usr/bin/python\'"\'"\'; command -v \'"\'"\'python3.7\'"\'"\'; command -v \'"\'"\'python3.6\'"\'"\'; command -v \'"\'"\'python3.5\'"\'"\'; command -v \'"\'"\'python2.7\'"\'"\'; command -v \'"\'"\'python2.6\'"\'"\'; command -v \'"\'"\'/usr/libexec/platform-python\'"\'"\'; command -v \'"\'"\'/usr/bin/python3\'"\'"\'; command -v \'"\'"\'python\'"\'"\'; echo ENDFOUND && sleep 0\'']...), pausing for 3 seconds
 attempt: 4, pause: 7
<127.0.0.1> ssh_retry: attempt: 4, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b'/bin/sh -c \'echo PLATFORM; uname; echo FOUND; command -v \'"\'"\'/usr/bin/python\'"\'"\'; command -v \'"\'"\'python3.7\'"\'"\'; command -v \'"\'"\'python3.6\'"\'"\'; command -v \'"\'"\'python3.5\'"\'"\'; command -v \'"\'"\'python2.7\'"\'"\'; command -v \'"\'"\'python2.6\'"\'"\'; command -v \'"\'"\'/usr/libexec/platform-python\'"\'"\'; command -v \'"\'"\'/usr/bin/python3\'"\'"\'; command -v \'"\'"\'python\'"\'"\'; echo ENDFOUND && sleep 0\'']...), pausing for 7 seconds
 attempt: 5, pause: 15
<127.0.0.1> ssh_retry: attempt: 5, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b'/bin/sh -c \'echo PLATFORM; uname; echo FOUND; command -v \'"\'"\'/usr/bin/python\'"\'"\'; command -v \'"\'"\'python3.7\'"\'"\'; command -v \'"\'"\'python3.6\'"\'"\'; command -v \'"\'"\'python3.5\'"\'"\'; command -v \'"\'"\'python2.7\'"\'"\'; command -v \'"\'"\'python2.6\'"\'"\'; command -v \'"\'"\'/usr/libexec/platform-python\'"\'"\'; command -v \'"\'"\'/usr/bin/python3\'"\'"\'; command -v \'"\'"\'python\'"\'"\'; echo ENDFOUND && sleep 0\'']...), pausing for 15 seconds
 attempt: 6, pause: 30
<127.0.0.1> ssh_retry: attempt: 6, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b'/bin/sh -c \'echo PLATFORM; uname; echo FOUND; command -v \'"\'"\'/usr/bin/python\'"\'"\'; command -v \'"\'"\'python3.7\'"\'"\'; command -v \'"\'"\'python3.6\'"\'"\'; command -v \'"\'"\'python3.5\'"\'"\'; command -v \'"\'"\'python2.7\'"\'"\'; command -v \'"\'"\'python2.6\'"\'"\'; command -v \'"\'"\'/usr/libexec/platform-python\'"\'"\'; command -v \'"\'"\'/usr/bin/python3\'"\'"\'; command -v \'"\'"\'python\'"\'"\'; echo ENDFOUND && sleep 0\'']...), pausing for 30 seconds
 attempt: 7, pause: 30
<127.0.0.1> ssh_retry: attempt: 7, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b'/bin/sh -c \'echo PLATFORM; uname; echo FOUND; command -v \'"\'"\'/usr/bin/python\'"\'"\'; command -v \'"\'"\'python3.7\'"\'"\'; command -v \'"\'"\'python3.6\'"\'"\'; command -v \'"\'"\'python3.5\'"\'"\'; command -v \'"\'"\'python2.7\'"\'"\'; command -v \'"\'"\'python2.6\'"\'"\'; command -v \'"\'"\'/usr/libexec/platform-python\'"\'"\'; command -v \'"\'"\'/usr/bin/python3\'"\'"\'; command -v \'"\'"\'python\'"\'"\'; echo ENDFOUND && sleep 0\'']...), pausing for 30 seconds
 attempt: 8
[WARNING]: Unhandled error in Python interpreter discovery for host 127.0.0.1: Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 22: Connection refused
 remaining tries: 8
 attempt: 1, pause: 0
<127.0.0.1> ssh_retry: attempt: 1, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b"/bin/sh -c '/usr/bin/python && sleep 0'"]...), pausing for 0 seconds
 attempt: 2, pause: 1
<127.0.0.1> ssh_retry: attempt: 2, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b"/bin/sh -c '/usr/bin/python && sleep 0'"]...), pausing for 1 seconds
 attempt: 3, pause: 3
<127.0.0.1> ssh_retry: attempt: 3, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b"/bin/sh -c '/usr/bin/python && sleep 0'"]...), pausing for 3 seconds
 attempt: 4, pause: 7
<127.0.0.1> ssh_retry: attempt: 4, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b"/bin/sh -c '/usr/bin/python && sleep 0'"]...), pausing for 7 seconds
 attempt: 5, pause: 15
<127.0.0.1> ssh_retry: attempt: 5, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b"/bin/sh -c '/usr/bin/python && sleep 0'"]...), pausing for 15 seconds
 attempt: 6, pause: 30
<127.0.0.1> ssh_retry: attempt: 6, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b"/bin/sh -c '/usr/bin/python && sleep 0'"]...), pausing for 30 seconds
 attempt: 7, pause: 30
<127.0.0.1> ssh_retry: attempt: 7, ssh return code is 255. cmd ([b'ssh', b'-o', b'ControlMaster=auto', b'-o', b'ControlPersist=30m', b'-o', b'ConnectTimeout=10', b'-o', b'ConnectionAttempts=10', b'-o', b'UserKnownHostsFile=/dev/null', b'-o', b'StrictHostKeyChecking=no', b'-o', b'KbdInteractiveAuthentication=no', b'-o', b'PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey', b'-o', b'PasswordAuthentication=no', b'-o', b'User="ansible"', b'-o', b'ConnectTimeout=10', b'-o', b'ControlPath=/home/milosz/.ansible/cp/58926ec2f8', b'127.0.0.1', b"/bin/sh -c '/usr/bin/python && sleep 0'"]...), pausing for 30 seconds
 attempt: 8
fatal: [127.0.0.1]: UNREACHABLE! => {"changed": false, "msg": "Data could not be sent to remote host \"127.0.0.1\". Make sure this host can be reached over ssh: ssh: connect to host 127.0.0.1 port 22: Connection refused\r\n", "unreachable": true}

PLAY RECAP ******************************************************************************************************************************
127.0.0.1                  : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0   

How to define the number of connection attempts

Use environment variable to define the number of connection attempts.

$ ANSIBLE_SSH_RETRIES=7 ansible-playbook -i hosts playbook.yml

Use ansible configuration file to define the number of connection attempts in a lasting way.

$ cat ansible.cfg
[ssh_connection]
pipelining = True
retries    = 2

[defaults]
vault_password_file = .vault_pass.txt