| 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: job_template |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Job Templates |
| 27 |
description: |
| 28 |
- Manage Remote Execution Job Templates |
| 29 |
author: |
| 30 |
- "Manuel Bonk (@manuelbonk) ATIX AG" |
| 31 |
- "Matthias Dellweg (@mdellweg) ATIX AG" |
| 32 |
options: |
| 33 |
audit_comment: |
| 34 |
description: |
| 35 |
- Content of the audit comment field |
| 36 |
type: str |
| 37 |
description_format: |
| 38 |
description: |
| 39 |
- description of the job template. Template inputs can be referenced. |
| 40 |
type: str |
| 41 |
file_name: |
| 42 |
description: |
| 43 |
- The path of a template file, that shall be imported. |
| 44 |
- Either this or I(template) is required as a source for the Job Template "content". |
| 45 |
type: path |
| 46 |
job_category: |
| 47 |
description: |
| 48 |
- The category the template should be assigend to |
| 49 |
type: str |
| 50 |
locked: |
| 51 |
description: |
| 52 |
- Determines whether the template shall be locked |
| 53 |
default: false |
| 54 |
type: bool |
| 55 |
name: |
| 56 |
description: |
| 57 |
- The name of the Job Template. |
| 58 |
- If omited, will be determined from the C(name) header of the template or the filename (in that order). |
| 59 |
- The special value "*" can be used to perform bulk actions (modify, delete) on all existing templates. |
| 60 |
type: str |
| 61 |
provider_type: |
| 62 |
description: |
| 63 |
- Determines via which provider the template shall be executed |
| 64 |
required: false |
| 65 |
type: str |
| 66 |
snippet: |
| 67 |
description: |
| 68 |
- Determines whether the template shall be a snippet |
| 69 |
type: bool |
| 70 |
template: |
| 71 |
description: |
| 72 |
- The content of the Job Template. |
| 73 |
- Either this or I(file_name) is required as a source for the Job Template "content". |
| 74 |
type: str |
| 75 |
template_inputs: |
| 76 |
description: |
| 77 |
- The template inputs used in the Job Template |
| 78 |
type: list |
| 79 |
elements: dict |
| 80 |
suboptions: |
| 81 |
advanced: |
| 82 |
description: |
| 83 |
- Template Input is advanced |
| 84 |
type: bool |
| 85 |
description: |
| 86 |
description: |
| 87 |
- description of the Template Input |
| 88 |
type: str |
| 89 |
fact_name: |
| 90 |
description: |
| 91 |
- Fact name to use. |
| 92 |
- Required when I(input_type=fact). |
| 93 |
type: str |
| 94 |
input_type: |
| 95 |
description: |
| 96 |
- input type |
| 97 |
required: true |
| 98 |
choices: |
| 99 |
- user |
| 100 |
- fact |
| 101 |
- variable |
| 102 |
- puppet_parameter |
| 103 |
type: str |
| 104 |
name: |
| 105 |
description: |
| 106 |
- name of the Template Input |
| 107 |
required: true |
| 108 |
type: str |
| 109 |
options: |
| 110 |
description: |
| 111 |
- Template values for user inputs. Must be an array of any type. |
| 112 |
type: list |
| 113 |
elements: raw |
| 114 |
puppet_class_name: |
| 115 |
description: |
| 116 |
- Puppet class name. |
| 117 |
- Required when I(input_type=puppet_parameter). |
| 118 |
type: str |
| 119 |
puppet_parameter_name: |
| 120 |
description: |
| 121 |
- Puppet parameter name. |
| 122 |
- Required when I(input_type=puppet_parameter). |
| 123 |
type: str |
| 124 |
required: |
| 125 |
description: |
| 126 |
- Is the input required |
| 127 |
type: bool |
| 128 |
variable_name: |
| 129 |
description: |
| 130 |
- Variable name to use. |
| 131 |
- Required when I(input_type=variable). |
| 132 |
type: str |
| 133 |
value_type: |
| 134 |
description: |
| 135 |
- Type of the value |
| 136 |
choices: |
| 137 |
- plain |
| 138 |
- search |
| 139 |
- date |
| 140 |
- resource |
| 141 |
type: str |
| 142 |
resource_type: |
| 143 |
description: |
| 144 |
- Type of the resource |
| 145 |
type: str |
| 146 |
hidden_value: |
| 147 |
description: |
| 148 |
- The value contains sensitive information and should't be normally visible, useful e.g. for passwords |
| 149 |
type: bool |
| 150 |
default: |
| 151 |
description: |
| 152 |
- Default value for user input |
| 153 |
type: str |
| 154 |
version_added: 3.8.0 |
| 155 |
extends_documentation_fragment: |
| 156 |
- theforeman.foreman.foreman |
| 157 |
- theforeman.foreman.foreman.entity_state_with_defaults |
| 158 |
- theforeman.foreman.foreman.taxonomy |
| 159 |
''' |
| 160 |
|
| 161 |
EXAMPLES = ''' |
| 162 |
|
| 163 |
- name: "Create a Job Template inline" |
| 164 |
theforeman.foreman.job_template: |
| 165 |
username: "admin" |
| 166 |
password: "changeme" |
| 167 |
server_url: "https://foreman.example.com" |
| 168 |
name: A New Job Template |
| 169 |
state: present |
| 170 |
template: | |
| 171 |
<%# |
| 172 |
name: A Job Template |
| 173 |
%> |
| 174 |
rm -rf <%= input("toDelete") %> |
| 175 |
template_inputs: |
| 176 |
- name: toDelete |
| 177 |
input_type: user |
| 178 |
locations: |
| 179 |
- Gallifrey |
| 180 |
organizations: |
| 181 |
- TARDIS INC |
| 182 |
|
| 183 |
- name: "Create a Job Template from a file" |
| 184 |
theforeman.foreman.job_template: |
| 185 |
username: "admin" |
| 186 |
password: "changeme" |
| 187 |
server_url: "https://foreman.example.com" |
| 188 |
name: a new job template |
| 189 |
file_name: timeywimey_template.erb |
| 190 |
template_inputs: |
| 191 |
- name: a new template input |
| 192 |
input_type: user |
| 193 |
state: present |
| 194 |
locations: |
| 195 |
- Gallifrey |
| 196 |
organizations: |
| 197 |
- TARDIS INC |
| 198 |
|
| 199 |
- name: "remove a job template's template inputs" |
| 200 |
theforeman.foreman.job_template: |
| 201 |
username: "admin" |
| 202 |
password: "changeme" |
| 203 |
server_url: "https://foreman.example.com" |
| 204 |
name: a new job template |
| 205 |
template_inputs: [] |
| 206 |
state: present |
| 207 |
locations: |
| 208 |
- Gallifrey |
| 209 |
organizations: |
| 210 |
- TARDIS INC |
| 211 |
|
| 212 |
- name: "Delete a Job Template" |
| 213 |
theforeman.foreman.job_template: |
| 214 |
username: "admin" |
| 215 |
password: "changeme" |
| 216 |
server_url: "https://foreman.example.com" |
| 217 |
name: timeywimey |
| 218 |
state: absent |
| 219 |
|
| 220 |
- name: "Create a Job Template from a file and modify with parameter(s)" |
| 221 |
theforeman.foreman.job_template: |
| 222 |
username: "admin" |
| 223 |
password: "changeme" |
| 224 |
server_url: "https://foreman.example.com" |
| 225 |
file_name: timeywimey_template.erb |
| 226 |
name: Wibbly Wobbly Template |
| 227 |
state: present |
| 228 |
locations: |
| 229 |
- Gallifrey |
| 230 |
organizations: |
| 231 |
- TARDIS INC |
| 232 |
|
| 233 |
# Providing a name in this case wouldn't be very sensible. |
| 234 |
# Alternatively make use of with_filetree to parse recursively with filter. |
| 235 |
- name: Parsing a directory of Job templates |
| 236 |
theforeman.foreman.job_template: |
| 237 |
username: "admin" |
| 238 |
password: "changeme" |
| 239 |
server_url: "https://foreman.example.com" |
| 240 |
file_name: "{{ item }}" |
| 241 |
state: present |
| 242 |
locations: |
| 243 |
- SKARO |
| 244 |
organizations: |
| 245 |
- DALEK INC |
| 246 |
with_fileglob: |
| 247 |
- "./arsenal_templates/*.erb" |
| 248 |
|
| 249 |
# If the templates are stored locally and the ansible module is executed on a remote host |
| 250 |
- name: Ensure latest version of all your Job Templates |
| 251 |
theforeman.foreman.job_template: |
| 252 |
server_url: "https://foreman.example.com" |
| 253 |
username: "admin" |
| 254 |
password: "changeme" |
| 255 |
state: present |
| 256 |
template: '{{ lookup("file", item.src) }}' |
| 257 |
name: '{{ item.path }}' |
| 258 |
with_filetree: '/path/to/job/templates' |
| 259 |
when: item.state == 'file' |
| 260 |
|
| 261 |
|
| 262 |
# with name set to "*" bulk actions can be performed |
| 263 |
- name: "Delete *ALL* Job Templates" |
| 264 |
theforeman.foreman.job_template: |
| 265 |
username: "admin" |
| 266 |
password: "changeme" |
| 267 |
server_url: "https://foreman.example.com" |
| 268 |
name: "*" |
| 269 |
state: absent |
| 270 |
|
| 271 |
- name: "Assign all Job Templates to the same organization(s)" |
| 272 |
theforeman.foreman.job_template: |
| 273 |
username: "admin" |
| 274 |
password: "changeme" |
| 275 |
server_url: "https://foreman.example.com" |
| 276 |
name: "*" |
| 277 |
state: present |
| 278 |
organizations: |
| 279 |
- DALEK INC |
| 280 |
- sky.net |
| 281 |
- Doc Brown's garage |
| 282 |
''' |
| 283 |
|
| 284 |
RETURN = ''' |
| 285 |
entity: |
| 286 |
description: Final state of the affected entities grouped by their type. |
| 287 |
returned: success |
| 288 |
type: dict |
| 289 |
contains: |
| 290 |
job_templates: |
| 291 |
description: List of job templates. |
| 292 |
type: list |
| 293 |
elements: dict |
| 294 |
template_inputs: |
| 295 |
description: List of template inputs associated with the job template. |
| 296 |
type: list |
| 297 |
elements: dict |
| 298 |
''' |
| 299 |
|
| 300 |
import os |
| 301 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ( |
| 302 |
ForemanTaxonomicEntityAnsibleModule, |
| 303 |
parse_template, |
| 304 |
parse_template_from_file, |
| 305 |
) |
| 306 |
|
| 307 |
|
| 308 |
template_defaults = { |
| 309 |
'provider_type': 'SSH', |
| 310 |
'job_category': 'unknown', |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
template_input_foreman_spec = { |
| 315 |
'id': dict(invisible=True), |
| 316 |
'name': dict(required=True), |
| 317 |
'description': dict(), |
| 318 |
'required': dict(type='bool'), |
| 319 |
'advanced': dict(type='bool'), |
| 320 |
'input_type': dict(required=True, choices=[ |
| 321 |
'user', |
| 322 |
'fact', |
| 323 |
'variable', |
| 324 |
'puppet_parameter', |
| 325 |
]), |
| 326 |
'fact_name': dict(), |
| 327 |
'variable_name': dict(), |
| 328 |
'puppet_class_name': dict(), |
| 329 |
'puppet_parameter_name': dict(), |
| 330 |
'options': dict(type='list', elements='raw'), |
| 331 |
'value_type': dict(choices=[ |
| 332 |
'plain', |
| 333 |
'search', |
| 334 |
'date', |
| 335 |
'resource', |
| 336 |
]), |
| 337 |
'resource_type': dict(), |
| 338 |
'hidden_value': dict(type='bool'), |
| 339 |
'default': dict(), |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
class ForemanJobTemplateModule(ForemanTaxonomicEntityAnsibleModule): |
| 344 |
pass |
| 345 |
|
| 346 |
|
| 347 |
def main(): |
| 348 |
module = ForemanJobTemplateModule( |
| 349 |
foreman_spec=dict( |
| 350 |
description_format=dict(), |
| 351 |
job_category=dict(), |
| 352 |
locked=dict(type='bool', default=False), |
| 353 |
name=dict(), |
| 354 |
provider_type=dict(), |
| 355 |
snippet=dict(type='bool'), |
| 356 |
template=dict(), |
| 357 |
template_inputs=dict( |
| 358 |
type='nested_list', |
| 359 |
foreman_spec=template_input_foreman_spec, |
| 360 |
required_if=[ |
| 361 |
['input_type', 'fact', ('fact_name',)], |
| 362 |
['input_type', 'variable', ('variable_name',)], |
| 363 |
['input_type', 'puppet_parameter', ('puppet_class_name', 'puppet_parameter_name')], |
| 364 |
], |
| 365 |
), |
| 366 |
), |
| 367 |
argument_spec=dict( |
| 368 |
audit_comment=dict(), |
| 369 |
file_name=dict(type='path'), |
| 370 |
state=dict(default='present', choices=['absent', 'present_with_defaults', 'present']), |
| 371 |
), |
| 372 |
mutually_exclusive=[ |
| 373 |
['file_name', 'template'], |
| 374 |
], |
| 375 |
required_one_of=[ |
| 376 |
['name', 'file_name', 'template'], |
| 377 |
], |
| 378 |
) |
| 379 |
|
| 380 |
|
| 381 |
if module.foreman_params.get('name') == '*': |
| 382 |
if module.foreman_params.get('file_name') or module.foreman_params.get('template'): |
| 383 |
module.fail_json( |
| 384 |
msg="Neither file_name nor template allowed if 'name: *'!") |
| 385 |
|
| 386 |
entity = None |
| 387 |
file_name = module.foreman_params.pop('file_name', None) |
| 388 |
|
| 389 |
if file_name or 'template' in module.foreman_params: |
| 390 |
if file_name: |
| 391 |
parsed_dict = parse_template_from_file(file_name, module) |
| 392 |
else: |
| 393 |
parsed_dict = parse_template(module.foreman_params['template'], module) |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
if parsed_dict.get('name') == '*': |
| 399 |
module.fail_json(msg="Cannot use '*' as a job template name!") |
| 400 |
|
| 401 |
parsed_dict.update(module.foreman_params) |
| 402 |
|
| 403 |
module.foreman_params = template_defaults.copy() |
| 404 |
module.foreman_params.update(parsed_dict) |
| 405 |
|
| 406 |
|
| 407 |
if 'name' not in module.foreman_params: |
| 408 |
if file_name: |
| 409 |
module.foreman_params['name'] = os.path.splitext( |
| 410 |
os.path.basename(file_name))[0] |
| 411 |
else: |
| 412 |
module.fail_json( |
| 413 |
msg='No name specified and no filename to infer it.') |
| 414 |
|
| 415 |
affects_multiple = module.foreman_params['name'] == '*' |
| 416 |
|
| 417 |
if affects_multiple: |
| 418 |
if module.state == 'present_with_defaults': |
| 419 |
module.fail_json(msg="'state: present_with_defaults' and 'name: *' cannot be used together") |
| 420 |
if module.desired_absent: |
| 421 |
further_params = set(module.foreman_params.keys()) - {'name', 'entity'} |
| 422 |
if further_params: |
| 423 |
module.fail_json(msg='When deleting all job templates, there is no need to specify further parameters: %s ' % further_params) |
| 424 |
|
| 425 |
with module.api_connection(): |
| 426 |
if 'audit_comment' in module.foreman_params: |
| 427 |
extra_params = {'audit_comment': module.foreman_params['audit_comment']} |
| 428 |
else: |
| 429 |
extra_params = {} |
| 430 |
|
| 431 |
if affects_multiple: |
| 432 |
module.set_entity('entity', None) |
| 433 |
entities = module.list_resource('job_templates') |
| 434 |
if not entities: |
| 435 |
|
| 436 |
module.exit_json() |
| 437 |
if not module.desired_absent: |
| 438 |
entities = [module.show_resource('job_templates', entity['id']) for entity in entities] |
| 439 |
module.auto_lookup_entities() |
| 440 |
module.foreman_params.pop('name') |
| 441 |
for entity in entities: |
| 442 |
module.ensure_entity('job_templates', module.foreman_params, entity, params=extra_params) |
| 443 |
else: |
| 444 |
|
| 445 |
module.foreman_params['entity'] = module.foreman_params['name'] |
| 446 |
entity = module.lookup_entity('entity') |
| 447 |
|
| 448 |
template_inputs = module.foreman_params.get('template_inputs') |
| 449 |
|
| 450 |
job_template = module.run(params=extra_params) |
| 451 |
|
| 452 |
update_dependent_entities = (module.state == 'present' or (module.state == 'present_with_defaults' and module.changed)) |
| 453 |
if update_dependent_entities and template_inputs is not None: |
| 454 |
scope = {'template_id': job_template['id']} |
| 455 |
|
| 456 |
|
| 457 |
current_template_input_list = module.list_resource('template_inputs', params=scope) if entity else [] |
| 458 |
current_template_inputs = {item['name']: item for item in current_template_input_list} |
| 459 |
for template_input_dict in template_inputs: |
| 460 |
template_input_entity = current_template_inputs.pop(template_input_dict['name'], None) |
| 461 |
|
| 462 |
module.ensure_entity( |
| 463 |
'template_inputs', template_input_dict, template_input_entity, |
| 464 |
params=scope, foreman_spec=template_input_foreman_spec, |
| 465 |
) |
| 466 |
|
| 467 |
|
| 468 |
for template_input_entity in current_template_inputs.values(): |
| 469 |
module.ensure_entity( |
| 470 |
'template_inputs', None, template_input_entity, state="absent", |
| 471 |
params=scope, foreman_spec=template_input_foreman_spec, |
| 472 |
) |
| 473 |
|
| 474 |
|
| 475 |
if __name__ == '__main__': |
| 476 |
main() |