Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Ansible postgresql_user with psycopg2 from VirtualEnv

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?

like image 282
Peter Tröger Avatar asked Apr 19 '15 10:04

Peter Tröger


1 Answers

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"
like image 79
tux Avatar answered Nov 03 '22 02:11

tux