| 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: os_default_template |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Default Template Associations To Operating Systems |
| 27 |
description: |
| 28 |
- Manage OSDefaultTemplate Entities |
| 29 |
author: |
| 30 |
- "Matthias M Dellweg (@mdellweg) ATIX AG" |
| 31 |
options: |
| 32 |
operatingsystem: |
| 33 |
required: true |
| 34 |
template_kind: |
| 35 |
description: |
| 36 |
- name of the template kind |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
choices: |
| 40 |
- Bootdisk |
| 41 |
- cloud-init |
| 42 |
- finish |
| 43 |
- host_init_config |
| 44 |
- iPXE |
| 45 |
- job_template |
| 46 |
- kexec |
| 47 |
- POAP |
| 48 |
- provision |
| 49 |
- PXEGrub |
| 50 |
- PXEGrub2 |
| 51 |
- PXELinux |
| 52 |
- registration |
| 53 |
- script |
| 54 |
- user_data |
| 55 |
- ZTP |
| 56 |
provisioning_template: |
| 57 |
description: |
| 58 |
- name of provisioning template |
| 59 |
required: false |
| 60 |
type: str |
| 61 |
extends_documentation_fragment: |
| 62 |
- theforeman.foreman.foreman |
| 63 |
- theforeman.foreman.foreman.entity_state_with_defaults |
| 64 |
- theforeman.foreman.foreman.operatingsystem |
| 65 |
''' |
| 66 |
|
| 67 |
EXAMPLES = ''' |
| 68 |
- name: "Create an Association" |
| 69 |
theforeman.foreman.os_default_template: |
| 70 |
username: "admin" |
| 71 |
password: "changeme" |
| 72 |
server_url: "https://foreman.example.com" |
| 73 |
operatingsystem: "CoolOS" |
| 74 |
template_kind: "finish" |
| 75 |
provisioning_template: "CoolOS finish" |
| 76 |
state: present |
| 77 |
|
| 78 |
- name: "Delete an Association" |
| 79 |
theforeman.foreman.os_default_template: |
| 80 |
username: "admin" |
| 81 |
password: "changeme" |
| 82 |
server_url: "https://foreman.example.com" |
| 83 |
operatingsystem: "CoolOS" |
| 84 |
template_kind: "finish" |
| 85 |
state: absent |
| 86 |
''' |
| 87 |
|
| 88 |
RETURN = ''' |
| 89 |
entity: |
| 90 |
description: Final state of the affected entities grouped by their type. |
| 91 |
returned: success |
| 92 |
type: dict |
| 93 |
contains: |
| 94 |
os_default_templates: |
| 95 |
description: List of operatingsystem default templates. |
| 96 |
type: list |
| 97 |
elements: dict |
| 98 |
''' |
| 99 |
|
| 100 |
|
| 101 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanEntityAnsibleModule, TEMPLATE_KIND_LIST |
| 102 |
|
| 103 |
|
| 104 |
class ForemanOsDefaultTemplateModule(ForemanEntityAnsibleModule): |
| 105 |
pass |
| 106 |
|
| 107 |
|
| 108 |
def main(): |
| 109 |
module = ForemanOsDefaultTemplateModule( |
| 110 |
argument_spec=dict( |
| 111 |
state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']), |
| 112 |
), |
| 113 |
foreman_spec=dict( |
| 114 |
operatingsystem=dict(required=True, type='entity'), |
| 115 |
template_kind=dict(required=True, choices=TEMPLATE_KIND_LIST, type='entity'), |
| 116 |
provisioning_template=dict(type='entity', thin=False), |
| 117 |
), |
| 118 |
required_if=[ |
| 119 |
['state', 'present', ['provisioning_template']], |
| 120 |
['state', 'present_with_defaults', ['provisioning_template']], |
| 121 |
], |
| 122 |
entity_opts={'scope': ['operatingsystem']}, |
| 123 |
) |
| 124 |
|
| 125 |
if 'provisioning_template' in module.foreman_params and module.desired_absent: |
| 126 |
module.fail_json(msg='Provisioning template must not be specified for deletion.') |
| 127 |
|
| 128 |
with module.api_connection(): |
| 129 |
template_kind_id = module.lookup_entity('template_kind')['id'] |
| 130 |
if not module.desired_absent: |
| 131 |
if module.lookup_entity('provisioning_template')['template_kind_id'] != template_kind_id: |
| 132 |
module.fail_json(msg='Provisioning template kind mismatching.') |
| 133 |
|
| 134 |
scope = module.scope_for('operatingsystem') |
| 135 |
|
| 136 |
|
| 137 |
entities = module.list_resource('os_default_templates', params=scope) |
| 138 |
entity = next((item for item in entities if item['template_kind_id'] == template_kind_id), None) |
| 139 |
module.set_entity('entity', entity) |
| 140 |
|
| 141 |
module.run() |
| 142 |
|
| 143 |
|
| 144 |
if __name__ == '__main__': |
| 145 |
main() |