Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Ansible hangs, then times out waiting for the sudo prompt

ErrorTimeout (12s) waiting for privilege escalation prompt

Short answer

Ansible ran sudo on the target and never saw the answer it expected. Most often sudo wanted a password Ansible did not have, or the become method or sudo path is wrong for that host. A slow host can also exceed the connection timeout. Rerun with -K and -vvv to see which.

  • Applies to ansible-core 2.18, Ubuntu 24.04 target, Amazon Linux 2023 target
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Rerun the failing play with verbose output and ask for the become password. If it now works, the missing password was the cause.

    ansible-playbook site.yml -i inventory.ini -K -vvv
  2. 2Match what you see, then jump to its fix

    1. 1The play hangs at the first become task and works with -Ksudo needs a password and Ansible was not given oneFix
    2. 2The host uses su, doas or a sudo outside the default path, so the expected prompt never appearsThe become method or sudo path does not match the hostFix
    3. 3Fails only on busy or distant hosts, and passes when retriedThe connection timeout is too short for a slow hostFix

What does the error look like?

  • Timeout (12s) waiting for privilege escalation prompt
  • a playbook using become seems to hang

Cause 1 of 3

sudo needs a password and Ansible was not given one

1Check

Ansible's documentation says a playbook that appears to hang is most likely stuck at the privilege escalation prompt. Check whether your user can run sudo without a password.

ssh -i key.pem ubuntu@host 'sudo -n true && echo passwordless'

2Fix

Give Ansible the password at run time with -K, or store it encrypted with Ansible Vault and reference it as ansible_become_password. Never keep it in plain text.

ansible-playbook site.yml -K

# or in a vault-encrypted vars file
ansible_become_password: "{{ vault_sudo_password }}"

3Verify

The play passes its first become task.

ansible all -i inventory.ini -b -m ping -K

Confirmed by docs.ansible.com

Did this fix it?

Cause 2 of 3

The become method or sudo path does not match the host

1Check

Ansible uses sudo by default. Other methods exist, such as su and doas, and the sudo executable can be set separately.

ssh -i key.pem ubuntu@host 'command -v sudo doas su'

2Fix

Name the method the host really uses, or the full path to sudo, for the group in the inventory.

[web:vars]
ansible_become=true
ansible_become_method=sudo
ansible_become_exe=/usr/bin/sudo

3Verify

Run a privileged ping against the group.

ansible web -i inventory.ini -b -m ping

Confirmed by docs.ansible.com

Did this fix it?

Cause 3 of 3

The connection timeout is too short for a slow host

1Check

The default timeout for connection plugins is 10 seconds, set by the timeout setting in ansible.cfg or ANSIBLE_TIMEOUT. Raising it tells you whether time is the limit.

ansible-config dump | grep -i DEFAULT_TIMEOUT

2Fix

Raise it for this run, or for the project in ansible.cfg.

ANSIBLE_TIMEOUT=30 ansible-playbook site.yml -K

# ansible.cfg
[defaults]
timeout = 30

3Verify

Rerun the play on the slow host.

ansible-playbook site.yml -i inventory.ini -K

Confirmed by docs.ansible.com

Did this fix it?

How do I stop it coming back?

  • Use a dedicated deploy user with passwordless sudo only for the commands the play needs.
  • Keep ansible_become_password in Ansible Vault, never in the inventory.
  • Run a small `-b -m ping` check against new hosts before the real playbook.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

dnf cannot install MySQL on Amazon Linux 2023

dnf cannot find a mysql-server package on Amazon Linux 2023

Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.