def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( operatingsystem=dict(required=True), template_kind=dict(required=True), provisioning_template=dict(required=False), state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']), ), required_if=( ['state', 'present', ['provisioning_template']], ['state', 'present_with_defaults', ['provisioning_template']], ), supports_check_mode=True, ) (server_params, entity_dict, state) = module.parse_params() try: (server_url, username, password, verify_ssl) = server_params create_server(server_url, (username, password), verify_ssl) except Exception as e: module.fail_json(msg="Failed to connect to Foreman server: %s " % e) ping_server(module) entity_dict['operatingsystem'] = find_operating_system_by_title( module, entity_dict['operatingsystem']) entity_dict['template_kind'] = find_entities_by_name( TemplateKind, [entity_dict['template_kind']], module)[0] entity = find_os_default_template( module, operatingsystem=entity_dict['operatingsystem'], template_kind=entity_dict['template_kind'], failsafe=True, ) # Find Provisioning Template if 'provisioning_template' in entity_dict: if state == 'absent': module.fail_json( msg='Provisioning template must not be specified for deletion.' ) entity_dict['provisioning_template'] = find_entities_by_name( ProvisioningTemplate, [entity_dict['provisioning_template']], module)[0] if entity_dict['provisioning_template'].template_kind.id != entity_dict[ 'template_kind'].id: module.fail_json(msg='Provisioning template kind mismatching.') entity_dict = sanitize_os_default_template_dict(entity_dict) changed = naildown_entity_state(OSDefaultTemplate, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( operatingsystem=dict(required=True), template_kind=dict(required=True), provisioning_template=dict(required=False), state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']), ), required_if=( ['state', 'present', ['provisioning_template']], ['state', 'present_with_defaults', ['provisioning_template']], ), ) (entity_dict, state) = module.parse_params() module.connect() entity_dict['operatingsystem'] = find_operating_system_by_title( module, entity_dict['operatingsystem']) entity_dict['template_kind'] = find_entities_by_name( TemplateKind, [entity_dict['template_kind']], module)[0] entity = find_os_default_template( module, operatingsystem=entity_dict['operatingsystem'], template_kind=entity_dict['template_kind'], failsafe=True, ) # Find Provisioning Template if 'provisioning_template' in entity_dict: if state == 'absent': module.fail_json( msg='Provisioning template must not be specified for deletion.' ) entity_dict['provisioning_template'] = find_entities_by_name( ProvisioningTemplate, [entity_dict['provisioning_template']], module)[0] if entity_dict['provisioning_template'].template_kind.id != entity_dict[ 'template_kind'].id: module.fail_json(msg='Provisioning template kind mismatching.') entity_dict = sanitize_os_default_template_dict(entity_dict) changed = naildown_entity_state(OSDefaultTemplate, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = AnsibleModule( argument_spec=dict( server_url=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), verify_ssl=dict(type='bool', default=True), name=dict(required=True), description=dict(), family=dict(), major=dict(), minor=dict(), architectures=dict(type='list'), media=dict(type='list'), ptables=dict(type='list'), provisioning_templates=dict(type='list'), password_hash=dict(choices=['MD5', 'SHA256', 'SHA512']), state=dict(required=True, choices=['present', 'present_with_defaults', 'absent']), ), supports_check_mode=True, ) handle_no_nailgun(module, HAS_NAILGUN_PACKAGE) operating_system_dict = dict([(k, v) for (k, v) in module.params.iteritems() if v is not None]) server_url = operating_system_dict.pop('server_url') username = operating_system_dict.pop('username') password = operating_system_dict.pop('password') verify_ssl = operating_system_dict.pop('verify_ssl') state = operating_system_dict.pop('state') try: create_server(server_url, (username, password), verify_ssl) except Exception as e: module.fail_json(msg="Failed to connect to Foreman server: %s " % e) ping_server(module) try: entities = find_entities(OperatingSystem, name=operating_system_dict['name']) if len(entities) > 0: entity = entities[0] else: entity = None except Exception as e: module.fail_json(msg='Failed to find entity: %s ' % e) if not entity and (state == 'present' or 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 operating_system_dict.keys(): module.fail_json( msg= '{} is a required parameter to create a new operating system.' .format(param_name)) # Set Architectures of Operating System if 'architectures' in operating_system_dict: operating_system_dict['architectures'] = find_entities_by_name( Architecture, operating_system_dict['architectures'], module) # Set Installation Media of Operating System if 'media' in operating_system_dict: operating_system_dict['media'] = find_entities_by_name( Media, operating_system_dict['media'], module) # Set Partition Tables of Operating System if 'ptables' in operating_system_dict: operating_system_dict['ptables'] = find_entities_by_name( PartitionTable, operating_system_dict['ptables'], module) # Set Provisioning Templates of Operating System if 'provisioning_templates' in operating_system_dict: operating_system_dict[ 'provisioning_templates'] = find_entities_by_name( ProvisioningTemplate, operating_system_dict['provisioning_templates'], module) operating_system_dict = sanitize_operating_system_dict( operating_system_dict) changed = naildown_entity_state(OperatingSystem, operating_system_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = AnsibleModule( argument_spec=dict( server_url=dict(required=True), username=dict(required=True, no_log=True), password=dict(required=True, no_log=True), verify_ssl=dict(type='bool', default=True), audit_comment=dict(), kind=dict(choices=[ 'finish', 'iPXE', 'job_template', 'POAP', 'provision', 'ptable', 'PXELinux', 'PXEGrub', 'PXEGrub2', 'script', 'snippet', 'user_data', 'ZTP', ]), template=dict(), file_name=dict(type='path'), locations=dict(type='list'), locked=dict(type='bool', default=False), name=dict(), organizations=dict(type='list'), operatingsystems=dict(type='list'), state=dict(default='present', choices=['absent', 'present_with_defaults', 'present']), ), supports_check_mode=True, 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: *'!") handle_no_nailgun(module, HAS_NAILGUN_PACKAGE) entity_dict = dict( [(k, v) for (k, v) in module.params.items() if v is not None]) server_url = entity_dict.pop('server_url') username = entity_dict.pop('username') password = entity_dict.pop('password') verify_ssl = entity_dict.pop('verify_ssl') state = entity_dict.pop('state') 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.') name = entity_dict['name'] affects_multiple = name == '*' # sanitize user input, filter unuseful configuration combinations with 'name: *' if affects_multiple: if state == 'present_with_defaults': module.fail_json(msg="'state: present_with_defaults' and 'name: *' cannot be used together") if state == 'absent': if entity_dict.keys() != ['name', 'locked']: module.fail_json(msg="When deleting all templates, there is no need to specify further parameters.") try: create_server(server_url, (username, password), verify_ssl) except Exception as e: module.fail_json(msg='Failed to connect to Foreman server: %s ' % e) ping_server(module) try: if affects_multiple: entities = find_entities(ProvisioningTemplate) else: entities = find_entities(ProvisioningTemplate, name=entity_dict['name']) except Exception as e: module.fail_json(msg='Failed to search for entities: %s ' % e) # Set Locations of Template if 'locations' in entity_dict: entity_dict['locations'] = find_entities_by_name( Location, entity_dict['locations'], module) # Set Organizations of Template if 'organizations' in entity_dict: entity_dict['organizations'] = find_entities_by_name( Organization, entity_dict['organizations'], module) if 'operatingsystems' in entity_dict: entity_dict['operatingsystems'] = [find_operating_system_by_title(module, title) for title in entity_dict['operatingsystems']] if not affects_multiple: entity_dict = find_template_kind(entity_dict, module) entity_dict = sanitize_entity_dict(entity_dict, name_map) changed = False if not affects_multiple: if len(entities) == 0: entity = None else: entity = entities[0] changed = naildown_entity_state( ProvisioningTemplate, entity_dict, entity, state, module) else: entity_dict.pop('name') for entity in entities: changed |= naildown_entity_state( ProvisioningTemplate, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = AnsibleModule( argument_spec=dict( # Foreman credentials server_url=dict(required=True), username=dict(required=True, no_log=True), password=dict(required=True, no_log=True), verify_ssl=dict(type='bool', default=True), # Entity parameter audit_comment=dict(), description_format=dict(), # effectice_user=dict(type='dict'), file_name=dict(type='path'), job_category=dict(), locations=dict(type='list'), locked=dict(type='bool', default=False), name=dict(), organizations=dict(type='list'), provider_type=dict(default='SSH'), snippet=dict(type='bool'), template=dict(), template_inputs=dict(type='list'), # Control parameter state=dict(default='present', choices=['absent', 'present_with_defaults', 'present']), ), supports_check_mode=True, mutually_exclusive=[ ['file_name', 'template'], ], required_one_of=[ ['name', 'file_name', 'template'], ], ) # We do not want a layout 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: *'!") if HAS_IMPORT_ERROR: module.fail_json(msg=IMPORT_ERROR) entity_dict = dict([(k, v) for (k, v) in module.params.items() if v is not None]) server_url = entity_dict.pop('server_url') username = entity_dict.pop('username') password = entity_dict.pop('password') verify_ssl = entity_dict.pop('verify_ssl') state = entity_dict.pop('state') 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 job 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.') name = entity_dict['name'] affects_multiple = name == '*' # sanitize user input, filter unuseful configuration combinations with 'name: *' if affects_multiple: if state == 'present_with_defaults': module.fail_json( msg= "'state: present_with_defaults' and 'name: *' cannot be used together" ) if state == 'absent': if list(entity_dict.keys()) != ['name']: module.fail_json( msg= "When deleting all job templates, there is no need to specify further parameters." ) try: create_server(server_url, (username, password), verify_ssl) except Exception as e: module.fail_json(msg='Failed to connect to Foreman server: %s ' % e) ping_server(module) try: if affects_multiple: entities = find_entities(JobTemplate) else: entities = find_entities(JobTemplate, name=entity_dict['name']) except Exception as e: module.fail_json(msg='Failed to find entity: %s ' % e) # Set Locations of job template if 'locations' in entity_dict: entity_dict['locations'] = find_entities_by_name( Location, entity_dict['locations'], module) # Set Organizations of job template if 'organizations' in entity_dict: entity_dict['organizations'] = find_entities_by_name( Organization, entity_dict['organizations'], module) # TemplateInputs need to be added as separate entities later template_input_list = entity_dict.get('template_inputs', []) entity_dict = sanitize_entity_dict(entity_dict, name_map) changed = False if not affects_multiple: if entities: entity = entities[0] else: entity = None changed, result = naildown_entity(JobTemplate, entity_dict, entity, state, module) if state in ("present", "present_with_defaults"): # Manage TemplateInputs here for template_input_dict in template_input_list: template_input_dict = template_input_dict.copy() # assign template_input to a template template_input_dict['template'] = result ti_entity = find_template_input( module, str(template_input_dict['name']), result) changed |= naildown_entity_state(TemplateInput, template_input_dict, ti_entity, state, module) # remove template inputs if they aren't present in template_input_list found_tis = find_entities( entity_class=lambda: TemplateInput(template=result)) template_input_names = set(ti['name'] for ti in template_input_list) for ti in found_tis: if ti.name not in template_input_names: changed |= naildown_entity_state(TemplateInput, None, ti, "absent", module) else: entity_dict.pop('name') for entity in entities: changed |= naildown_entity_state(JobTemplate, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = AnsibleModule( argument_spec=dict( server_url=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), verify_ssl=dict(type='bool', default=True), operatingsystem=dict(required=True), template_kind=dict(required=True), provisioning_template=dict(required=False), state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']), ), required_if=( ['state', 'present', ['provisioning_template']], ['state', 'present_with_defaults', ['provisioning_template']], ), supports_check_mode=True, ) if has_import_error: module.fail_json(msg=import_error_msg) entity_dict = dict( [(k, v) for (k, v) in module.params.items() if v is not None]) server_url = entity_dict.pop('server_url') username = entity_dict.pop('username') password = entity_dict.pop('password') verify_ssl = entity_dict.pop('verify_ssl') state = entity_dict.pop('state') try: create_server(server_url, (username, password), verify_ssl) except Exception as e: module.fail_json(msg="Failed to connect to Foreman server: %s " % e) ping_server(module) entity_dict['operatingsystem'] = find_operating_system_by_title(module, entity_dict['operatingsystem']) entity_dict['template_kind'] = find_entities_by_name( TemplateKind, [entity_dict['template_kind']], module)[0] entity = find_os_default_template( module, operatingsystem=entity_dict['operatingsystem'], template_kind=entity_dict['template_kind'], failsafe=True, ) # Find Provisioning Template if 'provisioning_template' in entity_dict: if state == 'absent': module.fail_json(msg='Provisioning template must not be specified for deletion.') entity_dict['provisioning_template'] = find_entities_by_name( ProvisioningTemplate, [entity_dict['provisioning_template']], module)[0] if entity_dict['provisioning_template'].template_kind.id != entity_dict['template_kind'].id: module.fail_json(msg='Provisioning template kind mismatching.') entity_dict = sanitize_os_default_template_dict(entity_dict) changed = naildown_entity_state(OSDefaultTemplate, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( # audit_comment=dict(), layout=dict(), file_name=dict(type='path'), locations=dict(type='list'), locked=dict(type='bool'), name=dict(), organizations=dict(type='list'), os_family=dict(choices=list(_OPERATING_SYSTEMS)), state=dict(default='present', choices=['absent', 'present_with_defaults', 'present']), ), supports_check_mode=True, 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, state) = module.parse_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) # 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.') name = entity_dict['name'] affects_multiple = name == '*' # sanitize user input, filter unuseful configuration combinations with 'name: *' if affects_multiple: if state == 'present_with_defaults': module.fail_json( msg= "'state: present_with_defaults' and 'name: *' cannot be used together" ) if state == '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() try: if affects_multiple: entities = find_entities(PartitionTable) else: entities = find_entities(PartitionTable, name=entity_dict['name']) except Exception as e: module.fail_json(msg='Failed to find entity: %s ' % e) # Set Locations of partition table if 'locations' in entity_dict: entity_dict['locations'] = find_entities_by_name( Location, entity_dict['locations'], module) # Set Organizations of partition table if 'organizations' in entity_dict: entity_dict['organizations'] = find_entities_by_name( Organization, entity_dict['organizations'], module) entity_dict = sanitize_entity_dict(entity_dict, name_map) changed = False if not affects_multiple: if len(entities) == 0: entity = None else: entity = entities[0] changed = naildown_entity_state(PartitionTable, entity_dict, entity, state, module) else: entity_dict.pop('name') for entity in entities: changed |= naildown_entity_state(PartitionTable, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), release_name=dict(), description=dict(), family=dict(), major=dict(), minor=dict(), architectures=dict(type='list'), media=dict(type='list'), ptables=dict(type='list'), provisioning_templates=dict(type='list'), password_hash=dict(choices=['MD5', 'SHA256', 'SHA512']), state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']), ), supports_check_mode=True, ) (server_params, operating_system_dict, state) = module.parse_params() try: (server_url, username, password, verify_ssl) = server_params create_server(server_url, (username, password), verify_ssl) except Exception as e: module.fail_json(msg="Failed to connect to Foreman server: %s " % e) ping_server(module) try: # 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 operating_system_dict and operating_system_dict['description'] != '': entity = find_operating_system_by_title(module, title=operating_system_dict['description'], failsafe=True) # If we did not yet find a unique OS, search by name & version if entity is None: search_dict = {'name': operating_system_dict['name']} if 'major' in operating_system_dict: search_dict['major'] = operating_system_dict['major'] if 'minor' in operating_system_dict: search_dict['minor'] = operating_system_dict['minor'] entities = find_entities(OperatingSystem, **search_dict) if len(entities) == 1: entity = entities[0] except Exception as e: module.fail_json(msg='Failed to find entity: %s ' % e) if not entity and (state == 'present' or 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 operating_system_dict.keys(): module.fail_json(msg='{} is a required parameter to create a new operating system.'.format(param_name)) # Set Architectures of Operating System if 'architectures' in operating_system_dict: operating_system_dict['architectures'] = find_entities_by_name( Architecture, operating_system_dict['architectures'], module) # Set Installation Media of Operating System if 'media' in operating_system_dict: operating_system_dict['media'] = find_entities_by_name( Media, operating_system_dict['media'], module) # Set Partition Tables of Operating System if 'ptables' in operating_system_dict: operating_system_dict['ptables'] = find_entities_by_name( PartitionTable, operating_system_dict['ptables'], module) # Set Provisioning Templates of Operating System if 'provisioning_templates' in operating_system_dict: operating_system_dict['provisioning_templates'] = find_entities_by_name( ProvisioningTemplate, operating_system_dict['provisioning_templates'], module) operating_system_dict = sanitize_entity_dict(operating_system_dict, name_map) changed = naildown_entity_state(OperatingSystem, operating_system_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), release_name=dict(), description=dict(), family=dict(required=True), major=dict(required=True), minor=dict(), architectures=dict(type='list'), media=dict(type='list'), ptables=dict(type='list'), provisioning_templates=dict(type='list'), password_hash=dict(choices=['MD5', 'SHA256', 'SHA512']), parameters=dict(type='dict'), state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']), ), ) (operating_system_dict, state) = module.parse_params() module.connect() try: # 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 operating_system_dict and operating_system_dict['description'] != '': entity = find_operating_system_by_title(module, title=operating_system_dict['description'], failsafe=True) # If we did not yet find a unique OS, search by name & version if entity is None: search_dict = {'name': operating_system_dict['name']} if 'major' in operating_system_dict: search_dict['major'] = operating_system_dict['major'] if 'minor' in operating_system_dict: search_dict['minor'] = operating_system_dict['minor'] entities = find_entities(OperatingSystem, **search_dict) if len(entities) == 1: entity = entities[0] except Exception as e: module.fail_json(msg='Failed to find entity: %s ' % e) if not entity and (state == 'present' or 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 operating_system_dict.keys(): module.fail_json(msg='{} is a required parameter to create a new operating system.'.format(param_name)) # Set Architectures of Operating System if 'architectures' in operating_system_dict: operating_system_dict['architectures'] = find_entities_by_name( Architecture, operating_system_dict['architectures'], module) # Set Installation Media of Operating System if 'media' in operating_system_dict: operating_system_dict['media'] = find_entities_by_name( Media, operating_system_dict['media'], module) # Set Partition Tables of Operating System if 'ptables' in operating_system_dict: operating_system_dict['ptables'] = find_entities_by_name( PartitionTable, operating_system_dict['ptables'], module) # Set Provisioning Templates of Operating System if 'provisioning_templates' in operating_system_dict: operating_system_dict['provisioning_templates'] = find_entities_by_name( ProvisioningTemplate, operating_system_dict['provisioning_templates'], module) desired_parameters = operating_system_dict.get('parameters') operating_system_dict = sanitize_entity_dict(operating_system_dict, name_map) changed, operating_system = naildown_entity(OperatingSystem, operating_system_dict, entity, state, module) if desired_parameters is not None: if state == "present" or (state == "present_with_defaults" and entity is None): if entity: current_parameters = OperatingSystemParameter(operatingsystem=operating_system).search() current_parameters = {p.name: p for p in current_parameters} else: current_parameters = {} desired_parameters = {name: {'name': name, 'value': value, 'operatingsystem': operating_system} for name, value in desired_parameters.items()} for name in desired_parameters: current_parameter = current_parameters.pop(name, None) changed |= naildown_entity_state(OperatingSystemParameter, desired_parameters[name], current_parameter, "present", module) for current_parameter in current_parameters.values(): changed |= naildown_entity_state(OperatingSystemParameter, {}, current_parameter, "absent", module) module.exit_json(changed=changed)
def main(): module = AnsibleModule( argument_spec=dict( server_url=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), verify_ssl=dict(type='bool', default=True), operatingsystem=dict(required=True), template_kind=dict(required=True), provisioning_template=dict(required=False), state=dict(required=True, choices=['present', 'present_with_defaults', 'absent']), ), required_if=( ['state', 'present', ['provisioning_template']], ['state', 'present_with_defaults', ['provisioning_template']], ), supports_check_mode=True, ) handle_no_nailgun(module, HAS_NAILGUN_PACKAGE) entity_dict = dict([(k, v) for (k, v) in module.params.iteritems() if v is not None]) server_url = entity_dict.pop('server_url') username = entity_dict.pop('username') password = entity_dict.pop('password') verify_ssl = entity_dict.pop('verify_ssl') state = entity_dict.pop('state') try: create_server(server_url, (username, password), verify_ssl) except Exception as e: module.fail_json(msg="Failed to connect to Foreman server: %s " % e) ping_server(module) os_list = OperatingSystem().search( set(), {'search': 'title~"{}"'.format(entity_dict['operatingsystem'])}) if len(os_list) == 0: module.fail_json(msg='Operating system "{}" not found.'.format( entity_dict['operatingsystem'])) if len(os_list) > 1: module.fail_json(msg='Provided operating system ({}) is not unique.'. format(entity_dict['operatingsystem'])) entity_dict['operatingsystem'] = os_list[0] entity_dict['template_kind'] = find_entities_by_name( TemplateKind, [entity_dict['template_kind']], module)[0] entity = find_os_default_template( module, operatingsystem=entity_dict['operatingsystem'], template_kind=entity_dict['template_kind'], failsafe=True, ) # Find Provisioning Template if 'provisioning_template' in entity_dict: if state == 'absent': module.fail_json( msg='Provisioning template must not be specified for deletion.' ) entity_dict['provisioning_template'] = find_entities_by_name( ProvisioningTemplate, [entity_dict['provisioning_template']], module)[0] if entity_dict['provisioning_template'].template_kind.id != entity_dict[ 'template_kind'].id: module.fail_json(msg='Provisioning template kind mismatching.') entity_dict = sanitize_os_default_template_dict(entity_dict) changed = naildown_entity_state(OSDefaultTemplate, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = AnsibleModule( argument_spec=dict( server_url=dict(required=True), username=dict(required=True, no_log=True), password=dict(required=True, no_log=True), verify_ssl=dict(type='bool', default=True), # audit_comment=dict(), layout=dict(), file_name=dict(type='path'), locations=dict(type='list'), # locked=dict(type='bool', default=False), name=dict(), organizations=dict(type='list'), os_family=dict(choices=list(_OPERATING_SYSTEMS)), state=dict(required=True, choices=['absent', 'present', 'latest']), ), supports_check_mode=True, 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: *'!") handle_no_nailgun(module, HAS_NAILGUN_PACKAGE) ptable_dict = dict([(k, v) for (k, v) in module.params.iteritems() if v is not None]) server_url = ptable_dict.pop('server_url') username = ptable_dict.pop('username') password = ptable_dict.pop('password') verify_ssl = ptable_dict.pop('verify_ssl') state = ptable_dict.pop('state') file_name = ptable_dict.pop('file_name', None) if file_name or 'layout' in ptable_dict: if file_name: parsed_dict = parse_template_from_file(file_name, module) else: parsed_dict = parse_template(ptable_dict['layout'], 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 partition table name!") # module params are priorized parsed_dict.update(ptable_dict) ptable_dict = parsed_dict # make sure, we have a name if 'name' not in ptable_dict: if file_name: ptable_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.') name = ptable_dict['name'] affects_multiple = name == '*' # sanitize user input, filter unuseful configuration combinations with 'name: *' if affects_multiple: if state == 'present': module.fail_json( msg="'state: present' and 'name: *' cannot be used together") if state == 'absent': if ptable_dict.keys() != ['name']: module.fail_json( msg= "When deleting all partition tables, there is no need to specify further parameters." ) try: create_server(server_url, (username, password), verify_ssl) except Exception as e: module.fail_json(msg='Failed to connect to Foreman server: %s ' % e) ping_server(module) try: if affects_multiple: entities = find_entities(PartitionTable) else: entities = find_entities(PartitionTable, name=ptable_dict['name']) except Exception as e: module.fail_json(msg='Failed to find entity: %s ' % e) # Set Locations of partition table if 'locations' in ptable_dict: ptable_dict['locations'] = find_entities_by_name( Location, ptable_dict['locations'], module) # Set Organizations of partition table if 'organizations' in ptable_dict: ptable_dict['organizations'] = find_entities_by_name( Organization, ptable_dict['organizations'], module) ptable_dict = sanitize_ptable_dict(ptable_dict) changed = False if not affects_multiple: if len(entities) == 0: entity = None else: entity = entities[0] changed = naildown_entity_state(PartitionTable, ptable_dict, entity, state, module) else: ptable_dict.pop('name') for entity in entities: changed |= naildown_entity_state(PartitionTable, ptable_dict, entity, state, module) module.exit_json(changed=changed)