def main(): module = ForemanEntityApypieAnsibleModule( entity_spec=dict( name=dict(required=True), locations=dict(type='entity_list', flat_name='location_ids'), organizations=dict(type='entity_list', flat_name='organization_ids'), operatingsystems=dict(type='entity_list', flat_name='operatingsystem_ids'), os_family=dict(), path=dict(), ), ) entity_dict = module.clean_params() module.connect() name = entity_dict['name'] affects_multiple = name == '*' # sanitize user input, filter unuseful configuration combinations with 'name: *' if affects_multiple: if module.state == 'present_with_defaults': module.fail_json(msg="'state: present_with_defaults' and 'name: *' cannot be used together") if module.desired_absent: if list(entity_dict.keys()) != ['name']: module.fail_json(msg='When deleting all installation media, there is no need to specify further parameters %s ' % entity_dict.keys()) if affects_multiple: entities = module.list_resource('media') if not module.desired_absent: # not 'thin' entities = [module.show_resource('media', entity['id']) for entity in entities] if not entities: # Nothing to do shortcut to exit module.exit_json(changed=False) else: entity = module.find_resource_by_name('media', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'operatingsystems' in entity_dict: entity_dict['operatingsystems'] = module.find_operatingsystems(entity_dict['operatingsystems'], thin=True) if not affects_multiple and len(entity_dict['operatingsystems']) == 1 and 'os_family' not in entity_dict and entity is None: entity_dict['os_family'] = module.show_resource('operatingsystems', entity_dict['operatingsystems'][0]['id'])['family'] if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title('locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name('organizations', entity_dict['organizations'], thin=True) changed = False if not affects_multiple: changed = module.ensure_entity_state('media', entity_dict, entity) else: entity_dict.pop('name') for entity in entities: changed |= module.ensure_entity_state('media', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityApypieAnsibleModule( argument_spec=dict( operatingsystem=dict(required=True), state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']), ), entity_spec=dict( template_kind=dict(required=True, type='entity', flat_name='template_kind_id'), provisioning_template=dict(type='entity', flat_name='provisioning_template_id'), ), required_if=( ['state', 'present', ['provisioning_template']], ['state', 'present_with_defaults', ['provisioning_template']], ), ) entity_dict = module.clean_params() module.connect() entity_dict['operatingsystem'] = module.find_operatingsystem( entity_dict['operatingsystem'], thin=True) entity_dict['template_kind'] = module.find_resource_by_name( 'template_kinds', entity_dict['template_kind'], thin=True) scope = {'operatingsystem_id': entity_dict['operatingsystem']['id']} search = 'template_kind_id={}'.format(entity_dict['template_kind']['id']) entity = module.find_resource('os_default_templates', search, params=scope, failsafe=True) # Find Provisioning Template if 'provisioning_template' in entity_dict: if module.desired_absent: module.fail_json( msg='Provisioning template must not be specified for deletion.' ) entity_dict['provisioning_template'] = module.find_resource_by_name( 'provisioning_templates', entity_dict['provisioning_template']) if entity_dict['provisioning_template'][ 'template_kind_id'] != entity_dict['template_kind']['id']: module.fail_json(msg='Provisioning template kind mismatching.') changed = module.ensure_entity_state('os_default_templates', entity_dict, entity, params=scope) module.exit_json(changed=changed)
def main(): module = ForemanEntityApypieAnsibleModule(argument_spec=dict( name=dict(required=True), parent=dict(), organizations=dict(type='list'), ), ) entity_dict = module.clean_params() module.connect() # Get short name and parent from provided name name, parent = split_fqn(entity_dict['name']) entity_dict['name'] = name if 'parent' in entity_dict: if parent: module.fail_json( msg= "Please specify the parent either separately, or as part of the title." ) parent = entity_dict['parent'] if parent: search_string = 'title="{}"'.format(parent) entity_dict['parent'] = module.find_resource( 'locations', search=search_string, thin=True, failsafe=module.desired_absent) if module.desired_absent and entity_dict['parent'] is None: # Parent location does not exist so just exit here module.exit_json(changed=False) if not module.desired_absent: if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) entity = module.find_resource('locations', search='title="{}"'.format( build_fqn(name, parent)), failsafe=True) changed = module.ensure_resource_state('locations', entity_dict, entity, name_map=name_map) module.exit_json(changed=changed)
def main(): module = ForemanEntityApypieAnsibleModule(entity_spec=dict( name=dict(required=True), puppetclasses=dict(type='entity_list', flat_name='puppetclass_ids'), ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('config_groups', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'puppetclasses' in entity_dict: # puppet classes API return puppet classes grouped by puppet module name puppet_classes = [] for puppet_class in entity_dict['puppetclasses']: search = 'name="{}"'.format(puppet_class) results = module.list_resource('puppetclasses', search) # verify that only one puppet module is returned with only one puppet class inside # as provided search results have to be like "results": { "ntp": [{"id": 1, "name": "ntp" ...}]} # and get the puppet class id if len(results) == 1 and len(list(results.values())[0]) == 1: puppet_classes.append( {'id': list(results.values())[0][0]['id']}) else: module.fail_json(msg='No data found for name="%s"' % search) entity_dict['puppetclasses'] = puppet_classes changed = module.ensure_entity_state('config_groups', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityApypieAnsibleModule( argument_spec=dict( file_name=dict(type='path'), state=dict(default='present', choices=['absent', 'present_with_defaults', 'present']), ), entity_spec=dict( audit_comment=dict(), kind=dict(choices=[ 'finish', 'iPXE', 'job_template', 'POAP', 'provision', 'ptable', 'PXELinux', 'PXEGrub', 'PXEGrub2', 'script', 'snippet', 'user_data', 'ZTP', ], type='entity', flat_name='template_kind_id'), template=dict(), locations=dict(type='entity_list', flat_name='location_ids'), locked=dict(type='bool'), name=dict(), organizations=dict(type='entity_list', flat_name='organization_ids'), operatingsystems=dict(type='entity_list', flat_name='operatingsystem_ids'), snippet=dict(type='invisible'), ), mutually_exclusive=[ ['file_name', 'template'], ], required_one_of=[ ['name', 'file_name', 'template'], ], ) # We do not want a template text for bulk operations if module.params['name'] == '*': if module.params['file_name'] or module.params['template']: module.fail_json( msg="Neither file_name nor template allowed if 'name: *'!") entity_dict = module.clean_params() file_name = entity_dict.pop('file_name', None) if file_name or 'template' in entity_dict: if file_name: parsed_dict = parse_template_from_file(file_name, module) else: parsed_dict = parse_template(entity_dict['template'], module) # sanitize name from template data # The following condition can actually be hit, when someone is trying to import a # template with the name set to '*'. # Besides not being sensible, this would go horribly wrong in this module. if 'name' in parsed_dict and parsed_dict['name'] == '*': module.fail_json(msg="Cannot use '*' as a template name!") # module params are priorized parsed_dict.update(entity_dict) entity_dict = parsed_dict # make sure, we have a name if 'name' not in entity_dict: if file_name: entity_dict['name'] = os.path.splitext( os.path.basename(file_name))[0] else: module.fail_json( msg='No name specified and no filename to infer it.') affects_multiple = entity_dict['name'] == '*' # sanitize user input, filter unuseful configuration combinations with 'name: *' if affects_multiple: if module.state == 'present_with_defaults': module.fail_json( msg= "'state: present_with_defaults' and 'name: *' cannot be used together" ) if module.desired_absent: if len(entity_dict.keys()) != 1: module.fail_json( msg= "When deleting all templates, there is no need to specify further parameters." ) module.connect() if affects_multiple: entities = module.list_resource('provisioning_templates') if not entities: # Nothing to do; shortcut to exit module.exit_json(changed=False) if not module.desired_absent: # not 'thin' entities = [ module.show_resource('provisioning_templates', entity['id']) for entity in entities ] else: entity = module.find_resource_by_name('provisioning_templates', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title( 'locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) if 'operatingsystems' in entity_dict: entity_dict['operatingsystems'] = module.find_operatingsystems( entity_dict['operatingsystems'], thin=True) if not affects_multiple: entity_dict = find_template_kind(module, entity_dict) changed = False if not affects_multiple: changed = module.ensure_entity_state('provisioning_templates', entity_dict, entity) else: entity_dict.pop('name') for entity in entities: changed |= module.ensure_entity_state('provisioning_templates', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityApypieAnsibleModule( entity_spec=dict( name=dict(required=True), updated_name=dict(), description=dict(), organizations=dict(type='entity_list', flat_name='organization_ids'), locations=dict(type='entity_list', flat_name='location_ids'), provider=dict(choices=['vmware', 'libvirt', 'ovirt']), display_type=dict(type='invisible'), datacenter=dict(type='invisible'), url=dict(type='invisible'), user=dict(type='invisible'), password=dict(type='invisible'), use_v4=dict(type='invisible'), ovirt_quota=dict(type='invisible'), ), argument_spec=dict( provider_params=dict(type='dict', options=dict( url=dict(), display_type=dict(), user=dict(), password=dict(no_log=True), datacenter=dict(), use_v4=dict(type='bool'), ovirt_quota=dict(), )), state=dict(type='str', default='present', choices=['present', 'absent', 'present_with_defaults']), ), required_if=([ 'state', 'present_with_defaults', ['provider', 'provider_params'] ], ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('compute_resources', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'updated_name' in entity_dict: entity_dict['name'] = entity_dict['updated_name'] if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title( 'locations', entity_dict['locations'], thin=True) if 'provider' in entity_dict: entity_dict['provider'], provider_param_keys = get_provider_info( provider=entity_dict['provider']) provider_params = { k: v for k, v in entity_dict.pop('provider_params', dict()).items() if v is not None } for key in provider_param_keys: if key in provider_params: entity_dict[key] = provider_params.pop(key) if provider_params: module.fail_json( msg= "Provider {} does not support the following given parameters: {}" .format(entity_dict['provider'], list(provider_params.keys()))) # Add provider specific params elif entity is None: module.fail_json( msg= 'To create a compute resource a valid provider must be supplied' ) changed = module.ensure_entity_state('compute_resources', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityApypieAnsibleModule( entity_spec=dict( name=dict(), release_name=dict(), description=dict(), family=dict(), major=dict(), minor=dict(), architectures=dict(type='entity_list', flat_name='architecture_ids'), media=dict(type='entity_list', flat_name='medium_ids'), ptables=dict(type='entity_list', flat_name='ptable_ids'), provisioning_templates=dict(type='entity_list', flat_name='provisioning_template_ids'), password_hash=dict(choices=['MD5', 'SHA256', 'SHA512']), parameters=dict(type='nested_list', entity_spec=parameter_entity_spec), ), argument_spec=dict( state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']), ), required_if=[ ['state', 'present', ['name', 'major', 'family']], ['state', 'present_with_defaults', ['name', 'major', 'family']], ], required_one_of=[ ['description', 'name'], ['description', 'major'], ], ) entity_dict = module.clean_params() module.connect() # Try to find the Operating System to work on # name is however not unique, but description is, as well as "<name> <major>[.<minor>]" entity = None # If we have a description, search for it if 'description' in entity_dict and entity_dict['description'] != '': search_string = 'description="{}"'.format(entity_dict['description']) entity = module.find_resource('operatingsystems', search_string, failsafe=True) # If we did not yet find a unique OS, search by name & version # In case of state == absent, those information might be missing, we assume that we did not find an operatingsytem to delete then if entity is None and 'name' in entity_dict and 'major' in entity_dict: search_string = ','.join('{}="{}"'.format(key, entity_dict[key]) for key in ('name', 'major', 'minor') if key in entity_dict) entity = module.find_resource('operatingsystems', search_string, failsafe=True) if not entity and (module.state == 'present' or module.state == 'present_with_defaults'): # we actually attempt to create a new one... for param_name in ['major', 'family', 'password_hash']: if param_name not in entity_dict.keys(): module.fail_json(msg='{} is a required parameter to create a new operating system.'.format(param_name)) if not module.desired_absent: if 'architectures' in entity_dict: entity_dict['architectures'] = module.find_resources_by_name('architectures', entity_dict['architectures'], thin=True) if 'media' in entity_dict: entity_dict['media'] = module.find_resources_by_name('media', entity_dict['media'], thin=True) if 'ptables' in entity_dict: entity_dict['ptables'] = module.find_resources_by_name('ptables', entity_dict['ptables'], thin=True) if 'provisioning_templates' in entity_dict: entity_dict['provisioning_templates'] = module.find_resources_by_name('provisioning_templates', entity_dict['provisioning_templates'], thin=True) parameters = entity_dict.get('parameters') changed, operating_system = module.ensure_entity('operatingsystems', entity_dict, entity) if operating_system: scope = {'operatingsystem_id': operating_system['id']} changed |= module.ensure_scoped_parameters(scope, entity, parameters) module.exit_json(changed=changed)
def main(): module = ForemanEntityApypieAnsibleModule( argument_spec=dict( file_name=dict(type='path'), state=dict(default='present', choices=['absent', 'present_with_defaults', 'present']), ), entity_spec=dict( layout=dict(), locations=dict(type='entity_list', flat_name='location_ids'), locked=dict(type='bool'), name=dict(), organizations=dict(type='entity_list', flat_name='organization_ids'), os_family=dict(choices=[ 'AIX', 'Altlinux', 'Archlinux', 'Debian', 'Freebsd', 'Gentoo', 'Junos', 'Redhat', 'Solaris', 'Suse', 'Windows', ]), ), mutually_exclusive=[ ['file_name', 'layout'], ], required_one_of=[ ['name', 'file_name', 'layout'], ], ) # We do not want a layout text for bulk operations if module.params['name'] == '*': if module.params['file_name'] or module.params['layout']: module.fail_json( msg="Neither file_name nor layout allowed if 'name: *'!") entity_dict = module.clean_params() file_name = entity_dict.pop('file_name', None) if file_name or 'layout' in entity_dict: if file_name: parsed_dict = parse_template_from_file(file_name, module) else: parsed_dict = parse_template(entity_dict['layout'], module) parsed_dict['layout'] = parsed_dict.pop('template') if 'oses' in parsed_dict: parsed_dict['os_family'] = parsed_dict.pop('oses') # sanitize name from template data # The following condition can actually be hit, when someone is trying to import a # template with the name set to '*'. # Besides not being sensible, this would go horribly wrong in this module. if 'name' in parsed_dict and parsed_dict['name'] == '*': module.fail_json(msg="Cannot use '*' as a partition table name!") # module params are priorized parsed_dict.update(entity_dict) entity_dict = parsed_dict # make sure, we have a name if 'name' not in entity_dict: if file_name: entity_dict['name'] = os.path.splitext( os.path.basename(file_name))[0] else: module.fail_json( msg='No name specified and no filename to infer it.') affects_multiple = entity_dict['name'] == '*' # sanitize user input, filter unuseful configuration combinations with 'name: *' if affects_multiple: if module.state == 'present_with_defaults': module.fail_json(msg="'state: present_with_defaults' and 'name: *' cannot be used together") if module.desired_absent: if len(entity_dict.keys()) != 1: module.fail_json(msg='When deleting all partition tables, there is no need to specify further parameters.') module.connect() if affects_multiple: entities = module.list_resource('ptables') if not entities: # Nothing to do; shortcut to exit module.exit_json(changed=False) if not module.desired_absent: # not 'thin' entities = [module.show_resource('ptables', entity['id']) for entity in entities] else: entity = module.find_resource_by_name('ptables', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title('locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name('organizations', entity_dict['organizations'], thin=True) changed = False if not affects_multiple: changed = module.ensure_entity_state('ptables', entity_dict, entity) else: entity_dict.pop('name') for entity in entities: changed |= module.ensure_entity_state('ptables', entity_dict, entity) module.exit_json(changed=changed)