community.postgresql.postgresql_info 模块 – 收集关于 PostgreSQL 服务器的信息

注意

此模块是 community.postgresql 集合 (版本 3.9.0) 的一部分。

如果您使用的是 ansible 包,则可能已经安装了此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.postgresql。您需要其他需求才能使用此模块,请参阅 需求 获取详细信息。

要在 playbook 中使用它,请指定:community.postgresql.postgresql_info

概要

  • 收集关于 PostgreSQL 服务器的信息。

需求

以下需求是在执行此模块的主机上需要的。

  • psycopg2 >= 2.5.1

参数

参数

注释

ca_cert

别名:ssl_rootcert

字符串

指定包含 SSL 证书颁发机构 (CA) 证书的文件名。

如果文件存在,则会验证服务器的证书是否已由其中一个颁发机构签名。

connect_params

字典

在 community.postgresql 2.3.0 中添加

要传递给 libpg 的任何其他参数。

这些参数具有优先级。

默认值: {}

db

别名:login_db

字符串

要连接的数据库名称。

filter

列表 / 元素=字符串

通过逗号分隔的字符串或 YAML 列表限制收集的信息。

允许的值为 versiondatabasesin_recoverysettingstablespacesrolesreplicationsrepl_slots

默认情况下,收集所有子集。

您可以使用 shell 风格 (fnmatch) 通配符来传递值组(请参阅示例)。

您可以在值之前使用 '!'(例如,!settings)将其从信息中排除。

如果您向过滤器传递包含和排除的值,例如 *filter=!settings,ver*,则将忽略排除的值。

login_host

别名:host

字符串

运行数据库的主机。

如果您在使用 localhost 时遇到连接问题,请尝试使用 127.0.0.1

默认值: ""

login_password

字符串

此模块应使用此密码来建立其 PostgreSQL 会话。

默认值: ""

login_unix_socket

别名:unix_socket

字符串

本地连接的 Unix 域套接字的路径。

默认值: ""

login_user

别名:login

字符串

此模块应使用此用户名来建立其 PostgreSQL 会话。

默认值: "postgres"

port

别名:login_port

整数

要连接到的数据库端口。

默认值: 5432

session_role

字符串

连接后切换到 session_role。指定的 session_role 必须是当前 login_user 属于的角色。

SQL 命令的权限检查将像 session_role 是最初登录的角色一样进行。

ssl_cert

路径

在 community.postgresql 2.4.0 中添加

指定客户端 SSL 证书的文件名。

ssl_key

路径

在 community.postgresql 2.4.0 中添加

指定用于客户端证书的密钥的位置。

ssl_mode

字符串

确定是否以及优先级如何与服务器协商安全的 SSL TCP/IP 连接。

有关模式的更多信息,请参阅 https://postgresql.ac.cn/docs/current/static/libpq-ssl.html

prefer 的默认值与 libpq 默认值匹配。

选项

  • "allow"

  • "disable"

  • "prefer" ← (默认)

  • "require"

  • "verify-ca"

  • "verify-full"

trust_input

布尔值

在 community.postgresql 0.2.0 中添加

如果为 false,则检查 *session_role* 的值是否潜在危险。

仅当可能通过session_role进行SQL注入时,使用false才合理。

选项

  • false

  • true ← (默认)

属性

属性

支持

描述

check_mode

支持:完全支持

可以在check_mode下运行,并在不修改目标的情况下返回更改状态预测。

备注

注意

  • 默认身份验证假设您正在以主机上的postgres帐户身份登录或使用sudo。

  • 为避免“用户postgres的同级身份验证失败”错误,请使用postgres用户作为become_user

  • 此模块使用psycopg,这是一个Python PostgreSQL数据库适配器。在使用此模块之前,必须确保主机上已安装psycopg2 >= 2.5.1psycopg3 >= 3.1.8

  • 如果远程主机是PostgreSQL服务器(默认情况),则远程主机上也必须安装PostgreSQL。

  • 对于基于Ubuntu的系统,在使用此模块之前,请在远程主机上安装postgresqllibpq-devpython3-psycopg2包。

另见

另见

community.postgresql.postgresql_ping

检查远程PostgreSQL服务器可用性。

示例

# Display info from postgres hosts.
# ansible postgres -m postgresql_info

# Display only databases and roles info from all hosts using shell-style wildcards:
# ansible all -m postgresql_info -a 'filter=dat*,rol*'

# Display only replications and repl_slots info from standby hosts using shell-style wildcards:
# ansible standby -m postgresql_info -a 'filter=repl*'

# Display all info from databases hosts except settings:
# ansible databases -m postgresql_info -a 'filter=!settings'

- name: Collect PostgreSQL version and extensions
  become: true
  become_user: postgres
  community.postgresql.postgresql_info:
    filter: ver*,ext*

- name: Collect all info except settings and roles
  become: true
  become_user: postgres
  community.postgresql.postgresql_info:
    filter: "!settings,!roles"

# On FreeBSD with PostgreSQL 9.5 version and lower use pgsql user to become
# and pass "postgres" as a database to connect to
- name: Collect tablespaces and repl_slots info
  become: true
  become_user: pgsql
  community.postgresql.postgresql_info:
    db: postgres
    filter:
    - tablesp*
    - repl_sl*

- name: Collect all info except databases
  become: true
  become_user: postgres
  community.postgresql.postgresql_info:
    filter:
    - "!databases"

返回值

常用返回值已在此处记录,以下是此模块特有的字段

描述

databases

字典

有关数据库的信息。

返回:成功

in_recovery

布尔值

指示服务是否处于恢复模式。

返回:成功

示例:false

pending_restart_settings

列表 / 元素=字符串

将在重启后生效的已更改设置列表。

返回:成功

repl_slots

字典

复制槽(在9.4及更高版本中可用)。

返回:如果存在

replications

字典

有关当前通过进程PID进行复制的信息。

返回:如果pg_stat_replication视图存在

roles

字典

有关角色的信息。

返回:成功

settings

字典

有关运行时服务器参数的信息。

返回:成功

tablespaces

字典

有关表空间的信息。

返回:成功

version

字典

数据库服务器版本。

返回:成功

作者

  • Andrew Klychkov (@Andersson007)