def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( updated_name=dict(), ), entity_spec=dict( name=dict(required=True), operatingsystems=dict(type='entity_list', flat_name='operatingsystem_ids'), ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('architectures', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if entity and 'updated_name' in entity_dict: entity_dict['name'] = entity_dict.pop('updated_name') if 'operatingsystems' in entity_dict: entity_dict['operatingsystems'] = module.find_operatingsystems(entity_dict['operatingsystems'], thin=True) changed = module.ensure_entity_state('architectures', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( entity_spec=dict( name=dict(required=True), usergroup=dict(required=True), auth_source_ldap=dict(required=True, type='entity', flat_name='auth_source_id'), ), ) entity_dict = module.clean_params() module.connect() params = {"usergroup_id": entity_dict.pop('usergroup')} entity = None # There is no way to find by name via API search, so we need # to iterate over all external user groups of a given usergroup for external_usergroup in module.list_resource("external_usergroups", params=params): if external_usergroup['name'] == entity_dict['name']: entity = external_usergroup entity_dict['auth_source_ldap'] = module.find_resource_by_name('auth_sources', entity_dict['auth_source_ldap'], thin=True) changed = module.ensure_entity_state('external_usergroups', entity_dict, entity, params) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( entity_spec=dict( name=dict(required=True), controller=dict(required=True), public=dict(default='true', type='bool'), query=dict(), ), argument_spec=dict(state=dict( default='present', choices=['present_with_defaults', 'present', 'absent']), ), required_if=( ['state', 'present', ['query']], ['state', 'present_with_defaults', ['query']], ), ) entity_dict = module.clean_params() module.connect() search = 'name="{0}",controller="{1}"'.format(entity_dict['name'], entity_dict['controller']) entity = module.find_resource('bookmarks', search, failsafe=True) module.ensure_entity('bookmarks', entity_dict, entity) module.exit_json()
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( host=dict(required=True), state=dict(default='present', choices=['present', 'absent', 'reverted']), ), entity_spec=dict( name=dict(required=True), description=dict(), ), ) snapshot_dict = module.clean_params() module.connect() host = module.find_resource_by_name('hosts', name=snapshot_dict['host'], failsafe=False, thin=True) scope = {'host_id': host['id']} entity = module.find_resource_by_name('snapshots', name=snapshot_dict['name'], params=scope, failsafe=True) changed = module.ensure_entity_state('snapshots', snapshot_dict, entity, params=scope) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule(entity_spec=dict( name=dict(required=True), realm_proxy=dict(type='entity', flat_name='realm_proxy_id', required=True), realm_type=dict(required=True, choices=[ 'Red Hat Identity Management', 'FreeIPA', 'Active Directory' ]), ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('realms', name=entity_dict['name'], failsafe=True) if not module.desired_absent: entity_dict['realm_proxy'] = module.find_resource_by_name( 'smart_proxies', entity_dict['realm_proxy'], thin=True) changed = module.ensure_entity_state('realms', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), value=dict(), state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']), ), required_if=( ['state', 'present_with_defaults', ['value']], ['state', 'present', ['value']], ), ) (global_parameter_dict, state) = module.parse_params() module.connect() try: entities = find_entities(CommonParameter, name=global_parameter_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) global_parameter_dict = sanitize_entity_dict(global_parameter_dict, name_map) changed = naildown_entity_state(CommonParameter, global_parameter_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( host=dict(required=True), state=dict(default='present', choices=['present', 'absent', 'reverted']), ), foreman_spec=dict( name=dict(required=True), description=dict(), ), required_plugins=[('snapshot_management', ['*'])], ) snapshot_dict = module.clean_params() with module.api_connection(): host = module.find_resource_by_name('hosts', name=snapshot_dict['host'], failsafe=False, thin=True) scope = {'host_id': host['id']} entity = module.find_resource_by_name('snapshots', name=snapshot_dict['name'], params=scope, failsafe=True) module.ensure_entity('snapshots', snapshot_dict, entity, params=scope)
def main(): module = ForemanEntityAnsibleModule( entity_spec=dict( name=dict(required=True), value=dict(type='raw'), parameter_type=dict(default='string', choices=[ 'string', 'boolean', 'integer', 'real', 'array', 'hash', 'yaml', 'json' ]), ), argument_spec=dict( state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']), updated_name=dict(), ), required_if=( ['state', 'present_with_defaults', ['value']], ['state', 'present', ['value']], ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('common_parameters', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if entity and 'updated_name' in entity_dict: entity_dict['name'] = entity_dict.pop('updated_name') # Convert values according to their corresponding parameter_type if entity and 'parameter_type' not in entity: entity['parameter_type'] = 'string' entity_dict['value'] = parameter_value_to_str( entity_dict['value'], entity_dict['parameter_type']) if entity and 'value' in entity: entity['value'] = parameter_value_to_str( entity['value'], entity.get('parameter_type', 'string')) module.ensure_entity('common_parameters', entity_dict, entity) module.exit_json()
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), label=dict(), ), 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 = find_organization(module, name=entity_dict['name'], failsafe=True) entity_dict = sanitize_entity_dict(entity_dict, name_map) changed = naildown_entity_state(Organization, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule(entity_spec=dict( name=dict(required=True), description=dict(), label=dict(), ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('organizations', name=entity_dict['name'], failsafe=True) module.ensure_entity('organizations', entity_dict, entity) module.exit_json()
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), description=dict(), dns_proxy=dict(), locations=dict(type='list'), organizations=dict(type='list'), ), supports_check_mode=True, ) (server_params, domain_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 Domain to work on entity = find_domain(module, name=domain_dict['name'], failsafe=True) except Exception as e: module.fail_json(msg='Failed to find entity: %s ' % e) if 'dns_proxy' in domain_dict: domain_dict['dns_proxy'] = find_smart_proxy(module, domain_dict['dns_proxy']) if 'locations' in domain_dict: domain_dict['locations'] = find_locations(module, domain_dict['locations']) if 'organizations' in domain_dict: domain_dict['organizations'] = find_organizations( module, domain_dict['organizations']) domain_dict = sanitize_entity_dict(domain_dict, name_map) changed = naildown_entity_state(Domain, domain_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule(argument_spec=dict( hostname=dict(required=True), state=dict(default='present', choices=['on', 'off', 'state']), ), ) (host_dict, state) = module.parse_params() module.connect() entity = find_host(module, host_dict['hostname'], failsafe=True) if state == 'state': power_state = query_power_state(module, entity) module.exit_json(changed=False, power_state=power_state) else: changed = naildown_power_state(module, entity, state) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), value=dict(), state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']), ), required_if=( ['state', 'present_with_defaults', ['value']], ['state', 'present', ['value']], ), supports_check_mode=True, ) (server_params, global_parameter_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: entities = find_entities(CommonParameter, name=global_parameter_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) global_parameter_dict = sanitize_entity_dict(global_parameter_dict, name_map) changed = naildown_entity_state(CommonParameter, global_parameter_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule(entity_spec=dict( compute_profile=dict(required=True, type='entity', flat_name='compute_profile_id'), compute_resource=dict(required=True, type='entity', flat_name='compute_resource_id'), vm_attrs=dict(type='dict', aliases=['vm_attributes']), ), ) entity_dict = module.clean_params() module.connect() entity_dict['compute_resource'] = module.find_resource_by_name( 'compute_resources', name=entity_dict['compute_resource'], failsafe=False, thin=False) compute_attributes = entity_dict['compute_resource'].get( 'compute_attributes') entity_dict['compute_profile'] = module.find_resource_by_name( 'compute_profiles', name=entity_dict['compute_profile'], failsafe=False, thin=True) entity = next((item for item in compute_attributes if item.get( 'compute_profile_id') == entity_dict['compute_profile']['id']), None) changed = module.ensure_entity_state('compute_attributes', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), locations=dict(type='list'), organizations=dict(type='list'), operatingsystems=dict(type='list'), os_family=dict(), path=dict(), ), supports_check_mode=True, ) (server_params, medium_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 = 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( name=dict(required=True), locations=dict(type='list'), organizations=dict(type='list'), ), 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: entity = find_environment(module, name=entity_dict['name'], failsafe=True) 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_locations(module, entity_dict['locations']) # Set Organizations of partition table if 'organizations' in entity_dict: entity_dict['organizations'] = find_organizations(module, entity_dict['organizations']) entity_dict = sanitize_entity_dict(entity_dict, name_map) changed = naildown_entity_state(Environment, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), hostgroup=dict(), location=dict(), organization=dict(), enabled=dict(default='true', type='bool'), state=dict(default='present', choices=['present_with_defaults', 'present', 'absent']), ), required_if=( ['state', 'present_with_defaults', ['hostgroup']], ['state', 'present', ['hostgroup']], ), supports_check_mode=True, ) (host_dict, state) = module.parse_params() module.connect() host_dict['hostgroup'] = find_hostgroup(module, host_dict['hostgroup'], failsafe=True) host_dict['name'] = host_dict['name'] + '.' + \ host_dict['hostgroup'].domain.read().fullname entity = find_host(module, host_dict['name'], failsafe=True) if 'location' in host_dict: host_dict['location'] = find_location(module, host_dict['location']) if 'organization' in host_dict: host_dict['organization'] = find_organization( module, host_dict['organization']) host_dict = sanitize_entity_dict(host_dict, name_map) changed = naildown_entity_state(Host, host_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), locations=dict(type='list'), organizations=dict(type='list'), operatingsystems=dict(type='list'), os_family=dict(), path=dict(), ), supports_check_mode=True, ) (medium_dict, state) = module.parse_params() module.connect() 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( name=dict(required=True), label=dict(), ), supports_check_mode=True, ) (entity_dict, state) = module.parse_params() module.connect() entity = find_organization(module, name=entity_dict['name'], failsafe=True) entity_dict = sanitize_entity_dict(entity_dict, name_map) changed = naildown_entity_state(Organization, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), parent=dict(), organizations=dict(type='list'), ), 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) name_or_title = entity_dict.pop('name') parent = entity_dict.pop('parent', None) # Get short name and parent from provided name parent_from_title, name = split_fqn(name_or_title) entity_dict['name'] = name if parent: entity_dict['parent'] = find_location(module, parent) elif parent_from_title: entity_dict['parent'] = find_location(module, parent_from_title) if 'organizations' in entity_dict: entity_dict['organizations'] = find_organizations(module, entity_dict['organizations']) entity = find_location(module, title=build_fqn(name_or_title, parent), failsafe=True) entity_dict = sanitize_entity_dict(entity_dict, name_map) changed = naildown_entity_state(Location, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), parent=dict(), organizations=dict(type='list'), ), supports_check_mode=True, ) (entity_dict, state) = module.parse_params() module.connect() name_or_title = entity_dict.pop('name') parent = entity_dict.pop('parent', None) # Get short name and parent from provided name parent_from_title, name = split_fqn(name_or_title) entity_dict['name'] = name if parent: entity_dict['parent'] = find_location(module, parent) elif parent_from_title: entity_dict['parent'] = find_location(module, parent_from_title) if 'organizations' in entity_dict: entity_dict['organizations'] = find_organizations( module, entity_dict['organizations']) entity = find_location(module, title=build_fqn(name_or_title, parent), failsafe=True) entity_dict = sanitize_entity_dict(entity_dict, name_map) changed = naildown_entity_state(Location, entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( 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']} # Default templates do not support a scoped search # see: https://projects.theforeman.org/issues/27722 entities = module.list_resource('os_default_templates', params=scope) entity = next( (item for item in entities if item['template_kind_id'] == entity_dict['template_kind']['id']), None) # 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.') module.ensure_entity('os_default_templates', entity_dict, entity, params=scope) module.exit_json()
def main(): module = ForemanEntityAnsibleModule( 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, operatingsystem = module.ensure_entity('operatingsystems', entity_dict, entity) if operatingsystem: scope = {'operatingsystem_id': operatingsystem['id']} changed |= module.ensure_scoped_parameters(scope, entity, parameters) 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']], ), supports_check_mode=True, ) (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 = ForemanEntityAnsibleModule( argument_spec=dict(updated_name=dict(), ), 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 entity and 'updated_name' in entity_dict: entity_dict['name'] = entity_dict.pop('updated_name') 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="{0}"'.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 module.ensure_entity('config_groups', entity_dict, entity) module.exit_json()
def main(): module = ForemanEntityAnsibleModule(foreman_spec=dict( name=dict(aliases=['hostname'], required=True), state=dict(default='state', choices=[ 'on', 'start', 'off', 'stop', 'soft', 'reboot', 'cycle', 'reset', 'state', 'status' ]), ), ) module_params = module.clean_params() with module.api_connection(): # power_status endpoint was only added in foreman 1.22.0 per https://projects.theforeman.org/issues/25436 # Delete this piece when versions below 1.22 are off common use # begin delete if 'power_status' not in module.foremanapi.resource('hosts').actions: params = {'id': module_params['name'], 'power_action': 'status'} power_state = module.resource_action('hosts', 'power', params=params, ignore_check_mode=True) power_state[ 'state'] = 'on' if power_state['power'] == 'running' else 'off' else: # end delete (on delete un-indent the below two lines) params = {'id': module_params['name']} power_state = module.resource_action('hosts', 'power_status', params=params, ignore_check_mode=True) if module.state in ['state', 'status']: module.exit_json(power_state=power_state['state']) elif ( (module.state in ['on', 'start'] and power_state['state'] == 'on') or (module.state in ['off', 'stop'] and power_state['state'] == 'off')): module.exit_json() else: params['power_action'] = module.state module.resource_action('hosts', 'power', params=params)
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(updated_name=dict(), ), entity_spec=dict( name=dict(required=True), admin=dict(required=False, type='bool', default=False), users=dict(required=False, type='entity_list', flat_name='user_ids'), usergroups=dict(required=False, type='entity_list', flat_name='usergroup_ids'), roles=dict(required=False, type='entity_list', flat_name='role_ids'), ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('usergroups', entity_dict['name'], failsafe=True) if not module.desired_absent: if entity and 'updated_name' in entity_dict: entity_dict['name'] = entity_dict.pop('updated_name') if 'roles' in entity_dict: entity_dict['roles'] = module.find_resources_by_name( 'roles', entity_dict['roles'], thin=True) if 'users' in entity_dict: entity_dict['users'] = module.find_resources('users', [ 'login="******"'.format(login) for login in entity_dict['users'] ], thin=True) if 'usergroups' in entity_dict: entity_dict['usergroups'] = module.find_resources_by_name( 'usergroups', entity_dict['usergroups'], thin=True) module.ensure_entity('usergroups', entity_dict, entity) module.exit_json()
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(type='str', required=True), updated_name=dict(type='str'), description=dict(type='str'), provider=dict(type='str', choices=['vmware', 'libvirt', 'ovirt']), provider_params=dict(type='dict'), locations=dict(type='list'), organizations=dict(type='list'), state=dict(type='str', default='present', choices=['present', 'absent', 'present_with_defaults']), # Deprecated provider's specific params, use nested keys in provider_params param instead provider_auth=dict(type='dict'), url=dict(type='str'), display_type=dict(type='str'), datacenter=dict(type='str'), ), required_if=(['state', 'present', ['provider']], ), supports_check_mode=True, ) (entity_dict, state) = module.parse_params() if 'provider' in entity_dict: entity_dict['provider'] = entity_dict['provider'].title() provider_infos = get_provider_infos( provider=entity_dict.get('provider', '')) provider_params = entity_dict.pop('provider_params', dict()) provider_auth = entity_dict.pop('provider_auth', dict()) module.connect() try: # Try to find the compute_resource to work on entity = find_compute_resource(module, name=entity_dict['name'], failsafe=True) except Exception as e: module.fail_json(msg='Failed to find entity: %s ' % e) if 'updated_name' in entity_dict and state == 'present': entity_dict['name'] = entity_dict['updated_name'] 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) # Add provider specific params if state in ['present', 'present_with_defaults']: if not provider_infos and not entity: module.fail_json( msg= 'To create a compute resource a valid provider must be supplied' ) for key in provider_infos['params']: # Manage deprecated params if key in provider_auth: entity_dict[key] = provider_auth[key] if key in provider_params: entity_dict[key] = provider_params[key] changed = naildown_entity_state(provider_infos['class'], entity_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule(entity_spec=dict( name=dict(aliases=['hostname'], required=True), state=dict(default='state', choices=[ 'on', 'start', 'off', 'stop', 'soft', 'reboot', 'cycle', 'reset', 'state', 'status' ]), ), ) entity_dict = module.clean_params() module.connect() params = {'id': entity_dict['name']} _power_state_changed, power_state = module.resource_action('hosts', 'power_status', params=params) if module.state in ['state', 'status']: module.exit_json(changed=False, power_state=power_state['state']) elif ((module.state in ['on', 'start'] and power_state['state'] == 'on') or (module.state in ['off', 'stop'] and power_state['state'] == 'off')): module.exit_json(changed=False) else: params['power_action'] = module.state changed, power_state = module.resource_action('hosts', 'power', params=params) module.exit_json(changed=changed)