大网数据广告活动
服务器运维问答

Can't use postgresql_db module with ansible

· 52CE 我爱测

Target server: -bash-4.2$ python -V Python 2.7.5 -bash-4.2$ pip list | grep psycopg2 psycopg2 (2.8.3) But if run ansible playbook with this task will fail: - name: Create repmgr database postgresql_db: name: repmgr become: true b…

问题原文

原帖链接 · rawmain · CC BY-SA 4.0

Target server:

-bash-4.2$ python -V
Python 2.7.5
-bash-4.2$ pip list | grep psycopg2
psycopg2 (2.8.3)

But if run ansible playbook with this task will fail:

- name: Create repmgr database
  postgresql_db:
    name: repmgr
  become: true
  become_user: postgres

Error:

TASK [db : Create repmgr database] ***************************************************************************
fatal: [192.168.0.1]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (psycopg2) on db's Python /usr/bin/python. Please read module documentation and install 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"}

Why can't it import psycopg2?

已采纳解答

原帖链接 · Zeitounator · CC BY-SA 4.0

You should make sure that the python autodiscovery or the fixed ansible_python_interpreter for your particular host is not pointing to an other version of python (than the one you used to install the lib).

If this is the case you can either:

  1. fix the version to the one you installed the library in (set ansible_python_interpreter for your host)
  2. install the library in the other version (e.g. pip3 install psycopg2)

Meanwhile, the best solution to make sure the requirement is always covered (in the correct python version) is to add the install to your playbook just before you actually use the postgres module:

- name: Make sure psycopg2 is installed
  pip:
    name: psycopg2
    state: present

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-12-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。