| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2017, Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: ipa_sudorule |
| 10 |
author: Thomas Krahn (@Nosmoht) |
| 11 |
short_description: Manage FreeIPA sudo rule |
| 12 |
description: |
| 13 |
- Add, modify or delete sudo rule within IPA server using IPA API. |
| 14 |
attributes: |
| 15 |
check_mode: |
| 16 |
support: full |
| 17 |
diff_mode: |
| 18 |
support: none |
| 19 |
options: |
| 20 |
cn: |
| 21 |
description: |
| 22 |
- Canonical name. |
| 23 |
- Can not be changed as it is the unique identifier. |
| 24 |
required: true |
| 25 |
aliases: ['name'] |
| 26 |
type: str |
| 27 |
cmdcategory: |
| 28 |
description: |
| 29 |
- Command category the rule applies to. |
| 30 |
choices: ['all'] |
| 31 |
type: str |
| 32 |
cmd: |
| 33 |
description: |
| 34 |
- List of commands assigned to the rule. |
| 35 |
- If an empty list is passed all commands are removed from the rule. |
| 36 |
- If option is omitted commands are not checked nor changed. |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
cmdgroup: |
| 40 |
description: |
| 41 |
- List of command groups assigned to the rule. |
| 42 |
- If an empty list is passed all command groups are removed from the rule. |
| 43 |
- If option is omitted command groups are not checked nor changed. |
| 44 |
type: list |
| 45 |
elements: str |
| 46 |
version_added: 2.0.0 |
| 47 |
deny_cmd: |
| 48 |
description: |
| 49 |
- List of denied commands assigned to the rule. |
| 50 |
- If an empty list is passed all commands are removed from the rule. |
| 51 |
- If option is omitted commands are not checked nor changed. |
| 52 |
type: list |
| 53 |
elements: str |
| 54 |
version_added: 8.1.0 |
| 55 |
deny_cmdgroup: |
| 56 |
description: |
| 57 |
- List of denied command groups assigned to the rule. |
| 58 |
- If an empty list is passed all command groups are removed from the rule. |
| 59 |
- If option is omitted command groups are not checked nor changed. |
| 60 |
type: list |
| 61 |
elements: str |
| 62 |
version_added: 8.1.0 |
| 63 |
description: |
| 64 |
description: |
| 65 |
- Description of the sudo rule. |
| 66 |
type: str |
| 67 |
host: |
| 68 |
description: |
| 69 |
- List of hosts assigned to the rule. |
| 70 |
- If an empty list is passed all hosts are removed from the rule. |
| 71 |
- If option is omitted hosts are not checked nor changed. |
| 72 |
- Option O(hostcategory) must be omitted to assign hosts. |
| 73 |
type: list |
| 74 |
elements: str |
| 75 |
hostcategory: |
| 76 |
description: |
| 77 |
- Host category the rule applies to. |
| 78 |
- If V(all) is passed one must omit O(host) and O(hostgroup). |
| 79 |
- Option O(host) and O(hostgroup) must be omitted to assign V(all). |
| 80 |
choices: ['all'] |
| 81 |
type: str |
| 82 |
hostgroup: |
| 83 |
description: |
| 84 |
- List of host groups assigned to the rule. |
| 85 |
- If an empty list is passed all host groups are removed from the rule. |
| 86 |
- If option is omitted host groups are not checked nor changed. |
| 87 |
- Option O(hostcategory) must be omitted to assign host groups. |
| 88 |
type: list |
| 89 |
elements: str |
| 90 |
runasextusers: |
| 91 |
description: |
| 92 |
- List of external RunAs users. |
| 93 |
type: list |
| 94 |
elements: str |
| 95 |
version_added: 2.3.0 |
| 96 |
runasusercategory: |
| 97 |
description: |
| 98 |
- RunAs User category the rule applies to. |
| 99 |
choices: ['all'] |
| 100 |
type: str |
| 101 |
runasgroupcategory: |
| 102 |
description: |
| 103 |
- RunAs Group category the rule applies to. |
| 104 |
choices: ['all'] |
| 105 |
type: str |
| 106 |
sudoopt: |
| 107 |
description: |
| 108 |
- List of options to add to the sudo rule. |
| 109 |
type: list |
| 110 |
elements: str |
| 111 |
user: |
| 112 |
description: |
| 113 |
- List of users assigned to the rule. |
| 114 |
- If an empty list is passed all users are removed from the rule. |
| 115 |
- If option is omitted users are not checked nor changed. |
| 116 |
type: list |
| 117 |
elements: str |
| 118 |
usercategory: |
| 119 |
description: |
| 120 |
- User category the rule applies to. |
| 121 |
choices: ['all'] |
| 122 |
type: str |
| 123 |
usergroup: |
| 124 |
description: |
| 125 |
- List of user groups assigned to the rule. |
| 126 |
- If an empty list is passed all user groups are removed from the rule. |
| 127 |
- If option is omitted user groups are not checked nor changed. |
| 128 |
type: list |
| 129 |
elements: str |
| 130 |
state: |
| 131 |
description: State to ensure. |
| 132 |
default: present |
| 133 |
choices: ['absent', 'disabled', 'enabled', 'present'] |
| 134 |
type: str |
| 135 |
extends_documentation_fragment: |
| 136 |
- community.general._ipa.documentation |
| 137 |
- community.general._ipa.connection_notes |
| 138 |
- community.general._attributes |
| 139 |
""" |
| 140 |
|
| 141 |
EXAMPLES = r""" |
| 142 |
- name: Ensure sudo rule is present that's allows all every body to execute any command on any host without being asked |
| 143 |
for a password. |
| 144 |
community.general.ipa_sudorule: |
| 145 |
name: sudo_all_nopasswd |
| 146 |
cmdcategory: all |
| 147 |
description: Allow to run every command with sudo without password |
| 148 |
hostcategory: all |
| 149 |
sudoopt: |
| 150 |
- '!authenticate' |
| 151 |
usercategory: all |
| 152 |
ipa_host: ipa.example.com |
| 153 |
ipa_user: admin |
| 154 |
ipa_pass: topsecret |
| 155 |
|
| 156 |
- name: Ensure user group developers can run every command on host group db-server as well as on host db01.example.com. |
| 157 |
community.general.ipa_sudorule: |
| 158 |
name: sudo_dev_dbserver |
| 159 |
description: Allow developers to run every command with sudo on all database server |
| 160 |
cmdcategory: all |
| 161 |
host: |
| 162 |
- db01.example.com |
| 163 |
hostgroup: |
| 164 |
- db-server |
| 165 |
sudoopt: |
| 166 |
- '!authenticate' |
| 167 |
usergroup: |
| 168 |
- developers |
| 169 |
ipa_host: ipa.example.com |
| 170 |
ipa_user: admin |
| 171 |
ipa_pass: topsecret |
| 172 |
|
| 173 |
- name: Ensure user group operations can run any commands that is part of operations-cmdgroup on any host as user root. |
| 174 |
community.general.ipa_sudorule: |
| 175 |
name: sudo_operations_all |
| 176 |
description: Allow operators to run any commands that is part of operations-cmdgroup on any host as user root. |
| 177 |
cmdgroup: |
| 178 |
- operations-cmdgroup |
| 179 |
hostcategory: all |
| 180 |
runasextusers: |
| 181 |
- root |
| 182 |
sudoopt: |
| 183 |
- '!authenticate' |
| 184 |
usergroup: |
| 185 |
- operators |
| 186 |
ipa_host: ipa.example.com |
| 187 |
ipa_user: admin |
| 188 |
ipa_pass: topsecret |
| 189 |
""" |
| 190 |
|
| 191 |
RETURN = r""" |
| 192 |
sudorule: |
| 193 |
description: Sudorule as returned by IPA. |
| 194 |
returned: always |
| 195 |
type: dict |
| 196 |
""" |
| 197 |
|
| 198 |
import traceback |
| 199 |
|
| 200 |
from ansible.module_utils.basic import AnsibleModule |
| 201 |
|
| 202 |
from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec |
| 203 |
from ansible_collections.community.general.plugins.module_utils._version import LooseVersion |
| 204 |
|
| 205 |
|
| 206 |
class SudoRuleIPAClient(IPAClient): |
| 207 |
def __init__(self, module, host, port, protocol): |
| 208 |
super().__init__(module, host, port, protocol) |
| 209 |
|
| 210 |
def sudorule_find(self, name): |
| 211 |
return self._post_json(method="sudorule_find", name=None, item={"all": True, "cn": name}) |
| 212 |
|
| 213 |
def sudorule_add(self, name, item): |
| 214 |
return self._post_json(method="sudorule_add", name=name, item=item) |
| 215 |
|
| 216 |
def sudorule_add_runasuser(self, name, item): |
| 217 |
return self._post_json(method="sudorule_add_runasuser", name=name, item={"user": item}) |
| 218 |
|
| 219 |
def sudorule_remove_runasuser(self, name, item): |
| 220 |
return self._post_json(method="sudorule_remove_runasuser", name=name, item={"user": item}) |
| 221 |
|
| 222 |
def sudorule_mod(self, name, item): |
| 223 |
return self._post_json(method="sudorule_mod", name=name, item=item) |
| 224 |
|
| 225 |
def sudorule_del(self, name): |
| 226 |
return self._post_json(method="sudorule_del", name=name) |
| 227 |
|
| 228 |
def sudorule_add_option(self, name, item): |
| 229 |
return self._post_json(method="sudorule_add_option", name=name, item=item) |
| 230 |
|
| 231 |
def sudorule_add_option_ipasudoopt(self, name, item): |
| 232 |
return self.sudorule_add_option(name=name, item={"ipasudoopt": item}) |
| 233 |
|
| 234 |
def sudorule_remove_option(self, name, item): |
| 235 |
return self._post_json(method="sudorule_remove_option", name=name, item=item) |
| 236 |
|
| 237 |
def sudorule_remove_option_ipasudoopt(self, name, item): |
| 238 |
return self.sudorule_remove_option(name=name, item={"ipasudoopt": item}) |
| 239 |
|
| 240 |
def sudorule_add_host(self, name, item): |
| 241 |
return self._post_json(method="sudorule_add_host", name=name, item=item) |
| 242 |
|
| 243 |
def sudorule_add_host_host(self, name, item): |
| 244 |
return self.sudorule_add_host(name=name, item={"host": item}) |
| 245 |
|
| 246 |
def sudorule_add_host_hostgroup(self, name, item): |
| 247 |
return self.sudorule_add_host(name=name, item={"hostgroup": item}) |
| 248 |
|
| 249 |
def sudorule_remove_host(self, name, item): |
| 250 |
return self._post_json(method="sudorule_remove_host", name=name, item=item) |
| 251 |
|
| 252 |
def sudorule_remove_host_host(self, name, item): |
| 253 |
return self.sudorule_remove_host(name=name, item={"host": item}) |
| 254 |
|
| 255 |
def sudorule_remove_host_hostgroup(self, name, item): |
| 256 |
return self.sudorule_remove_host(name=name, item={"hostgroup": item}) |
| 257 |
|
| 258 |
def sudorule_add_allow_command(self, name, item): |
| 259 |
return self._post_json(method="sudorule_add_allow_command", name=name, item={"sudocmd": item}) |
| 260 |
|
| 261 |
def sudorule_add_allow_command_group(self, name, item): |
| 262 |
return self._post_json(method="sudorule_add_allow_command", name=name, item={"sudocmdgroup": item}) |
| 263 |
|
| 264 |
def sudorule_add_deny_command(self, name, item): |
| 265 |
return self._post_json(method="sudorule_add_deny_command", name=name, item={"sudocmd": item}) |
| 266 |
|
| 267 |
def sudorule_add_deny_command_group(self, name, item): |
| 268 |
return self._post_json(method="sudorule_add_deny_command", name=name, item={"sudocmdgroup": item}) |
| 269 |
|
| 270 |
def sudorule_remove_allow_command(self, name, item): |
| 271 |
return self._post_json(method="sudorule_remove_allow_command", name=name, item=item) |
| 272 |
|
| 273 |
def sudorule_add_user(self, name, item): |
| 274 |
return self._post_json(method="sudorule_add_user", name=name, item=item) |
| 275 |
|
| 276 |
def sudorule_add_user_user(self, name, item): |
| 277 |
return self.sudorule_add_user(name=name, item={"user": item}) |
| 278 |
|
| 279 |
def sudorule_add_user_group(self, name, item): |
| 280 |
return self.sudorule_add_user(name=name, item={"group": item}) |
| 281 |
|
| 282 |
def sudorule_remove_user(self, name, item): |
| 283 |
return self._post_json(method="sudorule_remove_user", name=name, item=item) |
| 284 |
|
| 285 |
def sudorule_remove_user_user(self, name, item): |
| 286 |
return self.sudorule_remove_user(name=name, item={"user": item}) |
| 287 |
|
| 288 |
def sudorule_remove_user_group(self, name, item): |
| 289 |
return self.sudorule_remove_user(name=name, item={"group": item}) |
| 290 |
|
| 291 |
|
| 292 |
def get_sudorule_dict( |
| 293 |
cmdcategory=None, |
| 294 |
description=None, |
| 295 |
hostcategory=None, |
| 296 |
ipaenabledflag=None, |
| 297 |
usercategory=None, |
| 298 |
runasgroupcategory=None, |
| 299 |
runasusercategory=None, |
| 300 |
): |
| 301 |
data = {} |
| 302 |
if cmdcategory is not None: |
| 303 |
data["cmdcategory"] = cmdcategory |
| 304 |
if description is not None: |
| 305 |
data["description"] = description |
| 306 |
if hostcategory is not None: |
| 307 |
data["hostcategory"] = hostcategory |
| 308 |
if ipaenabledflag is not None: |
| 309 |
data["ipaenabledflag"] = ipaenabledflag |
| 310 |
if usercategory is not None: |
| 311 |
data["usercategory"] = usercategory |
| 312 |
if runasusercategory is not None: |
| 313 |
data["ipasudorunasusercategory"] = runasusercategory |
| 314 |
if runasgroupcategory is not None: |
| 315 |
data["ipasudorunasgroupcategory"] = runasgroupcategory |
| 316 |
return data |
| 317 |
|
| 318 |
|
| 319 |
def category_changed(module, client, category_name, ipa_sudorule): |
| 320 |
if ipa_sudorule.get(category_name, None) == ["all"]: |
| 321 |
if not module.check_mode: |
| 322 |
# cn is returned as list even with only a single value. |
| 323 |
client.sudorule_mod(name=ipa_sudorule.get("cn")[0], item={category_name: None}) |
| 324 |
return True |
| 325 |
return False |
| 326 |
|
| 327 |
|
| 328 |
def ensure(module, client): |
| 329 |
state = module.params["state"] |
| 330 |
name = module.params["cn"] |
| 331 |
cmd = module.params["cmd"] |
| 332 |
cmdgroup = module.params["cmdgroup"] |
| 333 |
cmdcategory = module.params["cmdcategory"] |
| 334 |
deny_cmd = module.params["deny_cmd"] |
| 335 |
deny_cmdgroup = module.params["deny_cmdgroup"] |
| 336 |
host = module.params["host"] |
| 337 |
hostcategory = module.params["hostcategory"] |
| 338 |
hostgroup = module.params["hostgroup"] |
| 339 |
runasusercategory = module.params["runasusercategory"] |
| 340 |
runasgroupcategory = module.params["runasgroupcategory"] |
| 341 |
runasextusers = module.params["runasextusers"] |
| 342 |
|
| 343 |
ipa_version = client.get_ipa_version() |
| 344 |
if state in ["present", "enabled"]: |
| 345 |
if LooseVersion(ipa_version) < LooseVersion("4.9.10"): |
| 346 |
ipaenabledflag = "TRUE" |
| 347 |
else: |
| 348 |
ipaenabledflag = True |
| 349 |
else: |
| 350 |
if LooseVersion(ipa_version) < LooseVersion("4.9.10"): |
| 351 |
ipaenabledflag = "FALSE" |
| 352 |
else: |
| 353 |
ipaenabledflag = False |
| 354 |
|
| 355 |
sudoopt = module.params["sudoopt"] |
| 356 |
user = module.params["user"] |
| 357 |
usercategory = module.params["usercategory"] |
| 358 |
usergroup = module.params["usergroup"] |
| 359 |
|
| 360 |
module_sudorule = get_sudorule_dict( |
| 361 |
cmdcategory=cmdcategory, |
| 362 |
description=module.params["description"], |
| 363 |
hostcategory=hostcategory, |
| 364 |
ipaenabledflag=ipaenabledflag, |
| 365 |
usercategory=usercategory, |
| 366 |
runasusercategory=runasusercategory, |
| 367 |
runasgroupcategory=runasgroupcategory, |
| 368 |
) |
| 369 |
ipa_sudorule = client.sudorule_find(name=name) |
| 370 |
|
| 371 |
changed = False |
| 372 |
if state in ["present", "disabled", "enabled"]: |
| 373 |
if not ipa_sudorule: |
| 374 |
changed = True |
| 375 |
if not module.check_mode: |
| 376 |
ipa_sudorule = client.sudorule_add(name=name, item=module_sudorule) |
| 377 |
else: |
| 378 |
diff = client.get_diff(ipa_sudorule, module_sudorule) |
| 379 |
if len(diff) > 0: |
| 380 |
changed = True |
| 381 |
if not module.check_mode: |
| 382 |
if "hostcategory" in diff: |
| 383 |
if ipa_sudorule.get("memberhost_host", None) is not None: |
| 384 |
client.sudorule_remove_host_host(name=name, item=ipa_sudorule.get("memberhost_host")) |
| 385 |
if ipa_sudorule.get("memberhost_hostgroup", None) is not None: |
| 386 |
client.sudorule_remove_host_hostgroup( |
| 387 |
name=name, item=ipa_sudorule.get("memberhost_hostgroup") |
| 388 |
) |
| 389 |
|
| 390 |
client.sudorule_mod(name=name, item=module_sudorule) |
| 391 |
|
| 392 |
if cmd is not None: |
| 393 |
changed = category_changed(module, client, "cmdcategory", ipa_sudorule) or changed |
| 394 |
if not module.check_mode: |
| 395 |
client.sudorule_add_allow_command(name=name, item=cmd) |
| 396 |
|
| 397 |
if cmdgroup is not None: |
| 398 |
changed = category_changed(module, client, "cmdcategory", ipa_sudorule) or changed |
| 399 |
if not module.check_mode: |
| 400 |
client.sudorule_add_allow_command_group(name=name, item=cmdgroup) |
| 401 |
|
| 402 |
if deny_cmd is not None: |
| 403 |
changed = category_changed(module, client, "cmdcategory", ipa_sudorule) or changed |
| 404 |
if not module.check_mode: |
| 405 |
client.sudorule_add_deny_command(name=name, item=deny_cmd) |
| 406 |
|
| 407 |
if deny_cmdgroup is not None: |
| 408 |
changed = category_changed(module, client, "cmdcategory", ipa_sudorule) or changed |
| 409 |
if not module.check_mode: |
| 410 |
client.sudorule_add_deny_command_group(name=name, item=deny_cmdgroup) |
| 411 |
|
| 412 |
if runasusercategory is not None: |
| 413 |
changed = category_changed(module, client, "iparunasusercategory", ipa_sudorule) or changed |
| 414 |
|
| 415 |
if runasgroupcategory is not None: |
| 416 |
changed = category_changed(module, client, "iparunasgroupcategory", ipa_sudorule) or changed |
| 417 |
|
| 418 |
if host is not None: |
| 419 |
changed = category_changed(module, client, "hostcategory", ipa_sudorule) or changed |
| 420 |
changed = ( |
| 421 |
client.modify_if_diff( |
| 422 |
name, |
| 423 |
ipa_sudorule.get("memberhost_host", []), |
| 424 |
host, |
| 425 |
client.sudorule_add_host_host, |
| 426 |
client.sudorule_remove_host_host, |
| 427 |
) |
| 428 |
or changed |
| 429 |
) |
| 430 |
|
| 431 |
if hostgroup is not None: |
| 432 |
changed = category_changed(module, client, "hostcategory", ipa_sudorule) or changed |
| 433 |
changed = ( |
| 434 |
client.modify_if_diff( |
| 435 |
name, |
| 436 |
ipa_sudorule.get("memberhost_hostgroup", []), |
| 437 |
hostgroup, |
| 438 |
client.sudorule_add_host_hostgroup, |
| 439 |
client.sudorule_remove_host_hostgroup, |
| 440 |
) |
| 441 |
or changed |
| 442 |
) |
| 443 |
if sudoopt is not None: |
| 444 |
# client.modify_if_diff does not work as each option must be removed/added by its own |
| 445 |
ipa_list = ipa_sudorule.get("ipasudoopt", []) |
| 446 |
module_list = sudoopt |
| 447 |
diff = list(set(ipa_list) - set(module_list)) |
| 448 |
if len(diff) > 0: |
| 449 |
changed = True |
| 450 |
if not module.check_mode: |
| 451 |
for item in diff: |
| 452 |
client.sudorule_remove_option_ipasudoopt(name, item) |
| 453 |
diff = list(set(module_list) - set(ipa_list)) |
| 454 |
if len(diff) > 0: |
| 455 |
changed = True |
| 456 |
if not module.check_mode: |
| 457 |
for item in diff: |
| 458 |
client.sudorule_add_option_ipasudoopt(name, item) |
| 459 |
|
| 460 |
if runasextusers is not None: |
| 461 |
ipa_sudorule_run_as_user = ipa_sudorule.get("ipasudorunasextuser", []) |
| 462 |
diff = list(set(ipa_sudorule_run_as_user) - set(runasextusers)) |
| 463 |
if len(diff) > 0: |
| 464 |
changed = True |
| 465 |
if not module.check_mode: |
| 466 |
for item in diff: |
| 467 |
client.sudorule_remove_runasuser(name=name, item=item) |
| 468 |
diff = list(set(runasextusers) - set(ipa_sudorule_run_as_user)) |
| 469 |
if len(diff) > 0: |
| 470 |
changed = True |
| 471 |
if not module.check_mode: |
| 472 |
for item in diff: |
| 473 |
client.sudorule_add_runasuser(name=name, item=item) |
| 474 |
|
| 475 |
if user is not None: |
| 476 |
changed = category_changed(module, client, "usercategory", ipa_sudorule) or changed |
| 477 |
changed = ( |
| 478 |
client.modify_if_diff( |
| 479 |
name, |
| 480 |
ipa_sudorule.get("memberuser_user", []), |
| 481 |
user, |
| 482 |
client.sudorule_add_user_user, |
| 483 |
client.sudorule_remove_user_user, |
| 484 |
) |
| 485 |
or changed |
| 486 |
) |
| 487 |
if usergroup is not None: |
| 488 |
changed = category_changed(module, client, "usercategory", ipa_sudorule) or changed |
| 489 |
changed = ( |
| 490 |
client.modify_if_diff( |
| 491 |
name, |
| 492 |
ipa_sudorule.get("memberuser_group", []), |
| 493 |
usergroup, |
| 494 |
client.sudorule_add_user_group, |
| 495 |
client.sudorule_remove_user_group, |
| 496 |
) |
| 497 |
or changed |
| 498 |
) |
| 499 |
else: |
| 500 |
if ipa_sudorule: |
| 501 |
changed = True |
| 502 |
if not module.check_mode: |
| 503 |
client.sudorule_del(name) |
| 504 |
|
| 505 |
return changed, client.sudorule_find(name) |
| 506 |
|
| 507 |
|
| 508 |
def main(): |
| 509 |
argument_spec = ipa_argument_spec() |
| 510 |
argument_spec.update( |
| 511 |
cmd=dict(type="list", elements="str"), |
| 512 |
cmdgroup=dict(type="list", elements="str"), |
| 513 |
cmdcategory=dict(type="str", choices=["all"]), |
| 514 |
cn=dict(type="str", required=True, aliases=["name"]), |
| 515 |
deny_cmd=dict(type="list", elements="str"), |
| 516 |
deny_cmdgroup=dict(type="list", elements="str"), |
| 517 |
description=dict(type="str"), |
| 518 |
host=dict(type="list", elements="str"), |
| 519 |
hostcategory=dict(type="str", choices=["all"]), |
| 520 |
hostgroup=dict(type="list", elements="str"), |
| 521 |
runasusercategory=dict(type="str", choices=["all"]), |
| 522 |
runasgroupcategory=dict(type="str", choices=["all"]), |
| 523 |
sudoopt=dict(type="list", elements="str"), |
| 524 |
state=dict(type="str", default="present", choices=["present", "absent", "enabled", "disabled"]), |
| 525 |
user=dict(type="list", elements="str"), |
| 526 |
usercategory=dict(type="str", choices=["all"]), |
| 527 |
usergroup=dict(type="list", elements="str"), |
| 528 |
runasextusers=dict(type="list", elements="str"), |
| 529 |
) |
| 530 |
module = AnsibleModule( |
| 531 |
argument_spec=argument_spec, |
| 532 |
mutually_exclusive=[ |
| 533 |
["cmdcategory", "cmd"], |
| 534 |
["cmdcategory", "deny_cmd"], |
| 535 |
["cmdcategory", "cmdgroup"], |
| 536 |
["cmdcategory", "deny_cmdgroup"], |
| 537 |
["hostcategory", "host"], |
| 538 |
["hostcategory", "hostgroup"], |
| 539 |
["usercategory", "user"], |
| 540 |
["usercategory", "usergroup"], |
| 541 |
], |
| 542 |
supports_check_mode=True, |
| 543 |
) |
| 544 |
|
| 545 |
client = SudoRuleIPAClient( |
| 546 |
module=module, |
| 547 |
host=module.params["ipa_host"], |
| 548 |
port=module.params["ipa_port"], |
| 549 |
protocol=module.params["ipa_prot"], |
| 550 |
) |
| 551 |
try: |
| 552 |
client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"]) |
| 553 |
changed, sudorule = ensure(module, client) |
| 554 |
module.exit_json(changed=changed, sudorule=sudorule) |
| 555 |
except Exception as e: |
| 556 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 557 |
|
| 558 |
|
| 559 |
if __name__ == "__main__": |
| 560 |
main() |