community.postgresql.postgresql_publication 模块 – 添加、更新或删除 PostgreSQL 发布

注意

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

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

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

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

概要

  • 添加、更新或删除 PostgreSQL 发布。

要求

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

  • psycopg2 >= 2.5.1

参数

参数

注释

ca_cert

别名:ssl_rootcert

字符串

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

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

cascade

布尔值

删除发布依赖项。仅当 state=absent 时有效。

选项

  • false ← (默认)

  • true

columns

字典

在 community.postgresql 3.8.0 中添加

要添加到发布的表及其列的列表。

如果未为表传递任何列,则会将其整体发布。

tablestables_in_schema 互斥。

comment

字符串

在 community.postgresql 3.3.0 中添加

在发布上设置注释。

要重置注释,请传递空字符串。

connect_params

字典

在 community.postgresql 2.3.0 中添加

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

这些参数具有优先级。

默认值: {}

db

别名:login_db

字符串

要连接到的数据库的名称,以及将在其中更改发布状态的数据库。

login_host

别名:host

字符串

运行数据库的主机。

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

默认值: ""

login_password

字符串

此模块应用于建立其 PostgreSQL 会话的密码。

默认值: ""

login_unix_socket

别名:unix_socket

字符串

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

默认值: ""

login_user

别名:login

字符串

此模块应用于建立其 PostgreSQL 会话的用户名。

默认值: "postgres"

name

字符串 / 必需

要添加、更新或删除的发布的名称。

owner

字符串

发布所有者。

如果未定义 owner,则所有者将设置为 login_usersession_role

parameters

字典

包含可选发布参数的字典。

可用的参数取决于 PostgreSQL 版本。

port

别名:login_port

整数

要连接到的数据库端口。

默认值: 5432

session_role

字符串

在 community.postgresql 0.2.0 中添加

连接后切换到 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"

state

字符串

发布状态。

选项

  • "absent"

  • "present" ← (默认)

tables

列表 / 元素=字符串

要添加到发布的表的列表。

如果未设置值,则所有表都是目标。

如果发布已存在于特定表中并且未传递 tables,则不会更改任何内容。

如果您需要将所有同名的表添加到发布物中,请删除现有表并创建新表,无需传递tables参数。

tables_in_schemacolumns参数互斥。

tables_in_schema

列表 / 元素=字符串

在community.postgresql 3.5.0中添加

指定要添加到发布物中的模式列表,以复制这些模式中所有表的更改。

如果要移除所有模式,请显式传递空列表[]

自PostgreSQL 15版本起支持。

tablescolumns参数互斥。

trust_input

布尔值

在 community.postgresql 0.2.0 中添加

如果为false,则检查参数nametablesownersession_roleparams的值是否潜在危险。

只有当可能通过参数进行SQL注入时,使用false才合理。

选项

  • false

  • true ← (默认)

属性

属性

支持

描述

check_mode

支持:完全支持

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

备注

注意

  • PostgreSQL版本必须为10或更高。

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

  • 为避免“Peer authentication failed for user postgres”错误,请使用postgres用户作为become_user

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

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

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

另请参见

另请参见

CREATE PUBLICATION 参考

CREATE PUBLICATION命令文档的完整参考。

ALTER PUBLICATION 参考

ALTER PUBLICATION命令文档的完整参考。

DROP PUBLICATION 参考

DROP PUBLICATION命令文档的完整参考。

示例

- name: Create a new publication with name "acme" targeting all tables in database "test"
  community.postgresql.postgresql_publication:
    db: test
    name: acme
    comment: Made by Ansible

- name: Create publication "acme" publishing only prices and vehicles tables
  community.postgresql.postgresql_publication:
    name: acme
    tables:
    - prices
    - vehicles

- name: Create publication "acme" publishing only prices table and id and named from vehicles tables
  community.postgresql.postgresql_publication:
    name: acme
    columns:
      prices:
      vehicles:
        - id
        - name

- name: Create a new publication "acme" for tables in schema "myschema"
  community.postgresql.postgresql_publication:
    db: test
    name: acme
    tables_in_schema: myschema

- name: Remove all schemas from "acme" publication
  community.postgresql.postgresql_publication:
    db: test
    name: acme
    tables_in_schema: []

- name: >
    Create publication "acme", set user alice as an owner, targeting all tables
    Allowable DML operations are INSERT and UPDATE only
  community.postgresql.postgresql_publication:
    name: acme
    owner: alice
    parameters:
      publish: 'insert,update'

- name: >
    Assuming publication "acme" exists and there are targeted
    tables "prices" and "vehicles", add table "stores" to the publication
  community.postgresql.postgresql_publication:
    name: acme
    tables:
    - prices
    - vehicles
    - stores

- name: Remove publication "acme" if exists in database "test"
  community.postgresql.postgresql_publication:
    db: test
    name: acme
    state: absent

返回值

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

描述

alltables

布尔值

标志指示所有表都已发布。

返回:如果发布物存在

示例:false

exists

布尔值

标志指示发布物在运行结束时是否存在。

返回:成功

示例:true

owner

字符串

运行结束时发布物的拥有者。

返回:如果发布物存在

示例:"alice"

parameters

字典

运行结束时发布物的参数。

返回:如果发布物存在

示例:{"publish": {"delete": false, "insert": false, "update": true}}

queries

字符串

已执行查询的列表。

返回:成功

示例:"['DROP PUBLICATION \"acme\" CASCADE']"

tables

列表 / 元素=字符串

运行结束时发布物中表的列表。

如果所有表都已发布,则返回空列表。

返回:如果发布物存在

示例:["\"public\".\"prices\"", "\"public\".\"vehicles\""]

作者

  • Loic Blot (@nerzhul)

  • Andrew Klychkov (@Andersson007)