Using Ansible postgresql_user with psycopg2 from VirtualEnv Using Ansible postgresql_user with psycopg2 from VirtualEnv postgresql postgresql

Using Ansible postgresql_user with psycopg2 from VirtualEnv


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/activatepip 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"