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(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) module.ensure_entity('compute_attributes', entity_dict, entity) module.exit_json()
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)
def main(): module = ForemanEntityAnsibleModule( entity_spec=dict( name=dict(required=True), locations=dict(type='entity_list', flat_name='location_ids'), organizations=dict(type='entity_list', flat_name='organization_ids'), ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('environments', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title('locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name('organizations', entity_dict['organizations'], thin=True) changed = module.ensure_entity_state('environments', entity_dict, entity) module.exit_json(changed=changed)
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) module.ensure_entity('architectures', 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( 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), puppetclasses=dict(type='entity_list', flat_name='puppetclass_ids'), ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('config_groups', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'puppetclasses' in entity_dict: # puppet classes API return puppet classes grouped by puppet module name puppet_classes = [] for puppet_class in entity_dict['puppetclasses']: search = 'name="{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 changed = module.ensure_entity_state('config_groups', 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( 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() 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': entity_dict['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': entity_dict['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( entity_spec=dict( name=dict(required=True), locations=dict(type='entity_list', flat_name='location_ids'), organizations=dict(type='entity_list', flat_name='organization_ids'), operatingsystems=dict(type='entity_list', flat_name='operatingsystem_ids'), os_family=dict(choices=['AIX', 'Altlinux', 'Archlinux', 'Coreos', 'Debian', 'Freebsd', 'Gentoo', 'Junos', 'NXOS', 'Rancheros', 'Redhat', 'Solaris', 'Suse', 'VRP', 'Windows', 'Xenserver']), path=dict(), ), ) entity_dict = module.clean_params() module.connect() name = entity_dict['name'] affects_multiple = name == '*' # sanitize user input, filter unuseful configuration combinations with 'name: *' if affects_multiple: if module.state == 'present_with_defaults': module.fail_json(msg="'state: present_with_defaults' and 'name: *' cannot be used together") if module.desired_absent: if list(entity_dict.keys()) != ['name']: entity_dict.pop('name', None) module.fail_json(msg='When deleting all installation media, there is no need to specify further parameters: %s ' % entity_dict.keys()) if affects_multiple: entities = module.list_resource('media') if not module.desired_absent: # not 'thin' entities = [module.show_resource('media', entity['id']) for entity in entities] if not entities: # Nothing to do shortcut to exit module.exit_json(changed=False) else: entity = module.find_resource_by_name('media', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'operatingsystems' in entity_dict: entity_dict['operatingsystems'] = module.find_operatingsystems(entity_dict['operatingsystems'], thin=True) if not affects_multiple and len(entity_dict['operatingsystems']) == 1 and 'os_family' not in entity_dict and entity is None: entity_dict['os_family'] = module.show_resource('operatingsystems', entity_dict['operatingsystems'][0]['id'])['family'] if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title('locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name('organizations', entity_dict['organizations'], thin=True) changed = False if not affects_multiple: changed = module.ensure_entity_state('media', entity_dict, entity) else: entity_dict.pop('name') for entity in entities: changed |= module.ensure_entity_state('media', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( entity_spec=dict( name=dict(required=True), host=dict(required=True), port=dict(type='int', default=389), account=dict(), account_password=dict(no_log=True), base_dn=dict(), attr_login=dict(), attr_firstname=dict(), attr_lastname=dict(), attr_mail=dict(), attr_photo=dict(), onthefly_register=dict(type='bool'), usergroup_sync=dict(type='bool'), tls=dict(type='bool'), groups_base=dict(), server_type=dict( choices=["free_ipa", "active_directory", "posix"]), ldap_filter=dict(), locations=dict(type='entity_list', flat_name='location_ids'), organizations=dict(type='entity_list', flat_name='organization_ids'), use_netgroups=dict(type='bool'), ), required_if=[[ 'onthefly_register', True, ['attr_login', 'attr_firstname', 'attr_lastname', 'attr_mail'] ]], ) entity_dict = module.clean_params() # additional parameter checks if 'use_netgroups' in entity_dict and entity_dict[ 'server_type'] == 'active_directory': module.fail_json( msg='use_netgroups cannot be used when server_type=active_directory' ) module.connect() entity = module.find_resource_by_name('auth_source_ldaps', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title( 'locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) module.ensure_entity('auth_source_ldaps', entity_dict, entity) module.exit_json()
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(required=True), hostgroup=dict(type='entity', flat_name='hostgroup_id'), location=dict(type='entity', flat_name='location_id'), organization=dict(type='entity', flat_name='organization_id'), enabled=dict(type='bool'), managed=dict(type='bool'), build=dict(type='bool'), ), required_if=( ['managed', True, ['hostgroup']], ['build', True, ['hostgroup']], ), ) entity_dict = module.clean_params() # additional param validation if '.' not in entity_dict['name']: module.fail_json(msg="The hostname must be FQDN") if not module.desired_absent: if 'hostgroup' not in entity_dict and entity_dict.get('managed', True): module.fail_json(msg='Hostgroup can be omitted only with managed=False') if 'build' in entity_dict and entity_dict['build']: # When 'build'=True, 'managed' has to be True. Assuming that user's priority is to build. if 'managed' in entity_dict and not entity_dict['managed']: module.warn("when 'build'=True, 'managed' is ignored and forced to True") entity_dict['managed'] = True elif 'build' not in entity_dict and 'managed' in entity_dict and not entity_dict['managed']: # When 'build' is not given and 'managed'=False, have to clear 'build' context that might exist on the server. entity_dict['build'] = False module.connect() entity = module.find_resource_by_name('hosts', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'hostgroup' in entity_dict: entity_dict['hostgroup'] = module.find_resource_by_name('hostgroups', entity_dict['hostgroup'], thin=True) if 'location' in entity_dict: entity_dict['location'] = module.find_resource_by_title('locations', entity_dict['location'], thin=True) if 'organization' in entity_dict: entity_dict['organization'] = module.find_resource_by_name('organizations', entity_dict['organization'], thin=True) changed, host = module.ensure_entity('hosts', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( entity_spec=dict( name=dict(required=True), compute_attributes=dict(type='nested_list', entity_spec=compute_attribute_entity_spec), ), argument_spec=dict(updated_name=dict(), ), ) entity_dict = module.clean_params() updated_name = entity_dict.get('updated_name') compute_attributes = entity_dict.pop('compute_attributes', None) module.connect() entity = module.find_resource_by_name('compute_profiles', name=entity_dict['name'], failsafe=True) if module.state == 'present' and updated_name: entity_dict['name'] = updated_name changed, compute_profile = module.ensure_entity('compute_profiles', entity_dict, entity) # Apply changes on underlying compute attributes only when present if module.state == 'present' and compute_attributes is not None: # Update or create compute attributes scope = {'compute_profile_id': compute_profile['id']} for ca_entity_dict in compute_attributes: ca_entity_dict['compute_resource'] = module.find_resource_by_name( 'compute_resources', name=ca_entity_dict['compute_resource'], failsafe=False, thin=False) ca_entities = ca_entity_dict['compute_resource'].get( 'compute_attributes', []) ca_entity = next( (item for item in ca_entities if item.get('compute_profile_id') == compute_profile['id']), None) changed |= module.ensure_entity_state( 'compute_attributes', ca_entity_dict, ca_entity, entity_spec=compute_attribute_entity_spec, params=scope) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( entity_spec=dict( name=dict(required=True), description=dict(), locations=dict(type='entity_list', flat_name='location_ids'), organizations=dict(type='entity_list', flat_name='organization_ids'), filters=dict(type='entity_list', flat_name='filter_ids'), ), ) filters_entity_spec = dict( permissions=dict(type='entity_list', flat_name='permission_ids'), resource=dict(), search=dict(), role_id=dict(required=True) ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('roles', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title('locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name('organizations', entity_dict['organizations'], thin=True) filters = entity_dict.pop("filters", None) changed, entity = module.ensure_entity('roles', entity_dict, entity) if not module.desired_absent and not module.check_mode: if filters is not None: for role_filter in filters: role_filter['role_id'] = entity['id'] role_filter['permissions'] = module.find_resources_by_name('permissions', role_filter['permissions'], thin=True) module.ensure_entity_state('filters', role_filter, None, None, 'present', filters_entity_spec) old_filters = entity.get('filter_ids', []) for old_filter in old_filters: module.ensure_entity('filters', None, {'id': old_filter}, {}, 'absent') if len(old_filters) != len(filters): changed = True module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict(updated_name=dict(), ), entity_spec=dict( name=dict(required=True), description=dict(aliases=['fullname'], flat_name='fullname'), dns_proxy=dict(type='entity', flat_name='dns_id', aliases=['dns']), locations=dict(type='entity_list', flat_name='location_ids'), organizations=dict(type='entity_list', flat_name='organization_ids'), parameters=dict(type='nested_list', entity_spec=parameter_entity_spec), ), ) entity_dict = module.clean_params() module.connect() # Try to find the Domain to work on entity = module.find_resource_by_name('domains', 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 'dns_proxy' in entity_dict: entity_dict['dns_proxy'] = module.find_resource_by_name( 'smart_proxies', entity_dict['dns_proxy'], thin=True) if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title( 'locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) parameters = entity_dict.get('parameters') domain = module.ensure_entity('domains', entity_dict, entity) if domain: scope = {'domain_id': domain['id']} module.ensure_scoped_parameters(scope, entity, parameters) module.exit_json()
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( 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')) changed = module.ensure_entity_state('common_parameters', entity_dict, entity) 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) changed = module.ensure_entity_state('organizations', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule(entity_spec=dict( name=dict(required=True), host=dict(required=True), port=dict(type='int', default=389), account=dict(), account_password=dict(no_log=True), base_dn=dict(), attr_login=dict(), attr_firstname=dict(), attr_lastname=dict(), attr_mail=dict(), attr_photo=dict(), onthefly_register=dict(type='bool'), usergroup_sync=dict(type='bool'), tls=dict(type='bool'), groups_base=dict(), server_type=dict(choices=["free_ipa", "active_directory", "posix"]), ldap_filter=dict(), locations=dict(type='entity_list', flat_name='location_ids'), organizations=dict(type='entity_list', flat_name='organization_ids'), ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('auth_source_ldaps', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title( 'locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) changed = module.ensure_entity_state('auth_source_ldaps', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule(entity_spec=dict( name=dict(required=True), parent=dict(type='entity', flat_name='parent_id'), organizations=dict(type='entity_list', flat_name='organization_ids'), ), ) entity_dict = module.clean_params() module.connect() # Get short name and parent from provided name name, parent = split_fqn(entity_dict['name']) entity_dict['name'] = name if 'parent' in entity_dict: if parent: module.fail_json( msg= "Please specify the parent either separately, or as part of the title." ) parent = entity_dict['parent'] if parent: entity_dict['parent'] = module.find_resource_by_title( 'locations', parent, thin=True, failsafe=module.desired_absent) if module.desired_absent and entity_dict['parent'] is None: # Parent location does not exist so just exit here module.exit_json() if not module.desired_absent: if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) entity = module.find_resource_by_title('locations', build_fqn(name, parent), failsafe=True) module.ensure_entity('locations', entity_dict, entity) 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( entity_spec=dict( audit_comment=dict(), description_format=dict(), job_category=dict(), locations=dict(type='entity_list', flat_name='location_ids'), locked=dict(type='bool', default=False), name=dict(), organizations=dict(type='entity_list', flat_name='organization_ids'), provider_type=dict(), snippet=dict(type='bool'), template=dict(), template_inputs=dict(type='nested_list', entity_spec=template_input_entity_spec), ), argument_spec=dict( file_name=dict(type='path'), state=dict(default='present', choices=['absent', 'present_with_defaults', 'present']), ), 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: *'!") entity_dict = module.clean_params() file_name = entity_dict.pop('file_name', None) if file_name or 'template' in entity_dict: if file_name: parsed_dict = parse_template_from_file(file_name, module) else: parsed_dict = parse_template(entity_dict['template'], module) # sanitize name from template data # The following condition can actually be hit, when someone is trying to import a # template with the name set to '*'. # Besides not being sensible, this would go horribly wrong in this module. if 'name' in parsed_dict and parsed_dict['name'] == '*': module.fail_json(msg="Cannot use '*' as a job template name!") # module params are priorized parsed_dict.update(entity_dict) # make sure certain values are set entity_dict = template_defaults.copy() entity_dict.update(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 module.state == 'present_with_defaults': module.fail_json( msg= "'state: present_with_defaults' and 'name: *' cannot be used together" ) if module.desired_absent: if len(entity_dict.keys()) != 1: module.fail_json( msg= "When deleting all job templates, there is no need to specify further parameters." ) module.connect() if affects_multiple: entities = module.list_resource('job_templates') if not entities: # Nothing to do; shortcut to exit module.exit_json(changed=False) if not module.desired_absent: # not 'thin' entities = [ module.show_resource('job_templates', entity['id']) for entity in entities ] else: entity = module.find_resource_by_name('job_templates', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title( 'locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) # TemplateInputs need to be added as separate entities later template_inputs = entity_dict.get('template_inputs') changed = False if not affects_multiple: changed, job_template = module.ensure_entity('job_templates', entity_dict, entity) update_dependent_entities = (module.state == 'present' or (module.state == 'present_with_defaults' and changed)) if update_dependent_entities and template_inputs is not None: scope = {'template_id': job_template['id']} # Manage TemplateInputs here current_template_input_list = module.list_resource( 'template_inputs', params=scope) current_template_inputs = { item['name']: item for item in current_template_input_list } for template_input_dict in template_inputs: template_input_dict = { key: value for key, value in template_input_dict.items() if value is not None } template_input_entity = current_template_inputs.pop( template_input_dict['name'], None) changed |= module.ensure_entity_state( 'template_inputs', template_input_dict, template_input_entity, params=scope, entity_spec=template_input_entity_spec, ) # At this point, desired template inputs have been removed from the dict. for template_input_entity in current_template_inputs.values(): changed |= module.ensure_entity_state( 'template_inputs', None, template_input_entity, state="absent", params=scope, entity_spec=template_input_entity_spec, ) else: entity_dict.pop('name') for entity in entities: changed |= module.ensure_entity_state('job_templates', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( file_name=dict(type='path'), state=dict(default='present', choices=['absent', 'present_with_defaults', 'present']), ), entity_spec=dict( layout=dict(), locations=dict(type='entity_list', flat_name='location_ids'), locked=dict(type='bool'), name=dict(), organizations=dict(type='entity_list', flat_name='organization_ids'), os_family=dict(choices=[ 'AIX', 'Altlinux', 'Archlinux', 'Debian', 'Freebsd', 'Gentoo', 'Junos', 'Redhat', 'Solaris', 'Suse', 'Windows', ]), ), mutually_exclusive=[ ['file_name', 'layout'], ], required_one_of=[ ['name', 'file_name', 'layout'], ], ) # We do not want a layout text for bulk operations if module.params['name'] == '*': if module.params['file_name'] or module.params['layout']: module.fail_json( msg="Neither file_name nor layout allowed if 'name: *'!") entity_dict = module.clean_params() file_name = entity_dict.pop('file_name', None) if file_name or 'layout' in entity_dict: if file_name: parsed_dict = parse_template_from_file(file_name, module) else: parsed_dict = parse_template(entity_dict['layout'], module) parsed_dict['layout'] = parsed_dict.pop('template') if 'oses' in parsed_dict: parsed_dict['os_family'] = parsed_dict.pop('oses') # sanitize name from template data # The following condition can actually be hit, when someone is trying to import a # template with the name set to '*'. # Besides not being sensible, this would go horribly wrong in this module. if 'name' in parsed_dict and parsed_dict['name'] == '*': module.fail_json(msg="Cannot use '*' as a partition table name!") # module params are priorized parsed_dict.update(entity_dict) entity_dict = parsed_dict # make sure, we have a name if 'name' not in entity_dict: if file_name: entity_dict['name'] = os.path.splitext( os.path.basename(file_name))[0] else: module.fail_json( msg='No name specified and no filename to infer it.') affects_multiple = entity_dict['name'] == '*' # sanitize user input, filter unuseful configuration combinations with 'name: *' if affects_multiple: if module.state == 'present_with_defaults': module.fail_json( msg= "'state: present_with_defaults' and 'name: *' cannot be used together" ) if module.desired_absent: if len(entity_dict.keys()) != 1: module.fail_json( msg= 'When deleting all partition tables, there is no need to specify further parameters.' ) module.connect() if affects_multiple: entities = module.list_resource('ptables') if not entities: # Nothing to do; shortcut to exit module.exit_json(changed=False) if not module.desired_absent: # not 'thin' entities = [ module.show_resource('ptables', entity['id']) for entity in entities ] else: entity = module.find_resource_by_name('ptables', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title( 'locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) changed = False if not affects_multiple: changed = module.ensure_entity_state('ptables', entity_dict, entity) else: entity_dict.pop('name') for entity in entities: changed |= module.ensure_entity_state('ptables', entity_dict, entity) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( updated_name=dict(), ), entity_spec=dict( name=dict(required=True), locations=dict(type='entity_list', flat_name='location_ids'), organizations=dict(type='entity_list', flat_name='organization_ids'), operatingsystems=dict(type='entity_list', flat_name='operatingsystem_ids'), os_family=dict(choices=OS_LIST), path=dict(), ), ) entity_dict = module.clean_params() module.connect() name = entity_dict['name'] affects_multiple = name == '*' # sanitize user input, filter unuseful configuration combinations with 'name: *' if affects_multiple: if module.state == 'present_with_defaults': module.fail_json(msg="'state: present_with_defaults' and 'name: *' cannot be used together") if module.params['updated_name']: module.fail_json(msg="updated_name not allowed if 'name: *'!") if module.desired_absent: if list(entity_dict.keys()) != ['name']: entity_dict.pop('name', None) module.fail_json(msg='When deleting all installation media, there is no need to specify further parameters: %s ' % entity_dict.keys()) if affects_multiple: entities = module.list_resource('media') if not module.desired_absent: # not 'thin' entities = [module.show_resource('media', entity['id']) for entity in entities] if not entities: # Nothing to do shortcut to exit module.exit_json() else: entity = module.find_resource_by_name('media', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if not affects_multiple and 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) if not affects_multiple and len(entity_dict['operatingsystems']) == 1 and 'os_family' not in entity_dict and entity is None: entity_dict['os_family'] = module.show_resource('operatingsystems', entity_dict['operatingsystems'][0]['id'])['family'] if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title('locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name('organizations', entity_dict['organizations'], thin=True) if not affects_multiple: module.ensure_entity('media', entity_dict, entity) else: entity_dict.pop('name') for entity in entities: module.ensure_entity('media', entity_dict, entity) module.exit_json()
def main(): module = ForemanEntityAnsibleModule( entity_spec=dict( name=dict(required=True), description=dict(), parent=dict(type='entity', flat_name='parent_id'), organizations=dict(type='entity_list', flat_name='organization_ids'), locations=dict(type='entity_list', flat_name='location_ids'), compute_resource=dict(type='entity', flat_name='compute_resource_id'), compute_profile=dict(type='entity', flat_name='compute_profile_id'), domain=dict(type='entity', flat_name='domain_id'), subnet=dict(type='entity', flat_name='subnet_id'), subnet6=dict(type='entity', flat_name='subnet6_id'), realm=dict(type='entity', flat_name='realm_id'), architecture=dict(type='entity', flat_name='architecture_id'), operatingsystem=dict(type='entity', flat_name='operatingsystem_id'), medium=dict(aliases=['media'], type='entity', flat_name='medium_id'), ptable=dict(type='entity', flat_name='ptable_id'), pxe_loader=dict(choices=[ 'PXELinux BIOS', 'PXELinux UEFI', 'Grub UEFI', 'Grub2 BIOS', 'Grub2 ELF', 'Grub2 UEFI', 'Grub2 UEFI SecureBoot', 'Grub2 UEFI HTTP', 'Grub2 UEFI HTTPS', 'Grub2 UEFI HTTPS SecureBoot', 'iPXE Embedded', 'iPXE UEFI HTTP', 'iPXE Chain BIOS', 'iPXE Chain UEFI' ]), root_pass=dict(no_log=True), environment=dict(type='entity', flat_name='environment_id'), config_groups=dict(type='entity_list', flat_name='config_group_ids'), puppet_proxy=dict(type='entity', flat_name='puppet_proxy_id'), puppet_ca_proxy=dict(type='entity', flat_name='puppet_ca_proxy_id'), openscap_proxy=dict(type='entity', flat_name='openscap_proxy_id'), parameters=dict(type='nested_list', entity_spec=parameter_entity_spec), content_source=dict(type='entity', flat_name='content_source_id'), lifecycle_environment=dict(type='entity', flat_name='lifecycle_environment_id'), content_view=dict(type='entity', flat_name='content_view_id'), ), argument_spec=dict(organization=dict()), ) entity_dict = module.clean_params() module.connect() # Get short name and parent from provided name name, parent = split_fqn(entity_dict['name']) entity_dict['name'] = name katello_params = [ 'content_source', 'lifecycle_environment', 'content_view' ] if 'organization' not in entity_dict and list( set(katello_params) & set(entity_dict.keys())): module.fail_json( msg="Please specify the organization when using katello parameters." ) if 'parent' in entity_dict: if parent: module.fail_json( msg= "Please specify the parent either separately, or as part of the title." ) parent = entity_dict['parent'] if parent: entity_dict['parent'] = module.find_resource_by_title( 'hostgroups', title=parent, thin=True, failsafe=module.desired_absent) if module.desired_absent and entity_dict['parent'] is None: # Parent hostgroup does not exist so just exit here module.exit_json(changed=False) if not module.desired_absent: if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title( 'locations', entity_dict['locations'], thin=True) if 'compute_resource' in entity_dict: entity_dict['compute_resource'] = module.find_resource_by_name( 'compute_resources', name=entity_dict['compute_resource'], failsafe=False, thin=True) if 'compute_profile' in entity_dict: entity_dict['compute_profile'] = module.find_resource_by_name( 'compute_profiles', name=entity_dict['compute_profile'], failsafe=False, thin=True) if 'domain' in entity_dict: entity_dict['domain'] = module.find_resource_by_name( 'domains', name=entity_dict['domain'], failsafe=False, thin=True) if 'subnet' in entity_dict: entity_dict['subnet'] = module.find_resource_by_name( 'subnets', name=entity_dict['subnet'], failsafe=False, thin=True) if 'subnet6' in entity_dict: entity_dict['subnet6'] = module.find_resource_by_name( 'subnets', name=entity_dict['subnet6'], failsafe=False, thin=True) if 'realm' in entity_dict: entity_dict['realm'] = module.find_resource_by_name( 'realms', name=entity_dict['realm'], failsafe=False, thin=True) if 'architecture' in entity_dict: entity_dict['architecture'] = module.find_resource_by_name( 'architectures', name=entity_dict['architecture'], failsafe=False, thin=True) if 'operatingsystem' in entity_dict: entity_dict['operatingsystem'] = module.find_operatingsystem( entity_dict['operatingsystem'], thin=True) if 'medium' in entity_dict: entity_dict['medium'] = module.find_resource_by_name( 'media', name=entity_dict['medium'], failsafe=False, thin=True) if 'ptable' in entity_dict: entity_dict['ptable'] = module.find_resource_by_name( 'ptables', name=entity_dict['ptable'], failsafe=False, thin=True) if 'environment' in entity_dict: entity_dict['environment'] = module.find_resource_by_name( 'environments', name=entity_dict['environment'], failsafe=False, thin=True) if 'config_groups' in entity_dict: entity_dict['config_groups'] = module.find_resources_by_name( 'config_groups', entity_dict['config_groups'], failsafe=False, thin=True) for proxy in [ 'puppet_proxy', 'puppet_ca_proxy', 'openscap_proxy', 'content_source' ]: if proxy in entity_dict: entity_dict[proxy] = module.find_resource_by_name( 'smart_proxies', entity_dict[proxy], thin=True) if 'organization' in entity_dict: if 'organizations' in entity_dict: if entity_dict['organization'] not in entity_dict[ 'organizations']: entity_dict['organizations'].append( entity_dict['organization']) else: entity_dict['organizations'] = [entity_dict['organization']] entity_dict['organization'] = module.find_resource_by_name( 'organizations', name=entity_dict['organization'], failsafe=False, thin=True) scope = {'organization_id': entity_dict['organization']['id']} if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) if 'lifecycle_environment' in entity_dict: entity_dict[ 'lifecycle_environment'] = module.find_resource_by_name( 'lifecycle_environments', name=entity_dict['lifecycle_environment'], params=scope, failsafe=False, thin=True) if 'content_view' in entity_dict: entity_dict['content_view'] = module.find_resource_by_name( 'content_views', name=entity_dict['content_view'], params=scope, failsafe=False, thin=True) entity = module.find_resource_by_title('hostgroups', title=build_fqn(name, parent), failsafe=True) if entity: entity['root_pass'] = None parameters = entity_dict.get('parameters') changed, hostgroup = module.ensure_entity('hostgroups', entity_dict, entity) if hostgroup: scope = {'hostgroup_id': hostgroup['id']} changed |= module.ensure_scoped_parameters(scope, entity, parameters) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( entity_spec=dict( name=dict(required=True), updated_name=dict(), description=dict(), organizations=dict(type='entity_list', flat_name='organization_ids'), locations=dict(type='entity_list', flat_name='location_ids'), provider=dict(choices=['vmware', 'libvirt', 'ovirt']), display_type=dict(type='invisible'), datacenter=dict(type='invisible'), url=dict(type='invisible'), user=dict(type='invisible'), password=dict(type='invisible'), use_v4=dict(type='invisible'), ovirt_quota=dict(type='invisible'), ), argument_spec=dict( provider_params=dict(type='dict', options=dict( url=dict(), display_type=dict(), user=dict(), password=dict(no_log=True), datacenter=dict(), use_v4=dict(type='bool'), ovirt_quota=dict(), )), state=dict(type='str', default='present', choices=['present', 'absent', 'present_with_defaults']), ), required_if=([ 'state', 'present_with_defaults', ['provider', 'provider_params'] ], ), ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('compute_resources', name=entity_dict['name'], failsafe=True) if not module.desired_absent: if 'updated_name' in entity_dict: entity_dict['name'] = entity_dict['updated_name'] if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title( 'locations', entity_dict['locations'], thin=True) if 'provider' in entity_dict: entity_dict['provider'], provider_param_keys = get_provider_info( provider=entity_dict['provider']) provider_params = { k: v for k, v in entity_dict.pop('provider_params', dict()).items() if v is not None } for key in provider_param_keys: if key in provider_params: entity_dict[key] = provider_params.pop(key) if provider_params: module.fail_json( msg= "Provider {0} does not support the following given parameters: {1}" .format(entity_dict['provider'], list(provider_params.keys()))) # Add provider specific params elif entity is None: module.fail_json( msg= 'To create a compute resource a valid provider must be supplied' ) module.ensure_entity('compute_resources', entity_dict, entity) module.exit_json()
def main(): module = ForemanEntityAnsibleModule(entity_spec=dict( login=dict(required=True, aliases=['name']), firstname=dict(required=False), lastname=dict(required=False), mail=dict(required=False), description=dict(required=False), admin=dict(required=False, type='bool', default=False), user_password=dict(required=False, no_log=True, flat_name='password'), default_location=dict(required=False, type='entity', flat_name='default_location_id'), default_organization=dict(required=False, type='entity', flat_name='default_organization_id'), auth_source=dict(required=False, type='entity', flat_name='auth_source_id'), timezone=dict(required=False, choices=timezone_list), locale=dict(required=False, choices=locale_list), roles=dict(required=False, type='entity_list', flat_name='role_ids'), locations=dict(required=False, type='entity_list', flat_name='location_ids'), organizations=dict(required=False, type='entity_list', flat_name='organization_ids')), ) entity_dict = module.clean_params() module.connect() search = 'login="******"'.format(entity_dict['login']) entity = module.find_resource('users', search, failsafe=True) if not module.desired_absent: if 'mail' not in entity_dict: entity_dict['mail'] = entity['mail'] if 'default_location' in entity_dict: entity_dict['default_location'] = module.find_resource_by_title( 'locations', entity_dict['default_location'], thin=True) if 'default_organization' in entity_dict: entity_dict['default_organization'] = module.find_resource_by_name( 'organizations', entity_dict['default_organization'], thin=True) if 'auth_source' in entity_dict: entity_dict['auth_source'] = module.find_resource_by_name( 'auth_sources', entity_dict['auth_source'], thin=True) if 'roles' in entity_dict: entity_dict['roles'] = module.find_resources_by_name( 'roles', entity_dict['roles'], thin=True) if 'locations' in entity_dict: entity_dict['locations'] = module.find_resources_by_title( 'locations', entity_dict['locations'], thin=True) if 'organizations' in entity_dict: entity_dict['organizations'] = module.find_resources_by_name( 'organizations', entity_dict['organizations'], thin=True) module.ensure_entity('users', entity_dict, entity) module.exit_json(entity_dict=entity_dict)