Skip to content
ohsudo, Home
Infrastructure as Code

Ansible "Missing sudo password": give it the password safely, or allow the user properly

Fix Ansible "Missing sudo password": use --ask-become-pass, ansible-vault for the become password, or NOPASSWD in sudoers, plus "Incorrect sudo password" and timeouts.

Beginner4 min readApplies to Ansible 2.14+ · ansible-core · Ubuntu · Debian · RHEL / Rocky / AlmaLinux

by OhSudo ··

Checked on real software: Ansible, Ubuntu 24.04 · November 2026How we check

fatal: [web01]: FAILED! => {"msg": "Missing sudo password"}

Ansible connected fine over SSH. Your play has become: true, so it ran sudo, and sudo asked for a password that Ansible doesn’t have. So your SSH key is fine: this is about the step after that.

You have two honest options: give Ansible the password in a safe way, or allow this user to use sudo without one.

  1. Ansibleconnects over SSH(works)
  2. sudo on the hostbecome: true(works)
  3. A password is neededAnsible has none to give(breaks here)
The SSH login worked. The play then runs sudo for become: true, and sudo asks for a password Ansible doesn't have.

TL;DR

For a run by hand:

Terminal window
ansible-playbook site.yml --ask-become-pass # or -K

For automation, use one of these:

Terminal window
ansible-vault encrypt_string 'the-password' --name 'ansible_become_password'
deploy ALL=(ALL:ALL) NOPASSWD: ALL # in /etc/sudoers.d/deploy, through visudo

1. Check it’s really this

Reproduce it with one command:

Terminal window
ansible web01 -m command -a 'whoami' --become
ansible web01 -m command -a 'whoami' --become -K

The second one asks BECOME password:, and it should print root. So which user does it connect as?

Terminal window
ansible web01 -m command -a 'id'
ansible-inventory --host web01

2. Ask for it when you run

Terminal window
ansible-playbook site.yml -K

That’s the simplest answer for a person at a keyboard. It’s one password for the whole run, so hosts that have different passwords need section 3.

3. Keep it in Ansible Vault

For automation, keep it encrypted, in your repository:

Terminal window
ansible-vault encrypt_string 'the-password' --name 'ansible_become_password' >> group_vars/web/vault.yml

Use a space before the command, or --stdin-name to read it from a prompt, so it stays out of your shell history:

Terminal window
ansible-vault encrypt_string --stdin-name 'ansible_become_password' >> group_vars/web/vault.yml

Then run it with the vault’s password:

Terminal window
ansible-playbook site.yml --ask-vault-pass
ansible-playbook site.yml --vault-password-file ~/.vault_pass # chmod 600

Different hosts can have different values through host_vars/web01/vault.yml.

Note the name: ansible_become_password. Older guides use ansible_sudo_pass, which is deprecated. And become_password alone is just an unrelated variable.

4. Allow the user with NOPASSWD

A dedicated deployment user often has this, as there’s no one there to type:

Terminal window
sudo visudo -f /etc/sudoers.d/deploy
deploy ALL=(ALL:ALL) NOPASSWD: ALL

Always go through visudo, as it checks the syntax: a mistake in sudoers can lock everyone out. See not in the sudoers file for that and recovering from it. Limit who can log in as this user, because anyone who can has root.

Ansible needs this on first setup as well, so a typical bootstrap does this once as an admin, and all later runs use it:

- hosts: all
become: true
tasks:
- name: Create the deploy user
ansible.builtin.user:
name: deploy
groups: sudo
append: true
- name: Allow deploy to sudo without a password
ansible.builtin.copy:
dest: /etc/sudoers.d/deploy
content: "deploy ALL=(ALL:ALL) NOPASSWD: ALL\n"
mode: "0440"
validate: visudo -cf %s

validate: makes Ansible check the file before installing it, which prevents a bad file from locking you out.

Limiting NOPASSWD to a few commands generally doesn’t work with Ansible. It doesn’t run your command directly, and wraps each task in a generated Python script, so a sudoers rule can’t recognize it.

5. Other errors from the same step

fatal: [web01]: FAILED! => {"msg": "Incorrect sudo password"}

Ansible has one, and it’s wrong. Check the value in your vault, and whether this host differs. Too many wrong tries can also lock out the account for some time, through faillock.

fatal: [web01]: FAILED! => {"msg": "Timeout (12s) waiting for privilege escalation prompt: "}

The prompt never arrived. Usually it’s slow DNS on the host: sudo tries to look up the machine’s name, which is what unable to resolve host is about. Raise it for now with timeout = 30 under [defaults], and fix that.

deploy is not in the sudoers file.

Ansible’s fine, and the user has no right to sudo. See that article.

To see what’s being run:

Terminal window
ansible-playbook site.yml -vvv

6. You may not need become at all

A become: true at the top of a play applies to every task. Maybe only a few need root. Moving it to those tasks is good practice:

- hosts: web
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
become: true

And connecting directly as root isn’t a better answer. Most servers forbid it, and you lose track of who did what.

If sudo isn’t there (sudo: command not found, as on a minimal Debian), use become_method: su, or install it first.

If you never got as far as this, and it’s the connection that fails, see Ansible UNREACHABLE. The SSH Config Generator can set up the host, user and key for your Ansible machine.

Warning

Don’t write the password in plain text in your inventory, group_vars or a play, even in a private repository, and don’t pass it with -e 'ansible_become_password=...', which ends up in your shell history and can be visible in the process list. If it’s been committed, change the password: removing it from a later commit doesn’t remove it from the history.

Shell, we have a problem!

Similar, but different: UNREACHABLE! means the SSH connection failed, not sudo: see that article. Permission denied (publickey) is your SSH key: see that article. And Attempting to decrypt but no vault secrets found means you need --ask-vault-pass. Outside Ansible, a script that runs sudo over SSH gets sudo: a terminal is required to read the password.

Found this useful? Share it.

Found a mistake? Let us know at contact@ohsudo.com.

Tools

Try it yourself

Articles

Related articles