| 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: provisioning_template |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Provisioning Templates |
| 27 |
description: |
| 28 |
- Manage Provisioning Templates |
| 29 |
author: |
| 30 |
- "Bernhard Hopfenmueller (@Fobhep) ATIX AG" |
| 31 |
- "Matthias Dellweg (@mdellweg) ATIX AG" |
| 32 |
options: |
| 33 |
audit_comment: |
| 34 |
description: |
| 35 |
- Content of the audit comment field |
| 36 |
required: false |
| 37 |
type: str |
| 38 |
kind: |
| 39 |
description: |
| 40 |
- The provisioning template kind |
| 41 |
required: false |
| 42 |
choices: |
| 43 |
- Bootdisk |
| 44 |
- cloud-init |
| 45 |
- finish |
| 46 |
- host_init_config |
| 47 |
- iPXE |
| 48 |
- job_template |
| 49 |
- kexec |
| 50 |
- POAP |
| 51 |
- provision |
| 52 |
- PXEGrub |
| 53 |
- PXEGrub2 |
| 54 |
- PXELinux |
| 55 |
- registration |
| 56 |
- script |
| 57 |
- snippet |
| 58 |
- user_data |
| 59 |
- ZTP |
| 60 |
type: str |
| 61 |
template: |
| 62 |
description: |
| 63 |
- The content of the provisioning template. |
| 64 |
- Either this or I(file_name) is required as a source for the Provisioning Template "content". |
| 65 |
required: false |
| 66 |
type: str |
| 67 |
file_name: |
| 68 |
description: |
| 69 |
- The path of a template file, that shall be imported. |
| 70 |
- Either this or I(template) is required as a source for the Provisioning Template "content". |
| 71 |
required: false |
| 72 |
type: path |
| 73 |
locked: |
| 74 |
description: |
| 75 |
- Determines whether the template shall be locked |
| 76 |
required: false |
| 77 |
type: bool |
| 78 |
name: |
| 79 |
description: |
| 80 |
- The name of the Provisioning Template. |
| 81 |
- If omited, will be determined from the C(name) header of the template or the filename (in that order). |
| 82 |
- The special value "*" can be used to perform bulk actions (modify, delete) on all existing templates. |
| 83 |
required: false |
| 84 |
type: str |
| 85 |
updated_name: |
| 86 |
description: New provisioning template name. When this parameter is set, the module will not be idempotent. |
| 87 |
type: str |
| 88 |
extends_documentation_fragment: |
| 89 |
- theforeman.foreman.foreman |
| 90 |
- theforeman.foreman.foreman.entity_state_with_defaults |
| 91 |
- theforeman.foreman.foreman.taxonomy |
| 92 |
- theforeman.foreman.foreman.operatingsystems |
| 93 |
''' |
| 94 |
|
| 95 |
EXAMPLES = ''' |
| 96 |
|
| 97 |
# Keep in mind, that in this case, the inline parameters will be overwritten |
| 98 |
- name: "Create a Provisioning Template inline" |
| 99 |
theforeman.foreman.provisioning_template: |
| 100 |
username: "admin" |
| 101 |
password: "changeme" |
| 102 |
server_url: "https://foreman.example.com" |
| 103 |
name: A New Finish Template |
| 104 |
kind: finish |
| 105 |
state: present |
| 106 |
template: | |
| 107 |
<%# |
| 108 |
name: Finish timetravel |
| 109 |
kind: finish |
| 110 |
%> |
| 111 |
cd / |
| 112 |
rm -rf * |
| 113 |
locations: |
| 114 |
- Gallifrey |
| 115 |
organizations: |
| 116 |
- TARDIS INC |
| 117 |
|
| 118 |
- name: "Create a Provisioning Template from a file" |
| 119 |
theforeman.foreman.provisioning_template: |
| 120 |
username: "admin" |
| 121 |
password: "changeme" |
| 122 |
server_url: "https://foreman.example.com" |
| 123 |
file_name: timeywimey_template.erb |
| 124 |
state: present |
| 125 |
locations: |
| 126 |
- Gallifrey |
| 127 |
organizations: |
| 128 |
- TARDIS INC |
| 129 |
|
| 130 |
# Due to the module logic, deleting requires a template dummy, |
| 131 |
# either inline or from a file. |
| 132 |
- name: "Delete a Provisioning Template" |
| 133 |
theforeman.foreman.provisioning_template: |
| 134 |
username: "admin" |
| 135 |
password: "changeme" |
| 136 |
server_url: "https://foreman.example.com" |
| 137 |
name: timeywimey_template |
| 138 |
template: | |
| 139 |
<%# |
| 140 |
dummy: |
| 141 |
%> |
| 142 |
state: absent |
| 143 |
|
| 144 |
- name: "Create a Provisioning Template from a file and modify with parameter" |
| 145 |
theforeman.foreman.provisioning_template: |
| 146 |
username: "admin" |
| 147 |
password: "changeme" |
| 148 |
server_url: "https://foreman.example.com" |
| 149 |
file_name: timeywimey_template.erb |
| 150 |
name: Wibbly Wobbly Template |
| 151 |
state: present |
| 152 |
locations: |
| 153 |
- Gallifrey |
| 154 |
organizations: |
| 155 |
- TARDIS INC |
| 156 |
|
| 157 |
# Providing a name in this case wouldn't be very sensible. |
| 158 |
# Alternatively make use of with_filetree to parse recursively with filter. |
| 159 |
- name: "Parsing a directory of provisioning templates" |
| 160 |
theforeman.foreman.provisioning_template: |
| 161 |
username: "admin" |
| 162 |
password: "changeme" |
| 163 |
server_url: "https://foreman.example.com" |
| 164 |
file_name: "{{ item }}" |
| 165 |
state: present |
| 166 |
locations: |
| 167 |
- SKARO |
| 168 |
organizations: |
| 169 |
- DALEK INC |
| 170 |
with_fileglob: |
| 171 |
- "./arsenal_templates/*.erb" |
| 172 |
|
| 173 |
# If the templates are stored locally and the ansible module is executed on a remote host |
| 174 |
- name: Ensure latest version of all Provisioning Community Templates |
| 175 |
theforeman.foreman.provisioning_template: |
| 176 |
server_url: "https://foreman.example.com" |
| 177 |
username: "admin" |
| 178 |
password: "changeme" |
| 179 |
state: present |
| 180 |
template: '{{ lookup("file", item.src) }}' |
| 181 |
with_filetree: '/path/to/provisioning/templates' |
| 182 |
when: item.state == 'file' |
| 183 |
|
| 184 |
|
| 185 |
# with name set to "*" bulk actions can be performed |
| 186 |
- name: "Delete *ALL* provisioning templates" |
| 187 |
theforeman.foreman.provisioning_template: |
| 188 |
username: "admin" |
| 189 |
password: "changeme" |
| 190 |
server_url: "https://foreman.example.com" |
| 191 |
name: "*" |
| 192 |
state: absent |
| 193 |
|
| 194 |
- name: "Assign all provisioning templates to the same organization(s)" |
| 195 |
theforeman.foreman.provisioning_template: |
| 196 |
username: "admin" |
| 197 |
password: "changeme" |
| 198 |
server_url: "https://foreman.example.com" |
| 199 |
name: "*" |
| 200 |
state: present |
| 201 |
organizations: |
| 202 |
- DALEK INC |
| 203 |
- sky.net |
| 204 |
- Doc Brown's garage |
| 205 |
''' |
| 206 |
|
| 207 |
RETURN = ''' |
| 208 |
entity: |
| 209 |
description: Final state of the affected entities grouped by their type. |
| 210 |
returned: success |
| 211 |
type: dict |
| 212 |
contains: |
| 213 |
provisioning_templates: |
| 214 |
description: List of provisioning templates. |
| 215 |
type: list |
| 216 |
elements: dict |
| 217 |
''' |
| 218 |
|
| 219 |
|
| 220 |
import os |
| 221 |
|
| 222 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ( |
| 223 |
ForemanTaxonomicEntityAnsibleModule, |
| 224 |
parse_template, |
| 225 |
parse_template_from_file, |
| 226 |
TEMPLATE_KIND_LIST, |
| 227 |
) |
| 228 |
|
| 229 |
|
| 230 |
def find_template_kind(module, module_params): |
| 231 |
if 'kind' not in module_params: |
| 232 |
return module_params |
| 233 |
|
| 234 |
module_params['snippet'] = (module_params['kind'] == 'snippet') |
| 235 |
if module_params['snippet']: |
| 236 |
module_params.pop('kind') |
| 237 |
else: |
| 238 |
module_params['kind'] = module.find_resource_by_name('template_kinds', module_params['kind'], thin=True) |
| 239 |
return module_params |
| 240 |
|
| 241 |
|
| 242 |
class ForemanProvisioningTemplateModule(ForemanTaxonomicEntityAnsibleModule): |
| 243 |
pass |
| 244 |
|
| 245 |
|
| 246 |
def main(): |
| 247 |
module = ForemanProvisioningTemplateModule( |
| 248 |
argument_spec=dict( |
| 249 |
audit_comment=dict(), |
| 250 |
file_name=dict(type='path'), |
| 251 |
state=dict(default='present', choices=['absent', 'present_with_defaults', 'present']), |
| 252 |
updated_name=dict(), |
| 253 |
), |
| 254 |
foreman_spec=dict( |
| 255 |
kind=dict(choices=TEMPLATE_KIND_LIST + ['snippet'], type='entity', flat_name='template_kind_id', resolve=False), |
| 256 |
template=dict(), |
| 257 |
locked=dict(type='bool'), |
| 258 |
name=dict(), |
| 259 |
operatingsystems=dict(type='entity_list'), |
| 260 |
snippet=dict(invisible=True), |
| 261 |
), |
| 262 |
mutually_exclusive=[ |
| 263 |
['file_name', 'template'], |
| 264 |
], |
| 265 |
required_one_of=[ |
| 266 |
['name', 'file_name', 'template'], |
| 267 |
], |
| 268 |
) |
| 269 |
|
| 270 |
|
| 271 |
if module.foreman_params.get('name') == '*': |
| 272 |
if module.foreman_params.get('file_name') or module.foreman_params.get('template') or module.foreman_params.get('updated_name'): |
| 273 |
module.fail_json( |
| 274 |
msg="Neither file_name nor template nor updated_name allowed if 'name: *'!") |
| 275 |
|
| 276 |
entity = None |
| 277 |
file_name = module.foreman_params.pop('file_name', None) |
| 278 |
|
| 279 |
if file_name or 'template' in module.foreman_params: |
| 280 |
if file_name: |
| 281 |
parsed_dict = parse_template_from_file(file_name, module) |
| 282 |
else: |
| 283 |
parsed_dict = parse_template(module.foreman_params['template'], module) |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
if parsed_dict.get('name') == '*': |
| 289 |
module.fail_json(msg="Cannot use '*' as a template name!") |
| 290 |
|
| 291 |
parsed_dict.update(module.foreman_params) |
| 292 |
module.foreman_params = parsed_dict |
| 293 |
|
| 294 |
|
| 295 |
if 'name' not in module.foreman_params: |
| 296 |
if file_name: |
| 297 |
module.foreman_params['name'] = os.path.splitext( |
| 298 |
os.path.basename(file_name))[0] |
| 299 |
else: |
| 300 |
module.fail_json( |
| 301 |
msg='No name specified and no filename to infer it.') |
| 302 |
|
| 303 |
affects_multiple = module.foreman_params['name'] == '*' |
| 304 |
|
| 305 |
if affects_multiple: |
| 306 |
if module.foreman_params.get('updated_name'): |
| 307 |
module.fail_json(msg="updated_name not allowed if 'name: *'!") |
| 308 |
if module.state == 'present_with_defaults': |
| 309 |
module.fail_json(msg="'state: present_with_defaults' and 'name: *' cannot be used together") |
| 310 |
if module.desired_absent: |
| 311 |
further_params = set(module.foreman_params.keys()) - {'name', 'entity'} |
| 312 |
if further_params: |
| 313 |
module.fail_json(msg='When deleting all templates, there is no need to specify further parameters: %s ' % further_params) |
| 314 |
|
| 315 |
with module.api_connection(): |
| 316 |
if 'audit_comment' in module.foreman_params: |
| 317 |
extra_params = {'audit_comment': module.foreman_params['audit_comment']} |
| 318 |
else: |
| 319 |
extra_params = {} |
| 320 |
|
| 321 |
if affects_multiple: |
| 322 |
module.set_entity('entity', None) |
| 323 |
entities = module.list_resource('provisioning_templates') |
| 324 |
if not entities: |
| 325 |
|
| 326 |
module.exit_json() |
| 327 |
if not module.desired_absent: |
| 328 |
entities = [module.show_resource('provisioning_templates', entity['id']) for entity in entities] |
| 329 |
module.auto_lookup_entities() |
| 330 |
module.foreman_params.pop('name') |
| 331 |
for entity in entities: |
| 332 |
module.ensure_entity('provisioning_templates', module.foreman_params, entity, params=extra_params) |
| 333 |
else: |
| 334 |
|
| 335 |
module.foreman_params['entity'] = module.foreman_params['name'] |
| 336 |
|
| 337 |
module.foreman_params = find_template_kind(module, module.foreman_params) |
| 338 |
|
| 339 |
module.run(params=extra_params) |
| 340 |
|
| 341 |
|
| 342 |
if __name__ == '__main__': |
| 343 |
main() |