# Ansible python interpreter

**URL:** <https://forum.ansible.com/t/ansible-python-interpreter/8500>\
**Category:** Get Help\
**Tags:** git, python, ansible, ubuntu, galaxy-ng\
**Created:** [September 13, 2024, 7:00pm UTC](https://forum.ansible.com/t/ansible-python-interpreter/8500 "2024-09-13T19:00:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chandra\_Mukherjee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chandra_mukherjee/32/4685_2.png) [@Chandra\_Mukherjee](https://forum.ansible.com/u/Chandra_Mukherjee)\
**Post date:** [September 13, 2024, 7:00pm UTC](https://forum.ansible.com/t/ansible-python-interpreter/8500/1 "2024-09-13T19:00:13Z")

</div>

Hi All,  
I am trying to execute an ansible playbook using github actions but it is failing with the below error:  
_“Failed to import the required Python library (psycopg2) on fv-az1811-354’s Python /opt/pipx/venvs/ansible-core/bin/python. 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”_

Below is the playbook i am using:

- name: Install SQL scripts on Private PostgreSQL Instance  
hosts: localhost  
gather\_facts: no  
become: yes

Below is my github action pipeline.yaml:

name: Ansible Shared CI/CD pipeline DEV

on:  
workflow\_call:  
inputs:  
DB\_NAME:  
required: true  
type: string  
DB\_INSTANCE\_NAME:  
required: true  
type: string  
DB\_HOST:  
required: true  
type: string  
DB\_USER:  
required: true  
type: string  
GCP\_PROJECT:  
required: true  
type: string  
SCRIPT\_PATH:  
required: true  
type: string  
DB\_INSTANCE\_LOCATION:  
required: true  
type: string

env:  
DB\_INSTANCE\_NAME: ${{ inputs.DB\_INSTANCE\_NAME }}  
DB\_NAME: ${{ inputs.DB\_NAME }}  
DB\_HOST: ${{ inputs.DB\_HOST }}  
DB\_USER: ${{ inputs.DB\_USER }}  
DB\_PASSWORD: ${{ secrets.DB\_PASSWORD }}  
GCP\_PROJECT: ${{ inputs.GCP\_PROJECT }}  
TOKEN\_KEY: ${{ secrets.GH\_PAT }}  
SCRIPT\_PATH: ${{ inputs.SCRIPT\_PATH }}  
DB\_INSTANCE\_LOCATION: ${{ inputs.DB\_INSTANCE\_LOCATION }}  
WORKLOAD\_IDENTITY\_PROVIDER: ${{ vars.DEV\_WORKLOAD\_IDENTITY\_PROVIDER }}  
SERVICE\_ACCOUNT: ${{ vars.DEV\_SERVICE\_ACCOUNT }}  
TF\_TOKEN\_app\_terraform\_io: ${{ secrets.TF\_API\_TOKEN }}

jobs:  
data-prep:  
permissions:  
contents: ‘read’  
id-token: ‘write’  
runs-on: ubuntu-latest  
if: always()  
steps:  
- name: Checkout the repo  
uses: actions/checkout@v3

```
  - id: 'auth'
    uses: 'google-github-actions/auth@v1'
    with:
      token_format: 'access_token'
      workload_identity_provider: '${{ env.WORKLOAD_IDENTITY_PROVIDER }}'
      service_account: '${{ env.SERVICE_ACCOUNT }}'
      project_id: '${{ env.GCP_PROJECT }}'

  - name: 'Setup Cloud SDK'
    uses: 'google-github-actions/setup-gcloud@v1'

  - name: 'Create Temporary storage bucket'
    run: |
      gcloud config set project ${{ env.GCP_PROJECT }}
      gcloud storage buckets create gs://${{ github.sha }} 

  - name: Copy Artifacts to bucket
    run: |
      gsutil -m cp -r . gs://${{ github.sha }}

```

playbook-execution:  
needs: [“data-prep”]  
permissions:  
contents: ‘read’  
id-token: ‘write’  
runs-on: ubuntu-latest  
if: always()  
steps:  
- name: Checkout the repo  
uses: actions/checkout@v3  
with:  
repository: brtspd/ansible-repo  
ref: refs/heads/master  
token: ${{ env.TOKEN\_KEY }}

```
  - id: 'auth'
    uses: 'google-github-actions/auth@v1'
    with:
      token_format: 'access_token'
      workload_identity_provider: '${{ env.WORKLOAD_IDENTITY_PROVIDER }}'
      service_account: '${{ env.SERVICE_ACCOUNT }}'
      project_id: '${{ env.GCP_PROJECT }}'

  - name: 'Setup Cloud SDK'
    uses: 'google-github-actions/setup-gcloud@v1'

  - name: Download artifacts from bucket
    run: |
      gcloud config set project ${{ env.GCP_PROJECT }}
      gsutil -m cp -r gs://${{ github.sha }}/* .

  - name: List files
    run: |
      ls -lrt

  - name: 'Setup Ansible'
    run: |
      sudo apt update
      sudo apt install -y software-properties-common
      sudo add-apt-repository --yes --update ppa:ansible/ansible
      sudo apt install -y ansible

  - name: 'Which python'
    run: |
      which python3
      which ansible-playbook
      ansible-playbook --version

  - name: 'Setup Ansible collection'
    run: |
      ansible-galaxy collection install community.postgresql

  - name: 'Setup cloudSQL proxy'
    run: |
      curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.13.0/cloud-sql-proxy.linux.amd64
      chmod +x cloud-sql-proxy

  - name: 'Start cloudSQL proxy'
    run: |
      nohup ./cloud-sql-proxy --private-ip --port 5432 "${{ env.GCP_PROJECT }}":"${{ env.DB_INSTANCE_LOCATION }}":"${{ env.DB_NAME }}" &

  - name: 'Check if proxy is running'
    run: |
      ps -ef | grep "proxy" | head

  - name: 'Run ansible playbook'
    run: |
      ansible-playbook postgres-deployment.yaml -e db_name="${{ env.DB_NAME }}" -e ansible_host="${{ env.DB_HOST }}" -e db_user="${{ env.DB_USER }}" -e db_password="${{ env.DB_PASSWORD }}"

  - name: 'Stop cloudSQL proxy'
    run: |
      pid=$(ps -ef | grep "proxy" | head -1 | awk -F" " '{print $2}')
      sudo kill -9 "$pid"
      exit 0;
      
  - name: Delete storage bucket
    if: always()
    run: |
      gcloud config set project ${{ env.GCP_PROJECT}}
      gcloud storage rm -r gs://${{ github.sha }}

```

Below is the snippet of the ansible config:

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/c/cce5567e7f6e91c0169d5e913734a98e9a449069.png)

How do i fix this issue?

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [September 13, 2024, 7:43pm UTC](https://forum.ansible.com/t/ansible-python-interpreter/8500/2 "2024-09-13T19:43:27Z")

</div>

The problem is that you are running the modules in a venv (the error message provides you with its path: `/opt/pipx/venvs/ansible-core`), while you install the required Python module for the OS Python interpreter (with the `apt` module). Thus the module cannot find it.

You need to use the OS’s Python interpreter for this task (or the whole playbook), so it can make use of the installed psycopg2 Python module.

---

<div class="post-metadata">

**Author:** ![Chandra\_Mukherjee](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chandra_mukherjee/32/4685_2.png) [@Chandra\_Mukherjee](https://forum.ansible.com/u/Chandra_Mukherjee)\
**Post date:** [September 14, 2024, 12:09pm UTC](https://forum.ansible.com/t/ansible-python-interpreter/8500/4 "2024-09-14T12:09:14Z")

</div>

Hi felix,  
How do I fix it? I am sorry i am new to ansible…

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [September 18, 2024, 7:17am UTC](https://forum.ansible.com/t/ansible-python-interpreter/8500/5 "2024-09-18T07:17:09Z")

</div>

Hi. You can fix it by installing the “required python packages” in your venv with pip.

```yaml
---
- name: Install SQL scripts on Private PostgreSQL Instance
  hosts: localhost
  gather_facts: true
  become: true

  tasks:
    - name: Install required python packages
      ansible.builtin.pip:
       name:
         - libpq-dev
         - python3-psycopg2
       virtualenv: "{{ ansible_facts['env']['VIRTUAL_ENV'] }}"
       state: present

```

The other option is to set the python interpreter to your OS python interpreter, as pointed out by @felixfontein:

```yaml
---
- name: Install SQL scripts on Private PostgreSQL Instance
  hosts: localhost
  gather_facts: false
  become: true
  vars:
     ansible_python_interpreter: "/usr/bin/python3"

```
