`Failed to import the required Python library (psycopg2) on db01's Python /usr/bin/python3. Please read the module documentation and install it in the appropriate location.`
IntermediateTroubleshootingRoles, Collections and Galaxy
Answer
Likely causes → Collection modules run on the target host and need their Python dependencies installed there, for the interpreter Ansible uses. It may be installed for a different Python (python3.11 against /usr/bin/python3).
Fix →
- ansible.builtin.package:
name: python3-psycopg2
state: present
Install it before any community.postgresql task, or set ansible_python_interpreter to the Python that has the library.
Prevent → Give each role a "dependencies" task at the top that installs the module libraries it needs.