| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2026, John McCall (@lowlydba) |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r''' |
| 8 |
--- |
| 9 |
module: user_role |
| 10 |
short_description: Configures a user's role in a database. |
| 11 |
description: |
| 12 |
- Adds or removes a user's role in a database. |
| 13 |
- Use the I(roles) option to work with multiple roles at once using the add/remove/set pattern. |
| 14 |
version_added: "2.4.0" |
| 15 |
options: |
| 16 |
database: |
| 17 |
description: |
| 18 |
- Database for the user. |
| 19 |
type: str |
| 20 |
required: true |
| 21 |
username: |
| 22 |
description: |
| 23 |
- Name of the user. |
| 24 |
type: str |
| 25 |
required: true |
| 26 |
roles: |
| 27 |
description: |
| 28 |
- A dictionary of roles to manage for the user. |
| 29 |
- Supports three keys C(add), C(remove), and C(set). |
| 30 |
- C(add) adds the user to the specified roles. An empty list is a no-op and returns current membership. |
| 31 |
- C(remove) removes the user from the specified roles. An empty list is a no-op and returns current membership. |
| 32 |
- C(set) replaces all current roles with the specified roles. An empty list removes all role memberships. |
| 33 |
- C(set) cannot be combined with C(add) or C(remove). |
| 34 |
- At least one key must be present. |
| 35 |
type: dict |
| 36 |
required: false |
| 37 |
version_added: "2.8.0" |
| 38 |
suboptions: |
| 39 |
add: |
| 40 |
description: |
| 41 |
- A list of role names to add the user to. May be empty to query current membership without changes. |
| 42 |
type: list |
| 43 |
elements: str |
| 44 |
remove: |
| 45 |
description: |
| 46 |
- A list of role names to remove the user from. May be empty to query current membership without changes. |
| 47 |
type: list |
| 48 |
elements: str |
| 49 |
set: |
| 50 |
description: |
| 51 |
- A list of role names that replaces the user's current roles. An empty list removes all role memberships. |
| 52 |
type: list |
| 53 |
elements: str |
| 54 |
role: |
| 55 |
description: |
| 56 |
- The database role for the user to be modified. |
| 57 |
- "B(Deprecated:) This parameter is deprecated and will be removed in version 3.0.0. Use I(roles) instead." |
| 58 |
type: str |
| 59 |
required: false |
| 60 |
state: |
| 61 |
description: |
| 62 |
- Desired state of the user role membership. |
| 63 |
- "Only applicable when using the I(role) parameter (legacy mode). Cannot be used with I(roles)." |
| 64 |
type: str |
| 65 |
choices: |
| 66 |
- present |
| 67 |
- absent |
| 68 |
author: "John McCall (@lowlydba)" |
| 69 |
requirements: |
| 70 |
- L(dbatools,https://www.powershellgallery.com/packages/dbatools/) PowerShell module |
| 71 |
extends_documentation_fragment: |
| 72 |
- lowlydba.sqlserver.sql_credentials |
| 73 |
- lowlydba.sqlserver.attributes.check_mode |
| 74 |
- lowlydba.sqlserver.attributes.platform_all |
| 75 |
''' |
| 76 |
|
| 77 |
EXAMPLES = r''' |
| 78 |
- name: Add a user to a fixed db role (legacy) |
| 79 |
lowlydba.sqlserver.user_role: |
| 80 |
sql_instance: sql-01.myco.io |
| 81 |
username: TheIntern |
| 82 |
database: InternProject1 |
| 83 |
role: db_owner |
| 84 |
|
| 85 |
- name: Remove a user from a fixed db role (legacy) |
| 86 |
lowlydba.sqlserver.user_role: |
| 87 |
sql_instance: sql-01.myco.io |
| 88 |
username: TheIntern |
| 89 |
database: InternProject1 |
| 90 |
role: db_owner |
| 91 |
state: absent |
| 92 |
|
| 93 |
- name: Add user to multiple roles |
| 94 |
lowlydba.sqlserver.user_role: |
| 95 |
sql_instance: sql-01.myco.io |
| 96 |
username: TheIntern |
| 97 |
database: InternProject1 |
| 98 |
roles: |
| 99 |
add: |
| 100 |
- db_owner |
| 101 |
- db_datareader |
| 102 |
|
| 103 |
- name: Remove user from multiple roles |
| 104 |
lowlydba.sqlserver.user_role: |
| 105 |
sql_instance: sql-01.myco.io |
| 106 |
username: TheIntern |
| 107 |
database: InternProject1 |
| 108 |
roles: |
| 109 |
remove: |
| 110 |
- db_owner |
| 111 |
- db_datareader |
| 112 |
|
| 113 |
- name: Set user's roles (replace all current roles) |
| 114 |
lowlydba.sqlserver.user_role: |
| 115 |
sql_instance: sql-01.myco.io |
| 116 |
username: TheIntern |
| 117 |
database: InternProject1 |
| 118 |
roles: |
| 119 |
set: |
| 120 |
- db_datareader |
| 121 |
- db_datawriter |
| 122 |
|
| 123 |
- name: Combine add and remove operations |
| 124 |
lowlydba.sqlserver.user_role: |
| 125 |
sql_instance: sql-01.myco.io |
| 126 |
username: TheIntern |
| 127 |
database: InternProject1 |
| 128 |
roles: |
| 129 |
add: |
| 130 |
- db_securityadmin |
| 131 |
remove: |
| 132 |
- db_owner |
| 133 |
''' |
| 134 |
|
| 135 |
RETURN = r''' |
| 136 |
data: |
| 137 |
description: |
| 138 |
- For the C(roles) parameter - a summary object containing current role membership and any roles added or removed. |
| 139 |
- For the legacy C(role) parameter - output from C(Add-DbaDbRoleMember) or C(Remove-DbaDbRoleMember). Not returned in check_mode. |
| 140 |
returned: success |
| 141 |
type: dict |
| 142 |
contains: |
| 143 |
roleMembership: |
| 144 |
description: List of roles the user is currently a member of. In check_mode reflects state before any changes. |
| 145 |
type: list |
| 146 |
sample: ["db_owner", "db_datareader"] |
| 147 |
added: |
| 148 |
description: List of roles that were added (or would be added in check_mode). |
| 149 |
type: list |
| 150 |
elements: str |
| 151 |
removed: |
| 152 |
description: List of roles that were removed (or would be removed in check_mode). |
| 153 |
type: list |
| 154 |
elements: str |
| 155 |
''' |