community.postgresql.postgresql_privs 模块 – 在 PostgreSQL 数据库对象上授予或撤销权限
注意
此模块是 community.postgresql 集合 (版本 3.9.0) 的一部分。
如果您使用的是 ansible
包,则可能已安装此集合。它不包含在 ansible-core
中。要检查是否已安装它,请运行 ansible-galaxy collection list
。
要安装它,请使用: ansible-galaxy collection install community.postgresql
。您需要其他要求才能使用此模块,有关详细信息,请参阅 要求。
要在 playbook 中使用它,请指定: community.postgresql.postgresql_privs
。
概要
授予或撤销 PostgreSQL 数据库对象的权限。
此模块基本上是对 PostgreSQL 的 GRANT 和 REVOKE 语句的大部分功能的包装器,并具有更改检测功能(GRANT/REVOKE *privs* ON *type* *objs* TO/FROM *roles*)。
要求
执行此模块的主机需要以下要求。
psycopg2 >= 2.5.1
参数
参数 |
注释 |
---|---|
指定包含 SSL 证书颁发机构 (CA) 证书的文件的名称。 如果文件存在,则服务器的证书将被验证是否已由这些机构之一签名。 |
|
要传递给 libpg 的任何其他参数。 这些参数优先。 默认值: |
|
要连接到的数据库的名称。 |
|
如果为 选项
|
|
设置为 grant_option 仅在 state 为 选项
|
|
运行数据库的主机。 如果您在使用 默认值: |
|
此模块应用于建立其 PostgreSQL 会话的密码。 默认值: |
|
本地连接的 Unix 域套接字的路径。 默认值: |
|
此模块应用于建立其 PostgreSQL 会话的用户名。 默认值: |
|
要设置其权限的数据库对象的逗号分隔列表。 如果type 为 从 PostgreSQL 11 和 community.postgresql 集合 1.3.0 开始支持 从 PostgreSQL 15 和 community.postgresql 集合 3.1.0 开始支持 如果type 为 如果type 为 |
|
用于身份验证的密码。 此选项已弃用,将在 community.postgresql 4.0.0 中移除,请改用login_password 选项。 与login_password 互斥。 默认值: |
|
连接到的数据库端口。 默认值: |
|
要授予/撤销的权限的逗号分隔列表。 |
|
要为其设置权限的角色(用户/组)名称的逗号分隔列表。 PostgreSQL 中隐式定义了角色 从集合版本 3.1.0 和 PostgreSQL 9.5 开始支持 从集合版本 3.1.0 和 PostgreSQL 14 开始支持 |
|
包含通过objs 指定的数据库对象的模式。 仅当type 为 请注意,对于嵌入式类型,当type=type 时,schema 可以分别为 如果未指定,则使用 |
|
连接后切换到session_role。 指定的session_role必须是当前登录用户所属的角色。 SQL命令的权限检查将像最初登录的session_role一样进行。 |
|
指定客户端SSL证书的文件名。 |
|
指定用于客户端证书的密钥位置。 |
|
确定是否以及以什么优先级与服务器协商安全的SSL TCP/IP连接。 有关模式的更多信息,请参见https://postgresql.ac.cn/docs/current/static/libpq-ssl.html。
选项
|
|
如果 选项
|
|
现有角色(用户/组)名称列表,设置为随后由其创建的数据库对象的默认权限。 参数target_roles 仅在 |
|
如果为 仅当可能通过参数进行SQL注入时,才建议使用 选项
|
|
要设置其权限的数据库对象的类型。 从 2.7 版本开始提供 从 Ansible 2.8 版本开始提供 从 Ansible 2.10 版本开始提供 从集合版本 1.3.0 和 PostgreSQL 11 开始支持 从集合版本 3.1.0 和 PostgreSQL 15 开始支持 从集合版本 3.6.0 开始, 选项
|
属性
属性 |
支持 |
描述 |
---|---|---|
支持:完全支持 |
可以在check_mode下运行,并在不修改目标的情况下返回更改状态预测。 |
备注
注意
接受逗号分隔列表的参数(privs、objs、roles)具有单数别名(priv、obj、role)。
要仅撤销特定对象的
GRANT OPTION
,请将state 设置为present
并将grant_option 设置为false
(请参见示例)。请注意,当从角色R撤销权限时,该角色可能仍然可以通过授予给任何R是成员的角色(包括
PUBLIC
)的权限进行访问。请注意,当从角色 R 撤销权限时,您将以通过login_user 指定的用户身份执行此操作。如果另一个用户也已向 R 授予相同的权限,则 R 仍可以通过这些权限访问数据库对象。
撤销权限时,假定为
RESTRICT
(请参见 PostgreSQL 文档)。默认身份验证假定您正在以主机上的
postgres
帐户身份登录或使用 sudo 命令。要避免“用户 postgres 的对等身份验证失败”错误,请使用 postgres 用户作为become_user。
此模块使用
psycopg
,这是一个 Python PostgreSQL 数据库适配器。在使用此模块之前,必须确保主机上已安装psycopg2 >= 2.5.1
或psycopg3 >= 3.1.8
。如果远程主机是 PostgreSQL 服务器(这是默认情况),则还必须在远程主机上安装 PostgreSQL。
对于基于 Ubuntu 的系统,在使用此模块之前,请在远程主机上安装
postgresql
、libpq-dev
和python3-psycopg2
包。
另请参阅
另请参阅
- community.postgresql.postgresql_user
创建、更改或删除 PostgreSQL 服务器实例中的用户(角色)。
- community.postgresql.postgresql_owner
更改PostgreSQL数据库对象的拥有者。
- community.postgresql.postgresql_membership
向组添加或删除 PostgreSQL 角色。
- PostgreSQL权限
关于PostgreSQL权限的常规信息。
- PostgreSQL GRANT 命令参考
PostgreSQL GRANT 命令文档的完整参考。
- PostgreSQL REVOKE 命令参考
PostgreSQL REVOKE 命令文档的完整参考。
示例
# On database "library":
# GRANT SELECT, INSERT, UPDATE ON TABLE public.books, public.authors
# TO librarian, reader WITH GRANT OPTION
- name: Grant privs to librarian and reader on database library
community.postgresql.postgresql_privs:
database: library
state: present
privs: SELECT,INSERT,UPDATE
type: table
objs: books,authors
schema: public
roles: librarian,reader
grant_option: true
- name: Same as above leveraging default values
community.postgresql.postgresql_privs:
db: library
privs: SELECT,INSERT,UPDATE
objs: books,authors
roles: librarian,reader
grant_option: true
# REVOKE GRANT OPTION FOR INSERT ON TABLE books FROM reader
# Note that role "reader" will be *granted* INSERT privilege itself if this
# isn't already the case (since state: present).
- name: Revoke privs from reader
community.postgresql.postgresql_privs:
db: library
state: present
priv: INSERT
obj: books
role: reader
grant_option: false
# "public" is the default schema. This also works for PostgreSQL 8.x.
- name: REVOKE INSERT, UPDATE ON ALL TABLES IN SCHEMA public FROM reader
community.postgresql.postgresql_privs:
db: library
state: absent
privs: INSERT,UPDATE
objs: ALL_IN_SCHEMA
role: reader
- name: GRANT ALL PRIVILEGES ON SCHEMA public, math TO librarian
community.postgresql.postgresql_privs:
db: library
privs: ALL
type: schema
objs: public,math
role: librarian
# Note the separation of arguments with colons.
- name: GRANT ALL PRIVILEGES ON FUNCTION math.add(int, int) TO librarian, reader
community.postgresql.postgresql_privs:
db: library
privs: ALL
type: function
obj: add(int:int)
schema: math
roles: librarian,reader
# Note that group role memberships apply cluster-wide and therefore are not
# restricted to database "library" here.
- name: GRANT librarian, reader TO alice, bob WITH ADMIN OPTION
community.postgresql.postgresql_privs:
db: library
type: group
objs: librarian,reader
roles: alice,bob
admin_option: true
# Note that here "db: postgres" specifies the database to connect to, not the
# database to grant privileges on (which is specified via the "objs" param)
- name: GRANT ALL PRIVILEGES ON DATABASE library TO librarian
community.postgresql.postgresql_privs:
db: postgres
privs: ALL
type: database
obj: library
role: librarian
# If objs is omitted for type "database", it defaults to the database
# to which the connection is established
- name: GRANT ALL PRIVILEGES ON DATABASE library TO librarian
community.postgresql.postgresql_privs:
db: library
privs: ALL
type: database
role: librarian
# Available since version 2.7
# Objs must be set, ALL_DEFAULT to TABLES/SEQUENCES/TYPES/FUNCTIONS
# ALL_DEFAULT works only with privs=ALL
# For specific
- name: ALTER DEFAULT PRIVILEGES ON DATABASE library TO librarian
community.postgresql.postgresql_privs:
db: library
objs: ALL_DEFAULT
privs: ALL
type: default_privs
role: librarian
grant_option: true
# Available since version 2.7
# Objs must be set, ALL_DEFAULT to TABLES/SEQUENCES/TYPES/FUNCTIONS
# ALL_DEFAULT works only with privs=ALL
# For specific
- name: ALTER DEFAULT PRIVILEGES ON DATABASE library TO reader, step 1
community.postgresql.postgresql_privs:
db: library
objs: TABLES,SEQUENCES
privs: SELECT
type: default_privs
role: reader
- name: ALTER DEFAULT PRIVILEGES ON DATABASE library TO reader, step 2
community.postgresql.postgresql_privs:
db: library
objs: TYPES
privs: USAGE
type: default_privs
role: reader
# Available since version 2.8
- name: GRANT ALL PRIVILEGES ON FOREIGN DATA WRAPPER fdw TO reader
community.postgresql.postgresql_privs:
db: test
objs: fdw
privs: ALL
type: foreign_data_wrapper
role: reader
# Available since community.postgresql 0.2.0
- name: GRANT ALL PRIVILEGES ON TYPE customtype TO reader
community.postgresql.postgresql_privs:
db: test
objs: customtype
privs: ALL
type: type
role: reader
# Available since version 2.8
- name: GRANT ALL PRIVILEGES ON FOREIGN SERVER fdw_server TO reader
community.postgresql.postgresql_privs:
db: test
objs: fdw_server
privs: ALL
type: foreign_server
role: reader
# Available since version 2.8
# Grant 'execute' permissions on all functions in schema 'common' to role 'caller'
- name: GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA common TO caller
community.postgresql.postgresql_privs:
type: function
state: present
privs: EXECUTE
roles: caller
objs: ALL_IN_SCHEMA
schema: common
# Available since collection version 1.3.0
# Grant 'execute' permissions on all procedures in schema 'common' to role 'caller'
# Needs PostreSQL 11 or higher and community.postgresql 1.3.0 or higher
- name: GRANT EXECUTE ON ALL PROCEDURES IN SCHEMA common TO caller
community.postgresql.postgresql_privs:
type: procedure
state: present
privs: EXECUTE
roles: caller
objs: ALL_IN_SCHEMA
schema: common
# Available since version 2.8
# ALTER DEFAULT PRIVILEGES FOR ROLE librarian IN SCHEMA library GRANT SELECT ON TABLES TO reader
# GRANT SELECT privileges for new TABLES objects created by librarian as
# default to the role reader.
# For specific
- name: ALTER privs
community.postgresql.postgresql_privs:
db: library
schema: library
objs: TABLES
privs: SELECT
type: default_privs
role: reader
target_roles: librarian
# Available since version 2.8
# ALTER DEFAULT PRIVILEGES FOR ROLE librarian IN SCHEMA library REVOKE SELECT ON TABLES FROM reader
# REVOKE SELECT privileges for new TABLES objects created by librarian as
# default from the role reader.
# For specific
- name: ALTER privs
community.postgresql.postgresql_privs:
db: library
state: absent
schema: library
objs: TABLES
privs: SELECT
type: default_privs
role: reader
target_roles: librarian
# Available since community.postgresql 0.2.0
- name: Grant type privileges for pg_catalog.numeric type to alice
community.postgresql.postgresql_privs:
type: type
roles: alice
privs: ALL
objs: numeric
schema: pg_catalog
db: acme
- name: Alter default privileges grant usage on schemas to datascience
community.postgresql.postgresql_privs:
database: test
type: default_privs
privs: usage
objs: schemas
role: datascience
# Available since community.postgresql 3.1.0
# Needs PostgreSQL 15 or higher
- name: GRANT SET ON PARAMETER log_destination,log_line_prefix TO logtest
community.postgresql.postgresql_privs:
database: logtest
state: present
privs: SET
type: parameter
objs: log_destination,log_line_prefix
roles: logtest
- name: GRANT ALTER SYSTEM ON PARAMETER primary_conninfo,synchronous_standby_names TO replicamgr
community.postgresql.postgresql_privs:
database: replicamgr
state: present
privs: ALTER_SYSTEM
type: parameter
objs: primary_conninfo,synchronous_standby_names
roles: replicamgr
返回值
常见的返回值已在此处记录,以下是此模块独有的字段
键 |
描述 |
---|---|
已执行查询的列表。 返回:成功 示例: |