community.mongodb.mongodb_user 模块 – 从 MongoDB 数据库添加或删除用户

注意

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

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

要安装它,请使用:ansible-galaxy collection install community.mongodb。您需要进一步的要求才能使用此模块,请参阅 要求 以了解详细信息。

要在 playbook 中使用它,请指定:community.mongodb.mongodb_user

community.mongodb 1.0.0 中的新功能

概要

  • 从 MongoDB 数据库添加或删除用户。

要求

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

  • pymongo

参数

参数

注释

atlas_auth

布尔值

用于 MongoDB Atlas 实例的身份验证路径

选择

  • false ←(默认)

  • true

auth_mechanism

字符串

身份验证类型。

选择

  • "SCRAM-SHA-256"

  • "SCRAM-SHA-1"

  • "MONGODB-X509"

  • "GSSAPI"

  • "PLAIN"

connection_options

列表 / 元素=任意

其他连接选项。

以字典列表或包含用“=”分隔的键值对的字符串形式提供。

create_for_localhost_exception

路径

此参数仅用于处理围绕 localhost 异常的特殊处理。

如果定义了 login_user,则 localhost 异常不活动,此参数无效。

如果此文件不存在(且未定义 login_user),则在成功添加用户后创建此文件。

如果此文件存在(且未定义 login_user),则跳过此任务。

database

别名:db

字符串 / 必需

从中添加/删除用户的数据库的名称。

login_database

字符串

存储登录凭据的数据库。

默认: "admin"

login_host

字符串

要登录的运行 MongoDB 实例的主机。

默认: "localhost"

login_password

字符串

用于身份验证的密码。

指定 login_user 时是必需的。

login_port

整数

要登录的 MongoDB 服务器端口。

默认: 27017

login_user

字符串

用于登录的 MongoDB 用户。

指定 login_password 时是必需的。

name

别名:user

字符串 / 必需

要添加或删除的用户的名称。

password

别名:pass

字符串

要用于用户的密码。

replica_set

字符串

要连接的副本集(自动连接到主节点进行写入)。

roles

列表 / 元素=任意

数据库用户角色,有效值可以是以下一个或多个字符串:“read”、“readWrite”、“dbAdmin”、“userAdmin”、“clusterAdmin”、“readAnyDatabase”、“readWriteAnyDatabase”、“userAdminAnyDatabase”、“dbAdminAnyDatabase”

或以下字典“{ db: DATABASE_NAME, role: ROLE_NAME }”。

此参数需要 pymongo 2.5+。 如果它是一个字符串,则还需要 mongodb 2.4+。 如果它是一个字典,则需要 mongo 2.6+。

ssl

别名:tls

布尔值

连接到数据库时是否使用 SSL 连接。

选择

  • false ←(默认)

  • true

ssl_ca_certs

别名:tlsCAFile

字符串

ssl_ca_certs 选项采用 CA 文件的路径。

ssl_cert_reqs

别名:tlsAllowInvalidCertificates

字符串

指定是否需要来自连接另一端的证书,以及如果提供证书是否会对其进行验证。

选择

  • "CERT_NONE"

  • "CERT_OPTIONAL"

  • "CERT_REQUIRED" ←(默认)

ssl_certfile

别名:tlsCertificateKeyFile

字符串

使用 ssl_certfile 选项呈现客户端证书。

ssl_crlfile

字符串

ssl_crlfile 选项采用 CRL 文件的路径。

ssl_keyfile

字符串

客户端证书的私钥。

ssl_pem_passphrase

别名:tlsCertificateKeyFilePassword

字符串

用于解密加密私钥的密码。

state

字符串

数据库用户的状态。

选择

  • "absent"

  • "present" ←(默认)

strict_compatibility

布尔值

强制执行 pymongo 和 MongoDB 软件版本的严格要求

选择

  • false

  • true ←(默认)

update_password

字符串

always 将始终更新密码并导致模块返回已更改。

on_create 将仅为新创建的用户设置密码。

必须为 always 才能在添加第一个管理员用户时使用 localhost 异常。

当 login_database 为“$external”时,此选项实际上会被忽略。 默认为“on_create”是为了保持特定模块行为。

选择

  • "always" ← (默认)

  • "on_create"

备注

注意

示例

- name: Create 'burgers' database user with name 'bob' and password '12345'.
  community.mongodb.mongodb_user:
    database: burgers
    name: bob
    password: 12345
    state: present

- name: Create a database user via SSL (MongoDB must be compiled with the SSL option and configured properly)
  community.mongodb.mongodb_user:
    database: burgers
    name: bob
    password: 12345
    state: present
    ssl: True

- name: Delete 'burgers' database user with name 'bob'.
  community.mongodb.mongodb_user:
    database: burgers
    name: bob
    state: absent

- name: Define more users with various specific roles (if not defined, no roles is assigned, and the user will be added via pre mongo 2.2 style)
  community.mongodb.mongodb_user:
    database: burgers
    name: ben
    password: 12345
    roles: read
    state: present

- name: Define roles
  community.mongodb.mongodb_user:
    database: burgers
    name: jim
    password: 12345
    roles: readWrite,dbAdmin,userAdmin
    state: present

- name: Define roles
  community.mongodb.mongodb_user:
    database: burgers
    name: joe
    password: 12345
    roles: readWriteAnyDatabase
    state: present

- name: Add a user to database in a replica set, the primary server is automatically discovered and written to
  community.mongodb.mongodb_user:
    database: burgers
    name: bob
    replica_set: belcher
    password: 12345
    roles: readWriteAnyDatabase
    state: present

# add a user 'oplog_reader' with read only access to the 'local' database on the replica_set 'belcher'. This is useful for oplog access (MONGO_OPLOG_URL).
# please notice the credentials must be added to the 'admin' database because the 'local' database is not synchronized and can't receive user credentials
# To login with such user, the connection string should be MONGO_OPLOG_URL="mongodb://oplog_reader:oplog_reader_password@server1,server2/local?authSource=admin"
# This syntax requires mongodb 2.6+ and pymongo 2.5+
- name: Roles as a dictionary
  community.mongodb.mongodb_user:
    login_user: root
    login_password: root_password
    database: admin
    user: oplog_reader
    password: oplog_reader_password
    state: present
    replica_set: belcher
    roles:
      - db: local
        role: read

- name: Adding a user with X.509 Member Authentication
  community.mongodb.mongodb_user:
    login_host: "mongodb-host.test"
    login_port: 27001
    login_database: "$external"
    database: "admin"
    name: "admin"
    password: "test"
    roles:
    - dbAdminAnyDatabase
    ssl: true
    ssl_ca_certs: "/tmp/ca.crt"
    ssl_certfile: "/tmp/tls.key" #cert and key in one file
    state: present
    auth_mechanism: "MONGODB-X509"
    connection_options:
     - "tlsAllowInvalidHostnames=true"

返回值

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

描述

用户

字符串

要添加或删除的用户的名称。

返回值: 成功

作者

  • Elliott Foster (@elliotttf)

  • Julien Thebault (@Lujeni)