13

The Ansible postgresql_user module demands a working installation of psycopg2:

http://docs.ansible.com/postgresql_user_module.html

If this is installed in a VirtualEnv on the server, how can the Ansible module find it?

Other Ansible modules seem to have explicit VirtualEnv support, so is this simply a missing feature?

1

1 Answer 1

2
+50

If you want to use the psycopg2 module from the virtualenv, one possible solution would be

Steps followed:

1) Created a ubuntu 16.04 vagrant machine and installed postgresql.

2) Used the ansible postgresql_db module to create a new database and failed with error FAILED! => {"changed": false, "failed": true, "msg": "the python psycopg2 module is required"}

3) Create a virtualenv and install psycopg2 in virtualenv

virtualenv venv -p /usr/bin/python (Note: python2.7)
source venv/bin/activate
pip install psycopg2

4) Run ansible-playbook with ansible_python_interpreter to point to the python interpreter from the virtualenv and the database create task succeeded. The ansible command and contents are as follows,

---
- hosts: vagrant
  sudo: true

  pre_tasks:
     - raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
     - setup:

  tasks:
    - name: db create
      postgresql_db:
        name: acme
      become_user: postgres

The ansible-playbook command

ansible-playbook playbook.yml -e "ansible_python_interpreter=/home/ubuntu/venv/bin/python"
Sign up to request clarification or add additional context in comments.

2 Comments

Using the "ansible_python_interpreter" parameter is a great hint. Before accepting the answer: Wouldn't it make more sense to set it in the inventory configuration, as described here: docs.ansible.com/ansible/intro_inventory.html ?
@PeterTröger There are multiple ways to specify the ansible_python_interpreter variable, it depends on the user's preference I believe. If you want a persistent approach, you could set it in inventory or as an environment variable on a play level in the hosts section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.