| 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: installation_medium |
| 25 |
version_added: 1.0.0 |
| 26 |
short_description: Manage Installation Media |
| 27 |
description: |
| 28 |
- Create, update, and delete Installation Media |
| 29 |
author: |
| 30 |
- "Manuel Bonk(@manuelbonk) ATIX AG" |
| 31 |
options: |
| 32 |
name: |
| 33 |
description: |
| 34 |
- The full installation medium name. |
| 35 |
- The special name "*" (only possible as parameter) is used to perform bulk actions (modify, delete) on all existing partition tables. |
| 36 |
required: true |
| 37 |
type: str |
| 38 |
updated_name: |
| 39 |
description: New full installation medium name. When this parameter is set, the module will not be idempotent. |
| 40 |
type: str |
| 41 |
os_family: |
| 42 |
description: |
| 43 |
- The OS family the template shall be assigned with. |
| 44 |
- If no os_family is set but a operatingsystem, the value will be derived from it. |
| 45 |
path: |
| 46 |
description: Path to the installation medium |
| 47 |
type: str |
| 48 |
extends_documentation_fragment: |
| 49 |
- theforeman.foreman.foreman |
| 50 |
- theforeman.foreman.foreman.entity_state_with_defaults |
| 51 |
- theforeman.foreman.foreman.taxonomy |
| 52 |
- theforeman.foreman.foreman.os_family |
| 53 |
- theforeman.foreman.foreman.operatingsystems |
| 54 |
''' |
| 55 |
|
| 56 |
EXAMPLES = ''' |
| 57 |
- name: create new debian medium |
| 58 |
theforeman.foreman.installation_medium: |
| 59 |
name: "wheezy" |
| 60 |
locations: |
| 61 |
- "Munich" |
| 62 |
organizations: |
| 63 |
- "ACME" |
| 64 |
operatingsystems: |
| 65 |
- "Debian" |
| 66 |
path: "http://debian.org/mirror/" |
| 67 |
server_url: "https://foreman.example.com" |
| 68 |
username: "admin" |
| 69 |
password: "changeme" |
| 70 |
state: present |
| 71 |
''' |
| 72 |
|
| 73 |
RETURN = ''' |
| 74 |
entity: |
| 75 |
description: Final state of the affected entities grouped by their type. |
| 76 |
returned: success |
| 77 |
type: dict |
| 78 |
contains: |
| 79 |
media: |
| 80 |
description: List of installation media. |
| 81 |
type: list |
| 82 |
elements: dict |
| 83 |
''' |
| 84 |
|
| 85 |
from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanTaxonomicEntityAnsibleModule, OS_LIST |
| 86 |
|
| 87 |
|
| 88 |
class ForemanInstallationMediumModule(ForemanTaxonomicEntityAnsibleModule): |
| 89 |
pass |
| 90 |
|
| 91 |
|
| 92 |
def main(): |
| 93 |
module = ForemanInstallationMediumModule( |
| 94 |
argument_spec=dict( |
| 95 |
updated_name=dict(), |
| 96 |
state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']), |
| 97 |
), |
| 98 |
foreman_spec=dict( |
| 99 |
name=dict(required=True), |
| 100 |
operatingsystems=dict(type='entity_list'), |
| 101 |
os_family=dict(choices=OS_LIST), |
| 102 |
path=dict(), |
| 103 |
), |
| 104 |
entity_opts=dict( |
| 105 |
resource_type='media', |
| 106 |
), |
| 107 |
entity_name='medium', |
| 108 |
) |
| 109 |
|
| 110 |
module_params = module.foreman_params |
| 111 |
entity = None |
| 112 |
|
| 113 |
name = module_params['name'] |
| 114 |
|
| 115 |
affects_multiple = name == '*' |
| 116 |
|
| 117 |
if affects_multiple: |
| 118 |
if module.state == 'present_with_defaults': |
| 119 |
module.fail_json(msg="'state: present_with_defaults' and 'name: *' cannot be used together") |
| 120 |
if module.params['updated_name']: |
| 121 |
module.fail_json(msg="updated_name not allowed if 'name: *'!") |
| 122 |
if module.desired_absent: |
| 123 |
further_params = set(module_params.keys()) - {'name', 'entity'} |
| 124 |
if further_params: |
| 125 |
module.fail_json(msg='When deleting all installation media, there is no need to specify further parameters: %s ' % further_params) |
| 126 |
|
| 127 |
with module.api_connection(): |
| 128 |
if affects_multiple: |
| 129 |
module.set_entity('entity', None) |
| 130 |
entities = module.list_resource('media') |
| 131 |
if not entities: |
| 132 |
|
| 133 |
module.exit_json() |
| 134 |
if not module.desired_absent: |
| 135 |
entities = [module.show_resource('media', entity['id']) for entity in entities] |
| 136 |
module.auto_lookup_entities() |
| 137 |
module_params.pop('name') |
| 138 |
for entity in entities: |
| 139 |
module.ensure_entity('media', module_params, entity) |
| 140 |
else: |
| 141 |
entity = module.lookup_entity('entity') |
| 142 |
if not module.desired_absent and 'operatingsystems' in module_params: |
| 143 |
operatingsystems = module.lookup_entity('operatingsystems') |
| 144 |
if len(operatingsystems) == 1 and 'os_family' not in module_params and entity is None: |
| 145 |
module_params['os_family'] = module.show_resource('operatingsystems', operatingsystems[0]['id'])['family'] |
| 146 |
|
| 147 |
module.run() |
| 148 |
|
| 149 |
|
| 150 |
if __name__ == '__main__': |
| 151 |
main() |