`ModuleNotFoundError: No module named 'apt'` / `Could not import python modules: apt, apt_pkg. Please install python3-apt package.`
IntermediateTroubleshootingPrivilege Escalation and Python Interpreter Issues
Answer
Likely causes → Ansible is using a Python interpreter (often a virtualenv or a custom /usr/local/bin/python3) that lacks the system python3-apt.
Diagnose →
ansible web01 -m setup -a 'filter=ansible_python_interpreter'
ansible web01 -m raw -a '/usr/bin/python3 -c "import apt"'
Fix → Point to /usr/bin/python3, or install python3-apt for that interpreter.
Prevent → Do not override the interpreter with a venv for system-package tasks.
More on Privilege Escalation and Python Interpreter Issues
`fatal: [web01]: FAILED! => {"msg": "Missing sudo password"}`Beginner`sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper`Intermediate`sudo: sorry, you must have a tty to run sudo`Intermediate`FAILED! => {"msg": "Failed to set permissions on the temporary files Ansible needs to create when becoming an unprivileged user (rc: 1, err: chmod: invalid mode: 'A+user:app:rx:allow')"}`Advanced`"msg": "/bin/sh: 1: /usr/bin/python: not found"` or `"The module failed to execute correctly, you probably need to set the interpreter."`Beginner`FAILED! => {"msg": "The Python 2 yum module is needed for this module"}`Intermediate