Search code examples
pythonpostgresqlansiblepsycopg2python-3.7

community.postgresql.postgresql_query raises `Failed to import the required Python library (psycopg2) on YYY's Python XXX/ansible-venv/bin/python3.7`


I am running an Ansible playbook with virtual environment ansible_venv activated.

My playbook get struck here

collect data from DB

  localhost failed | msg: Failed to import the required Python library (psycopg2) on YYY's Python XXX/ansible-venv/bin/python3.7. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter

where XXX is hidden path.

the task collect data from DB is :

- name: collect data from DB
  community.postgresql.postgresql_query:
  
    login_host: '{{ db_host }}'
    login_user: '{{ db_username }}'
    login_password: '{{ db_password }}'
    db: '{{ db_database }}'
    port: '{{ db_database_port }}'
    
    query: "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = '{{ my_table }}')"
  
  register: qres

The machine which runs this playbook does not have access to the internet, so I have manually installed these packages on my machine:

/home/myuser/XXX/ansible-venv/lib/python3.7/site-packages/psycopg2
/home/myuser/XXX/ansible-venv/lib/python3.7/site-packages/psycopg2_binary-2.9.6.dist-info    
/home/myuser/XXX/ansible-venv/lib/python3.7/site-packages/psycopg2_binary.libs

In order to do this, I have downloaded these packages on another machine with direct access to the internet, by using the terminal command pip install psycopg2-binary, then copied the files to the "ansible machine".

Theyr permissions and user are the same of the others modules installed there.

At path /home/myuser/XXX/ansible-venv/lib/ I don't have any other interpreter than python3.7.

However, I am still getting the same error.

update

The error is raised only on localhost (YYY in my example).

tasks using the community.postgresql.postgresql_query on other machines work.


Solution

  • I solved by rebuilding anew the ansible virtual environment on a machine having direct access to internet (machine2) and then moving it to the machine which didn't (machine1).

    on machine1:

    pip freeze > requirements.txt
    

    on machine1

    virtualenv ansible_venv
    
    source ansible_venv/bin/activate
    

    reinstalled the modules

    cat requirements.txt | xargs -n 1 pip install
    

    installed psycopg2-binary

    pip install psycopg2-binary
    

    compressed and moved the ansible_venv folder to machine1 via rsync