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(), ), supports_check_mode=True, ) if has_import_error: module.fail_json(msg=import_error_msg) params_dict = dict([(k, v) for (k, v) in module.params.items() if v is not None]) server_url = params_dict.pop('server_url') username = params_dict.pop('username') password = params_dict.pop('password') verify_ssl = params_dict.pop('verify_ssl') 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) search_params = {k: v for (k, v) in params_dict.items() if k == 'name'} entities = find_entities(Setting, **search_params) settings = [{ key: getattr(entity, value) for (key, value) in name_map.items() } for entity in entities] module.exit_json(changed=False, settings=settings)
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), name=dict(required=True), product=dict(), organization=dict(required=True), repositories=dict(required=True, type='list'), state=dict(required=True, choices=['disabled', 'enabled']), ), supports_check_mode=True, ) if has_import_error: module.fail_json(msg=import_error_msg) server_url = module.params['server_url'] username = module.params['username'] password = module.params['password'] verify_ssl = module.params['verify_ssl'] name = module.params['name'] product = module.params['product'] organization = module.params['organization'] repositories = module.params['repositories'] state = module.params['state'] create_server(server_url, (username, password), verify_ssl) ping_server(module) try: changed = repository_set(module, name, organization, product, state, repositories=repositories) module.exit_json(changed=changed) except Exception as e: module.fail_json(msg=e)
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 = 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), name=dict(required=True), organization=dict(required=True), composite=dict(type='bool', default=False), auto_publish=dict(type='bool', default=False), components=dict(type='list'), repositories=dict(type='list'), state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']), ), supports_check_mode=True, mutually_exclusive=[['repositories', 'components']], ) 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['organization'] = find_organization(module, name=entity_dict['organization']) if 'repositories' in entity_dict and not entity_dict['composite']: entity_dict['repositories'] = find_repositories(module, entity_dict['repositories'], entity_dict['organization']) content_view_entity = find_content_view(module, name=entity_dict['name'], organization=entity_dict['organization'], failsafe=True) content_view_dict = sanitize_entity_dict(entity_dict, name_map) changed, content_view_entity = naildown_entity(ContentView, content_view_dict, content_view_entity, state, module) # only update CVC's of newly created or updated CCV's if state == 'present' or (state == 'present_with_defaults' and changed): current_cvcs = [] if hasattr(content_view_entity, 'content_view_component'): current_cvcs = [cvc.read() for cvc in content_view_entity.content_view_component] if 'components' in entity_dict and content_view_entity.composite: for component in entity_dict['components']: cvc = component.copy() cvc['content_view'] = find_content_view(module, name=component['content_view'], organization=entity_dict['organization']) cvc_matched = None for _cvc in current_cvcs: if _cvc.content_view.id == cvc['content_view'].id: cvc_matched = _cvc force_update = list() if 'version' in component: cvc['version'] = find_content_view_version(module, cvc['content_view'], version=component['version']) cvc['latest'] = False if cvc_matched and cvc_matched.latest: # When changing to latest=False & version is the latest we must send 'content_view_version' to the server force_update.append('content_view_version') if cvc_matched: cvc['composite_content_view'] = content_view_entity cvc_dict = sanitize_entity_dict(cvc, cvc_map) cvc_changed = naildown_entity_state(ContentViewComponent, cvc_dict, cvc_matched, 'present', module, force_update=force_update) current_cvcs.remove(cvc_matched) if cvc_changed: changed = cvc_changed else: for attr in ['latest', 'version']: if attr not in cvc: cvc[attr] = None ContentViewComponent(composite_content_view=content_view_entity, content_view=cvc['content_view'], latest=cvc['latest'], content_view_version=cvc['version']).add() changed = True for cvc in current_cvcs: # desired cvcs have already been updated and removed from `current_cvcs` cvc.remove() changed = True module.exit_json(changed=changed)
def main(): module = KatelloEntityAnsibleModule( argument_spec=dict( name=dict(required=True), new_name=dict(), lifecycle_environment=dict(), content_view=dict(), subscriptions=dict(type='list'), content_overrides=dict(type='list'), auto_attach=dict(type='bool', default=True), state=dict(default='present', choices=['present', 'present_with_defaults', 'absent', 'copied']), ), supports_check_mode=True, required_if=[ ['state', 'copied', ['new_name']], ], ) (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['organization'] = find_organization(module, name=entity_dict['organization']) if 'lifecycle_environment' in entity_dict: entity_dict['lifecycle_environment'] = find_lifecycle_environment(module, entity_dict['lifecycle_environment'], entity_dict['organization']) if 'content_view' in entity_dict: entity_dict['content_view'] = find_content_view(module, entity_dict['content_view'], entity_dict['organization']) activation_key_dict = sanitize_entity_dict(entity_dict, name_map) activation_key_entity = find_activation_key(module, name=entity_dict['name'], organization=entity_dict['organization'], failsafe=True) try: changed, activation_key_entity = naildown_entity(ActivationKey, activation_key_dict, activation_key_entity, state, module) # only update subscriptions of newly created or updated AKs # copied keys inherit the subscriptions of the origin, so one would not have to specify them again # deleted keys don't need subscriptions anymore either if state == 'present' or (state == 'present_with_defaults' and changed): if 'subscriptions' in entity_dict: subscriptions = entity_dict['subscriptions'] desired_subscription_ids = map(lambda s: s.id, find_subscriptions(module, subscriptions, entity_dict['organization'])) current_subscriptions = [Subscription(**result) for result in Subscription().search_normalize(activation_key_entity.subscriptions()['results'])] current_subscription_ids = map(lambda s: s.id, current_subscriptions) if set(desired_subscription_ids) != set(current_subscription_ids): if not module.check_mode: for subscription_id in set(desired_subscription_ids) - set(current_subscription_ids): activation_key_entity.add_subscriptions(data={'quantity': 1, 'subscription_id': subscription_id}) for subscription_id in set(current_subscription_ids) - set(desired_subscription_ids): activation_key_entity.remove_subscriptions(data={'subscription_id': subscription_id}) changed = True if 'content_overrides' in entity_dict: content_overrides = entity_dict['content_overrides'] product_content = activation_key_entity.product_content() current_content_overrides = set() for product in product_content['results']: if product['enabled_content_override'] is not None: current_content_overrides.add((product['content']['label'], product['enabled_content_override'])) desired_content_overrides = set() for product in content_overrides: desired_content_overrides.add((product['label'], override_to_boolnone(product['override']))) if desired_content_overrides != current_content_overrides: if not module.check_mode: for (label, override) in current_content_overrides - desired_content_overrides: activation_key_entity.content_override(data={'content_override': {'content_label': label, 'value': 'default'}}) for (label, override) in desired_content_overrides - current_content_overrides: activation_key_entity.content_override(data={'content_override': {'content_label': label, 'value': str(override_to_boolnone(override)).lower()}}) changed = True module.exit_json(changed=changed) except Exception as e: module.fail_json(msg=e)
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), src=dict(required=True, type='path', aliases=['file']), repository=dict(required=True), product=dict(required=True), organization=dict(required=True), ), 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') 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['organization'] = find_organization(module, name=entity_dict['organization']) entity_dict['product'] = find_product(module, name=entity_dict['product'], organization=entity_dict['organization']) entity_dict['repository'] = find_repository(module, name=entity_dict['repository'], product=entity_dict['product']) content_unit = None if entity_dict['repository'].content_type == "yum": name, version, release, arch = check_output("rpm --queryformat '%%{NAME} %%{VERSION} %%{RELEASE} %%{ARCH}' -qp %s" % entity_dict['src'], shell=True).decode('ascii').split() query = "name = \"{}\" and version = \"{}\" and release = \"{}\" and arch = \"{}\"".format(name, version, release, arch) content_unit = find_package(module, query, repository=entity_dict['repository'], failsafe=True) elif entity_dict['repository'].content_type == "file": h = hashlib.sha256() with open(entity_dict['src'], "rb") as f: for chunk in iter(lambda: f.read(4096), b""): h.update(chunk) checksum = h.hexdigest() name = os.path.basename(entity_dict['src']) query = "name = \"{}\" and checksum = \"{}\"".format(name, checksum) content_unit = find_file(module, query, repository=entity_dict['repository'], failsafe=True) changed = False if not content_unit: try: changed = upload(module, entity_dict['src'], entity_dict['repository']) except Exception as e: module.fail_json(msg=to_native(e)) 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 = 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), locations=dict(type='list'), organizations=dict(type='list'), operatingsystems=dict(type='list'), os_family=dict(), path=dict(), state=dict(choices=['present', 'absent'], default='present'), ), supports_check_mode=True, ) if has_import_error: module.fail_json(msg=import_error_msg) medium_dict = dict([(k, v) for (k, v) in module.params.items() if v is not None]) server_url = medium_dict.pop('server_url') username = medium_dict.pop('username') password = medium_dict.pop('password') verify_ssl = medium_dict.pop('verify_ssl') state = medium_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 = find_installation_medium(module, name=medium_dict['name'], failsafe=True) if 'operatingsystems' in medium_dict: medium_dict['operatingsystems'] = find_operating_systems_by_title( module, medium_dict['operatingsystems']) if len(medium_dict['operatingsystems'] ) == 1 and 'os_family' not in medium_dict and entity is None: medium_dict['os_family'] = medium_dict['operatingsystems'][ 0].family if 'locations' in medium_dict: medium_dict['locations'] = find_locations(module, medium_dict['locations']) if 'organizations' in medium_dict: medium_dict['organizations'] = find_organizations( module, medium_dict['organizations']) medium_dict = sanitize_entity_dict(medium_dict, name_map) changed = naildown_entity_state(Media, medium_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: *'!") (server_params, 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 entity_dict.keys() != ['name']: module.fail_json(msg="When deleting all partition tables, there is no need to specify further parameters.") 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: 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 = 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), product=dict(required=True), label=dict(), organization=dict(required=True), name=dict(required=True), content_type=dict( required=True, choices=['docker', 'ostree', 'yum', 'puppet', 'file', 'deb']), url=dict(), gpg_key=dict(), docker_upstream_name=dict(), download_policy=dict( choices=['background', 'immediate', 'on_demand']), state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']), ), 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') verify_ssl = entity_dict.pop('verify_ssl') username = entity_dict.pop('username') password = entity_dict.pop('password') state = entity_dict.pop('state') if entity_dict[ 'content_type'] != 'docker' and 'docker_upstream_name' in entity_dict: module.fail_json( msg= "docker_upstream_name should not be set unless content_type: docker" ) 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['organization'] = find_organization( module, name=entity_dict['organization']) entity_dict['product'] = find_product( module, name=entity_dict['product'], organization=entity_dict['organization']) if 'gpg_key' in entity_dict: entity_dict['gpg_key'] = find_content_credential( module, name=entity_dict['gpg_key'], organization=entity_dict['organization']) entity = find_repository(module, name=entity_dict['name'], product=entity_dict['product'], failsafe=True) entity_dict = sanitize_entity_dict(entity_dict, name_map) changed = naildown_entity_state(Repository, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = KatelloEntityAnsibleModule( argument_spec=dict( product=dict(required=True), label=dict(), name=dict(required=True), content_type=dict( required=True, choices=['docker', 'ostree', 'yum', 'puppet', 'file', 'deb']), url=dict(), gpg_key=dict(), docker_upstream_name=dict(), download_policy=dict( choices=['background', 'immediate', 'on_demand']), state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']), ), supports_check_mode=True, ) (server_params, entity_dict, state) = module.parse_params() if entity_dict[ 'content_type'] != 'docker' and 'docker_upstream_name' in entity_dict: module.fail_json( msg= "docker_upstream_name should not be set unless content_type: docker" ) 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['organization'] = find_organization( module, name=entity_dict['organization']) entity_dict['product'] = find_product( module, name=entity_dict['product'], organization=entity_dict['organization']) if 'gpg_key' in entity_dict: entity_dict['gpg_key'] = find_content_credential( module, name=entity_dict['gpg_key'], organization=entity_dict['organization']) entity = find_repository(module, name=entity_dict['name'], product=entity_dict['product'], failsafe=True) entity_dict = sanitize_entity_dict(entity_dict, name_map) changed = naildown_entity_state(Repository, 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(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 = ForemanAnsibleModule( argument_spec=dict( name=dict(required=True), description=dict(), repositories=dict(type='list', default=[]), inclusion=dict(type='bool', default=False), content_view=dict(required=True), filter_type=dict( required=True, choices=['rpm', 'package_group', 'erratum', 'docker']), organization=dict(required=True), filter_state=dict(default='present', choices=['present', 'absent']), rule_state=dict(default='present', choices=['present', 'absent']), rule_name=dict(aliases=['package_name', 'package_group', 'tag']), date_type=dict(default='updated', choices=['issued', 'updated']), end_date=dict(), errata_id=dict(), max_version=dict(), min_version=dict(), start_date=dict(), types=dict(default=["bugfix", "enhancement", "security"], type='list'), version=dict(), ), supports_check_mode=False, ) (server_params, entity_dict) = module.parse_params() filter_state = entity_dict.pop('filter_state') rule_state = entity_dict.pop('rule_state') 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) organization = find_organization(module, name=entity_dict.pop('organization')) entity_dict['content_view'] = find_content_view( module, name=entity_dict['content_view'], organization=organization) if len(entity_dict['repositories']) > 0: entity_dict['repositories'] = find_repositories( module, entity_dict['repositories'], organization) content_view_filter_entity = find_content_view_filter( module, name=entity_dict['name'], content_view=entity_dict['content_view'], failsafe=True) content_view_filter_dict = sanitize_entity_dict(entity_dict, content_filter_map) content_view_filter_changed = naildown_entity_state( AbstractContentViewFilter, content_view_filter_dict, content_view_filter_entity, filter_state, module) if entity_dict['filter_type'] == 'erratum': entity_dict['rule_name'] = None elif 'rule_name' not in entity_dict: entity_dict['rule_name'] = entity_dict['name'] # Find content_view_filter again as it may have just been created entity_dict['content_view_filter'] = find_content_view_filter( module, name=entity_dict['name'], content_view=entity_dict['content_view'], failsafe=True) if entity_dict['content_view_filter'] is not None: if 'errata_id' in entity_dict: rule_map = content_filter_rule_erratum_id_map entity_dict['errata'] = find_errata(module, id=entity_dict['errata_id'], organization=organization) content_view_filter_rule_entity = find_content_view_filter_rule( module, content_view_filter=entity_dict['content_view_filter'], errata=entity_dict['errata'], failsafe=True) else: rule_map = globals()['content_filter_rule_%s_map' % (entity_dict['filter_type'])] content_view_filter_rule_entity = find_content_view_filter_rule( module, content_view_filter=entity_dict['content_view_filter'], name=entity_dict['rule_name'], failsafe=True) if entity_dict['filter_type'] == 'package_group': entity_dict['uuid'] = find_package_group( module, name=entity_dict['rule_name']).uuid content_view_filter_rule_dict = sanitize_entity_dict( entity_dict, rule_map) check_missing = [ 'min_version', 'max_version', 'version', 'start_date', 'end_date', 'architecture', 'date_type' ] content_view_filter_rule_changed = naildown_entity_state( ContentViewFilterRule, content_view_filter_rule_dict, content_view_filter_rule_entity, rule_state, module, check_missing) changed = content_view_filter_changed or content_view_filter_rule_changed else: changed = content_view_filter_changed 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( 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), content_view=dict(required=True), organization=dict(required=True), state=dict(default='present', choices=['present', 'absent']), version=dict(), lifecycle_environments=dict(type='list', default=['Library']), force=dict(type='bool', aliases=['force_promote'], default=False), force_yum_metadata_regeneration=dict(type='bool', default=False), synchronous=dict(type='bool', default=True), current_lifecycle_environment=dict(), ), mutually_exclusive=[['current_lifecycle_environment', 'version']], supports_check_mode=True, ) if has_import_error: module.fail_json(msg=import_error_msg) set_task_timeout(3600000) # 60 minutes params_dict = dict([(k, v) for (k, v) in module.params.items() if v is not None]) server_url = module.params['server_url'] username = module.params['username'] password = module.params['password'] verify_ssl = module.params['verify_ssl'] state = module.params['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) organization = find_organization(module, params_dict['organization']) content_view = find_content_view(module, name=params_dict['content_view'], organization=organization) if 'current_lifecycle_environment' in params_dict: params_dict[ 'current_lifecycle_environment'] = find_lifecycle_environment( module, name=params_dict['current_lifecycle_environment'], organization=organization) content_view_version = find_content_view_version( module, content_view, environment=params_dict['current_lifecycle_environment']) elif 'version' in params_dict: content_view_version = find_content_view_version( module, content_view, version=params_dict['version'], failsafe=True) else: content_view_version = None changed = False if state == 'present': if content_view_version is None: kwargs = dict(data=dict()) if 'description' in params_dict: kwargs['data'].update(description=params_dict['description']) if 'force_metadata_regeneration' in params_dict: kwargs['data'].update( force_yum_metadata_regeneration=params_dict[ 'force_metadata_regeneration']) if 'version' in params_dict: kwargs['data'].update( major=map(int, str(params_dict['version']).split('.'))[0]) kwargs['data'].update( minor=map(int, str(params_dict['version']).split('.'))[1]) response = content_view.publish(params_dict['synchronous'], **kwargs) changed = True content_view_version = ContentViewVersion( id=response['output']['content_view_version_id']).read() if 'lifecycle_environments' in params_dict: lifecycle_environments = find_lifecycle_environments( module, names=params_dict['lifecycle_environments'], organization=organization) le_changed = promote_content_view_version( module, content_view_version, organization, lifecycle_environments, params_dict['synchronous'], force=params_dict['force'], force_yum_metadata_regeneration=params_dict[ 'force_yum_metadata_regeneration']) elif state == 'absent': changed = naildown_entity_state(ContentViewVersion, dict(), content_view_version, state, module) module.exit_json(changed=changed or le_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 = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), network_type=dict(choices=['IPv4', 'IPv6'], default='IPv4'), dns_primary=dict(), dns_secondary=dict(), domains=dict(type='list'), gateway=dict(), network=dict(required=True), cidr=dict(type='int'), mask=dict(), from_ip=dict(), to_ip=dict(), boot_mode=dict(choices=['DHCP', 'Static'], default='DHCP'), ipam=dict(choices=['DHCP', 'Internal DB'], default='DHCP'), dhcp_proxy=dict(), tftp_proxy=dict(), discovery_proxy=dict(), dns_proxy=dict(), remote_execution_proxies=dict(type='list'), vlanid=dict(type='int'), mtu=dict(type='int'), locations=dict(type='list'), organizations=dict(type='list'), ), required_one_of=[['cidr', 'mask']], 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) try: # Try to find the Subnet to work on entity = find_subnet(module, name=entity_dict['name'], failsafe=True) except Exception as e: module.fail_json(msg='Failed to find entity: %s ' % e) if 'mask' in entity_dict and 'cidr' not in entity_dict: entity_dict['cidr'] = IPNetwork( '%s/%s' % (entity_dict['network'], entity_dict['mask'])).prefixlen elif 'mask' not in entity_dict and 'cidr' in entity_dict: entity_dict['mask'] = IPNetwork( '%s/%s' % (entity_dict['network'], entity_dict['cidr'])).netmask if 'domains' in entity_dict: entity_dict['domains'] = find_domains(module, entity_dict['domains']) for feature in ('dhcp_proxy', 'tftp_proxy', 'discovery_proxy', 'dns_proxy', 'remote_execution_proxies'): if feature in entity_dict: entity_dict[feature] = find_smart_proxy(module, entity_dict[feature]) if 'organizations' in entity_dict: entity_dict['organizations'] = find_organizations( module, entity_dict['organizations']) if 'locations' in entity_dict: entity_dict['locations'] = find_locations(module, entity_dict['locations']) entity_dict = sanitize_entity_dict(entity_dict, name_map) changed = naildown_entity_state(Subnet, entity_dict, entity, state, module) module.exit_json(changed=changed)