Interview Prep/Ansible/Privilege Escalation and Python Interpreter Issues

`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.

All Ansible questions Roadmap: Ansible

More on Privilege Escalation and Python Interpreter Issues