无法将 postgresql_db 模块与 ansible 一起使用

raw*_*ain 4 postgresql python ansible

目标服务器:

-bash-4.2$ python -V
Python 2.7.5
-bash-4.2$ pip list | grep psycopg2
psycopg2 (2.8.3)
Run Code Online (Sandbox Code Playgroud)

但如果使用此任务运行 ansible playbook 将失败:

- name: Create repmgr database
  postgresql_db:
    name: repmgr
  become: true
  become_user: postgres
Run Code Online (Sandbox Code Playgroud)

错误:

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"}
Run Code Online (Sandbox Code Playgroud)

为什么不能导入psycopg2

Zei*_*tor 5

您应该确保特定主机的python 自动发现或修复ansible_python_interpreter未指向 python 的其他版本(而不是您用于安装库的版本)。

如果是这种情况,您可以:

  1. 将版本修复为您安装库的版本(ansible_python_interpreter为您的主机设置)
  2. 在其他版本中安装库(例如pip3 install psycopg2

同时,确保始终满足要求(在正确的 python 版本中)的最佳解决方案是在实际使用 postgres 模块之前将安装添加到您的 playbook 中:

- name: Make sure psycopg2 is installed
  pip:
    name: psycopg2
    state: present
Run Code Online (Sandbox Code Playgroud)