I am trying to create an Ansible playbook which would setup a local Postgres database to be used for local/dev testing of a certain app.
postgresql_db
This is caused by a well-known and described behaviour of Ansible.
In short, if you specify localhost
wherever in your inventory, Ansible will default to using /usr/bin/python
for running the modules regardless of the connection: local
setting.
This in turn will cause problems if additional libraries were installed in a Python environment used to execute a playbook, but not for the /usr/bin/python
.
The solution is to specify ansible_python_interpreter
for the localhost
. In your case:
[my_ws]
localhost ansible_python_interpreter={{ansible_playbook_python}}
Because of the above, the test to verify module presence should be:
- command: "{{ ansible_python_interpreter | default('/usr/bin/python') }} -c 'import {{ module }}; print({{ module }}.__version__)'"
vars:
module: psycopg2
register: test
- debug:
var: test.stdout