| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
__metaclass__ = type |
| 20 |
|
| 21 |
|
| 22 |
DOCUMENTATION = ''' |
| 23 |
--- |
| 24 |
module: user |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Users |
| 27 |
description: |
| 28 |
- Create, update, and delete users |
| 29 |
author: |
| 30 |
- "Christoffer Reijer (@ephracis) Basalt AB" |
| 31 |
options: |
| 32 |
login: |
| 33 |
aliases: |
| 34 |
- name |
| 35 |
description: |
| 36 |
- Name of the user |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
firstname: |
| 40 |
description: |
| 41 |
- First name of the user |
| 42 |
required: false |
| 43 |
type: str |
| 44 |
lastname: |
| 45 |
description: |
| 46 |
- Last name of the user |
| 47 |
required: false |
| 48 |
type: str |
| 49 |
mail: |
| 50 |
description: |
| 51 |
- Email address of the user |
| 52 |
- Required when creating a new user |
| 53 |
required: false |
| 54 |
type: str |
| 55 |
description: |
| 56 |
description: |
| 57 |
- Description of the user |
| 58 |
required: false |
| 59 |
type: str |
| 60 |
admin: |
| 61 |
description: |
| 62 |
- Whether or not the user is an administrator |
| 63 |
required: false |
| 64 |
type: bool |
| 65 |
user_password: |
| 66 |
description: |
| 67 |
- Password for the user. |
| 68 |
- When this parameter is set, the module will not be idempotent. |
| 69 |
required: false |
| 70 |
type: str |
| 71 |
default_location: |
| 72 |
description: |
| 73 |
- The location that the user uses by default |
| 74 |
required: false |
| 75 |
type: str |
| 76 |
default_organization: |
| 77 |
description: |
| 78 |
- The organization that the user uses by default |
| 79 |
required: false |
| 80 |
type: str |
| 81 |
auth_source: |
| 82 |
description: |
| 83 |
- Authentication source where the user exists |
| 84 |
required: false |
| 85 |
type: str |
| 86 |
timezone: |
| 87 |
description: |
| 88 |
- Timezone for the user |
| 89 |
- If blank it will use the browser timezone. |
| 90 |
required: false |
| 91 |
type: str |
| 92 |
choices: |
| 93 |
- 'International Date Line West' |
| 94 |
- 'American Samoa' |
| 95 |
- 'Midway Island' |
| 96 |
- 'Hawaii' |
| 97 |
- 'Alaska' |
| 98 |
- 'Pacific Time (US & Canada)' |
| 99 |
- 'Tijuana' |
| 100 |
- 'Arizona' |
| 101 |
- 'Chihuahua' |
| 102 |
- 'Mazatlan' |
| 103 |
- 'Mountain Time (US & Canada)' |
| 104 |
- 'Central America' |
| 105 |
- 'Central Time (US & Canada)' |
| 106 |
- 'Guadalajara' |
| 107 |
- 'Mexico City' |
| 108 |
- 'Monterrey' |
| 109 |
- 'Saskatchewan' |
| 110 |
- 'Bogota' |
| 111 |
- 'Eastern Time (US & Canada)' |
| 112 |
- 'Indiana (East)' |
| 113 |
- 'Lima' |
| 114 |
- 'Quito' |
| 115 |
- 'Atlantic Time (Canada)' |
| 116 |
- 'Caracas' |
| 117 |
- 'Georgetown' |
| 118 |
- 'La Paz' |
| 119 |
- 'Puerto Rico' |
| 120 |
- 'Santiago' |
| 121 |
- 'Newfoundland' |
| 122 |
- 'Brasilia' |
| 123 |
- 'Buenos Aires' |
| 124 |
- 'Greenland' |
| 125 |
- 'Montevideo' |
| 126 |
- 'Mid-Atlantic' |
| 127 |
- 'Azores' |
| 128 |
- 'Cape Verde Is.' |
| 129 |
- 'Dublin' |
| 130 |
- 'Edinburgh' |
| 131 |
- 'Lisbon' |
| 132 |
- 'London' |
| 133 |
- 'Monrovia' |
| 134 |
- 'UTC' |
| 135 |
- 'Amsterdam' |
| 136 |
- 'Belgrade' |
| 137 |
- 'Berlin' |
| 138 |
- 'Bern' |
| 139 |
- 'Bratislava' |
| 140 |
- 'Brussels' |
| 141 |
- 'Budapest' |
| 142 |
- 'Casablanca' |
| 143 |
- 'Copenhagen' |
| 144 |
- 'Ljubljana' |
| 145 |
- 'Madrid' |
| 146 |
- 'Paris' |
| 147 |
- 'Prague' |
| 148 |
- 'Rome' |
| 149 |
- 'Sarajevo' |
| 150 |
- 'Skopje' |
| 151 |
- 'Stockholm' |
| 152 |
- 'Vienna' |
| 153 |
- 'Warsaw' |
| 154 |
- 'West Central Africa' |
| 155 |
- 'Zagreb' |
| 156 |
- 'Zurich' |
| 157 |
- 'Athens' |
| 158 |
- 'Bucharest' |
| 159 |
- 'Cairo' |
| 160 |
- 'Harare' |
| 161 |
- 'Helsinki' |
| 162 |
- 'Jerusalem' |
| 163 |
- 'Kaliningrad' |
| 164 |
- 'Kyiv' |
| 165 |
- 'Pretoria' |
| 166 |
- 'Riga' |
| 167 |
- 'Sofia' |
| 168 |
- 'Tallinn' |
| 169 |
- 'Vilnius' |
| 170 |
- 'Baghdad' |
| 171 |
- 'Istanbul' |
| 172 |
- 'Kuwait' |
| 173 |
- 'Minsk' |
| 174 |
- 'Moscow' |
| 175 |
- 'Nairobi' |
| 176 |
- 'Riyadh' |
| 177 |
- 'St. Petersburg' |
| 178 |
- 'Tehran' |
| 179 |
- 'Abu Dhabi' |
| 180 |
- 'Baku' |
| 181 |
- 'Muscat' |
| 182 |
- 'Samara' |
| 183 |
- 'Tbilisi' |
| 184 |
- 'Volgograd' |
| 185 |
- 'Yerevan' |
| 186 |
- 'Kabul' |
| 187 |
- 'Ekaterinburg' |
| 188 |
- 'Islamabad' |
| 189 |
- 'Karachi' |
| 190 |
- 'Tashkent' |
| 191 |
- 'Chennai' |
| 192 |
- 'Kolkata' |
| 193 |
- 'Mumbai' |
| 194 |
- 'New Delhi' |
| 195 |
- 'Sri Jayawardenepura' |
| 196 |
- 'Kathmandu' |
| 197 |
- 'Almaty' |
| 198 |
- 'Astana' |
| 199 |
- 'Dhaka' |
| 200 |
- 'Urumqi' |
| 201 |
- 'Rangoon' |
| 202 |
- 'Bangkok' |
| 203 |
- 'Hanoi' |
| 204 |
- 'Jakarta' |
| 205 |
- 'Krasnoyarsk' |
| 206 |
- 'Novosibirsk' |
| 207 |
- 'Beijing' |
| 208 |
- 'Chongqing' |
| 209 |
- 'Hong Kong' |
| 210 |
- 'Irkutsk' |
| 211 |
- 'Kuala Lumpur' |
| 212 |
- 'Perth' |
| 213 |
- 'Singapore' |
| 214 |
- 'Taipei' |
| 215 |
- 'Ulaanbaatar' |
| 216 |
- 'Osaka' |
| 217 |
- 'Sapporo' |
| 218 |
- 'Seoul' |
| 219 |
- 'Tokyo' |
| 220 |
- 'Yakutsk' |
| 221 |
- 'Adelaide' |
| 222 |
- 'Darwin' |
| 223 |
- 'Brisbane' |
| 224 |
- 'Canberra' |
| 225 |
- 'Guam' |
| 226 |
- 'Hobart' |
| 227 |
- 'Melbourne' |
| 228 |
- 'Port Moresby' |
| 229 |
- 'Sydney' |
| 230 |
- 'Vladivostok' |
| 231 |
- 'Magadan' |
| 232 |
- 'New Caledonia' |
| 233 |
- 'Solomon Is.' |
| 234 |
- 'Srednekolymsk' |
| 235 |
- 'Auckland' |
| 236 |
- 'Fiji' |
| 237 |
- 'Kamchatka' |
| 238 |
- 'Marshall Is.' |
| 239 |
- 'Wellington' |
| 240 |
- 'Chatham Is.' |
| 241 |
- "Nuku'alofa" |
| 242 |
- 'Samoa' |
| 243 |
- 'Tokelau Is.' |
| 244 |
locale: |
| 245 |
description: |
| 246 |
- The language locale for the user |
| 247 |
required: false |
| 248 |
type: str |
| 249 |
choices: |
| 250 |
- 'ca' |
| 251 |
- 'de' |
| 252 |
- 'en' |
| 253 |
- 'en_GB' |
| 254 |
- 'es' |
| 255 |
- 'fr' |
| 256 |
- 'gl' |
| 257 |
- 'it' |
| 258 |
- 'ja' |
| 259 |
- 'ko' |
| 260 |
- 'nl_NL' |
| 261 |
- 'pl' |
| 262 |
- 'pt_BR' |
| 263 |
- 'ru' |
| 264 |
- 'sv_SE' |
| 265 |
- 'zh_CN' |
| 266 |
- 'zh_TW' |
| 267 |
roles: |
| 268 |
description: |
| 269 |
- List of roles assigned to the user |
| 270 |
required: false |
| 271 |
type: list |
| 272 |
elements: str |
| 273 |
extends_documentation_fragment: |
| 274 |
- theforeman.foreman.foreman |
| 275 |
- theforeman.foreman.foreman.entity_state |
| 276 |
- theforeman.foreman.foreman.taxonomy |
| 277 |
''' |
| 278 |
|
| 279 |
EXAMPLES = ''' |
| 280 |
- name: Create a user |
| 281 |
theforeman.foreman.user: |
| 282 |
name: test |
| 283 |
firstname: Test |
| 284 |
lastname: Userson |
| 285 |
mail: [email protected] |
| 286 |
description: Dr. Test Userson |
| 287 |
admin: false |
| 288 |
user_password: s3cret |
| 289 |
default_location: Test Location |
| 290 |
default_organization: Test Organization |
| 291 |
auth_source: Internal |
| 292 |
timezone: Stockholm |
| 293 |
locale: sv_SE |
| 294 |
roles: |
| 295 |
- Manager |
| 296 |
locations: |
| 297 |
- Test Location |
| 298 |
organizations: |
| 299 |
- Test Organization |
| 300 |
state: present |
| 301 |
|
| 302 |
- name: Update a user |
| 303 |
theforeman.foreman.user: |
| 304 |
name: test |
| 305 |
firstname: Tester |
| 306 |
state: present |
| 307 |
|
| 308 |
- name: Change password |
| 309 |
theforeman.foreman.user: |
| 310 |
name: test |
| 311 |
user_password: newp@ss |
| 312 |
''' |
| 313 |
|
| 314 |
RETURN = ''' |
| 315 |
entity: |
| 316 |
description: Final state of the affected entities grouped by their type. |
| 317 |
returned: success |
| 318 |
type: dict |
| 319 |
contains: |
| 320 |
users: |
| 321 |
description: List of users. |
| 322 |
type: list |
| 323 |
elements: dict |
| 324 |
''' |
| 325 |
|
| 326 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ( |
| 327 |
ForemanTaxonomicEntityAnsibleModule, |
| 328 |
) |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
timezone_list = [ |
| 333 |
'International Date Line West', |
| 334 |
'American Samoa', |
| 335 |
'Midway Island', |
| 336 |
'Hawaii', |
| 337 |
'Alaska', |
| 338 |
'Pacific Time (US & Canada)', |
| 339 |
'Tijuana', |
| 340 |
'Arizona', |
| 341 |
'Chihuahua', |
| 342 |
'Mazatlan', |
| 343 |
'Mountain Time (US & Canada)', |
| 344 |
'Central America', |
| 345 |
'Central Time (US & Canada)', |
| 346 |
'Guadalajara', |
| 347 |
'Mexico City', |
| 348 |
'Monterrey', |
| 349 |
'Saskatchewan', |
| 350 |
'Bogota', |
| 351 |
'Eastern Time (US & Canada)', |
| 352 |
'Indiana (East)', |
| 353 |
'Lima', |
| 354 |
'Quito', |
| 355 |
'Atlantic Time (Canada)', |
| 356 |
'Caracas', |
| 357 |
'Georgetown', |
| 358 |
'La Paz', |
| 359 |
'Puerto Rico', |
| 360 |
'Santiago', |
| 361 |
'Newfoundland', |
| 362 |
'Brasilia', |
| 363 |
'Buenos Aires', |
| 364 |
'Greenland', |
| 365 |
'Montevideo', |
| 366 |
'Mid-Atlantic', |
| 367 |
'Azores', |
| 368 |
'Cape Verde Is.', |
| 369 |
'Dublin', |
| 370 |
'Edinburgh', |
| 371 |
'Lisbon', |
| 372 |
'London', |
| 373 |
'Monrovia', |
| 374 |
'UTC', |
| 375 |
'Amsterdam', |
| 376 |
'Belgrade', |
| 377 |
'Berlin', |
| 378 |
'Bern', |
| 379 |
'Bratislava', |
| 380 |
'Brussels', |
| 381 |
'Budapest', |
| 382 |
'Casablanca', |
| 383 |
'Copenhagen', |
| 384 |
'Ljubljana', |
| 385 |
'Madrid', |
| 386 |
'Paris', |
| 387 |
'Prague', |
| 388 |
'Rome', |
| 389 |
'Sarajevo', |
| 390 |
'Skopje', |
| 391 |
'Stockholm', |
| 392 |
'Vienna', |
| 393 |
'Warsaw', |
| 394 |
'West Central Africa', |
| 395 |
'Zagreb', |
| 396 |
'Zurich', |
| 397 |
'Athens', |
| 398 |
'Bucharest', |
| 399 |
'Cairo', |
| 400 |
'Harare', |
| 401 |
'Helsinki', |
| 402 |
'Jerusalem', |
| 403 |
'Kaliningrad', |
| 404 |
'Kyiv', |
| 405 |
'Pretoria', |
| 406 |
'Riga', |
| 407 |
'Sofia', |
| 408 |
'Tallinn', |
| 409 |
'Vilnius', |
| 410 |
'Baghdad', |
| 411 |
'Istanbul', |
| 412 |
'Kuwait', |
| 413 |
'Minsk', |
| 414 |
'Moscow', |
| 415 |
'Nairobi', |
| 416 |
'Riyadh', |
| 417 |
'St. Petersburg', |
| 418 |
'Tehran', |
| 419 |
'Abu Dhabi', |
| 420 |
'Baku', |
| 421 |
'Muscat', |
| 422 |
'Samara', |
| 423 |
'Tbilisi', |
| 424 |
'Volgograd', |
| 425 |
'Yerevan', |
| 426 |
'Kabul', |
| 427 |
'Ekaterinburg', |
| 428 |
'Islamabad', |
| 429 |
'Karachi', |
| 430 |
'Tashkent', |
| 431 |
'Chennai', |
| 432 |
'Kolkata', |
| 433 |
'Mumbai', |
| 434 |
'New Delhi', |
| 435 |
'Sri Jayawardenepura', |
| 436 |
'Kathmandu', |
| 437 |
'Almaty', |
| 438 |
'Astana', |
| 439 |
'Dhaka', |
| 440 |
'Urumqi', |
| 441 |
'Rangoon', |
| 442 |
'Bangkok', |
| 443 |
'Hanoi', |
| 444 |
'Jakarta', |
| 445 |
'Krasnoyarsk', |
| 446 |
'Novosibirsk', |
| 447 |
'Beijing', |
| 448 |
'Chongqing', |
| 449 |
'Hong Kong', |
| 450 |
'Irkutsk', |
| 451 |
'Kuala Lumpur', |
| 452 |
'Perth', |
| 453 |
'Singapore', |
| 454 |
'Taipei', |
| 455 |
'Ulaanbaatar', |
| 456 |
'Osaka', |
| 457 |
'Sapporo', |
| 458 |
'Seoul', |
| 459 |
'Tokyo', |
| 460 |
'Yakutsk', |
| 461 |
'Adelaide', |
| 462 |
'Darwin', |
| 463 |
'Brisbane', |
| 464 |
'Canberra', |
| 465 |
'Guam', |
| 466 |
'Hobart', |
| 467 |
'Melbourne', |
| 468 |
'Port Moresby', |
| 469 |
'Sydney', |
| 470 |
'Vladivostok', |
| 471 |
'Magadan', |
| 472 |
'New Caledonia', |
| 473 |
'Solomon Is.', |
| 474 |
'Srednekolymsk', |
| 475 |
'Auckland', |
| 476 |
'Fiji', |
| 477 |
'Kamchatka', |
| 478 |
'Marshall Is.', |
| 479 |
'Wellington', |
| 480 |
'Chatham Is.', |
| 481 |
"Nuku'alofa", |
| 482 |
'Samoa', |
| 483 |
'Tokelau Is.', |
| 484 |
] |
| 485 |
|
| 486 |
|
| 487 |
locale_list = [ |
| 488 |
'ca', |
| 489 |
'de', |
| 490 |
'en', |
| 491 |
'en_GB', |
| 492 |
'es', |
| 493 |
'fr', |
| 494 |
'gl', |
| 495 |
'it', |
| 496 |
'ja', |
| 497 |
'ko', |
| 498 |
'nl_NL', |
| 499 |
'pl', |
| 500 |
'pt_BR', |
| 501 |
'ru', |
| 502 |
'sv_SE', |
| 503 |
'zh_CN', |
| 504 |
'zh_TW', |
| 505 |
] |
| 506 |
|
| 507 |
|
| 508 |
class ForemanUserModule(ForemanTaxonomicEntityAnsibleModule): |
| 509 |
pass |
| 510 |
|
| 511 |
|
| 512 |
def main(): |
| 513 |
module = ForemanUserModule( |
| 514 |
foreman_spec=dict( |
| 515 |
login=dict(required=True, aliases=['name']), |
| 516 |
firstname=dict(required=False), |
| 517 |
lastname=dict(required=False), |
| 518 |
mail=dict(required=False), |
| 519 |
description=dict(required=False), |
| 520 |
admin=dict(required=False, type='bool'), |
| 521 |
user_password=dict(required=False, no_log=True, flat_name='password'), |
| 522 |
default_location=dict(required=False, type='entity', resource_type='locations'), |
| 523 |
default_organization=dict(required=False, type='entity', resource_type='organizations'), |
| 524 |
auth_source=dict(required=False, type='entity'), |
| 525 |
timezone=dict(required=False, choices=timezone_list), |
| 526 |
locale=dict(required=False, choices=locale_list), |
| 527 |
roles=dict(required=False, type='entity_list'), |
| 528 |
current_password=dict(required=False, no_log=True, invisible=True), |
| 529 |
), |
| 530 |
entity_key='login', |
| 531 |
) |
| 532 |
|
| 533 |
with module.api_connection(): |
| 534 |
entity = module.lookup_entity('entity') |
| 535 |
|
| 536 |
if not module.desired_absent: |
| 537 |
if 'mail' not in module.foreman_params: |
| 538 |
if not entity: |
| 539 |
module.fail_json(msg="The 'mail' parameter is required when creating a new user.") |
| 540 |
else: |
| 541 |
module.foreman_params['mail'] = entity['mail'] |
| 542 |
if module.foreman_params['login'] == module._foremanapi_username and module.foreman_params.get('user_password') is not None: |
| 543 |
module.foreman_params['current_password'] = module._foremanapi_password |
| 544 |
|
| 545 |
module.run() |
| 546 |
|
| 547 |
|
| 548 |
if __name__ == '__main__': |
| 549 |
main() |