def main(): module = ForemanEntityApypieAnsibleModule(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 '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') changed, domain = module.ensure_entity('domains', entity_dict, entity) if domain: scope = {'domain_id': domain['id']} changed |= module.ensure_scoped_parameters(scope, entity, parameters) module.exit_json(changed=changed)
def main(): module = ForemanEntityApypieAnsibleModule( entity_spec=dict( name=dict(), release_name=dict(), description=dict(), family=dict(), major=dict(), minor=dict(), architectures=dict(type='entity_list', flat_name='architecture_ids'), media=dict(type='entity_list', flat_name='medium_ids'), ptables=dict(type='entity_list', flat_name='ptable_ids'), provisioning_templates=dict(type='entity_list', flat_name='provisioning_template_ids'), password_hash=dict(choices=['MD5', 'SHA256', 'SHA512']), parameters=dict(type='nested_list', entity_spec=parameter_entity_spec), ), argument_spec=dict( state=dict(default='present', choices=['present', 'present_with_defaults', 'absent']), ), required_if=[ ['state', 'present', ['name', 'major', 'family']], ['state', 'present_with_defaults', ['name', 'major', 'family']], ], required_one_of=[ ['description', 'name'], ['description', 'major'], ], ) entity_dict = module.clean_params() module.connect() # Try to find the Operating System to work on # name is however not unique, but description is, as well as "<name> <major>[.<minor>]" entity = None # If we have a description, search for it if 'description' in entity_dict and entity_dict['description'] != '': search_string = 'description="{}"'.format(entity_dict['description']) entity = module.find_resource('operatingsystems', search_string, failsafe=True) # If we did not yet find a unique OS, search by name & version # In case of state == absent, those information might be missing, we assume that we did not find an operatingsytem to delete then if entity is None and 'name' in entity_dict and 'major' in entity_dict: search_string = ','.join('{}="{}"'.format(key, entity_dict[key]) for key in ('name', 'major', 'minor') if key in entity_dict) entity = module.find_resource('operatingsystems', search_string, failsafe=True) if not entity and (module.state == 'present' or module.state == 'present_with_defaults'): # we actually attempt to create a new one... for param_name in ['major', 'family', 'password_hash']: if param_name not in entity_dict.keys(): module.fail_json(msg='{} is a required parameter to create a new operating system.'.format(param_name)) if not module.desired_absent: if 'architectures' in entity_dict: entity_dict['architectures'] = module.find_resources_by_name('architectures', entity_dict['architectures'], thin=True) if 'media' in entity_dict: entity_dict['media'] = module.find_resources_by_name('media', entity_dict['media'], thin=True) if 'ptables' in entity_dict: entity_dict['ptables'] = module.find_resources_by_name('ptables', entity_dict['ptables'], thin=True) if 'provisioning_templates' in entity_dict: entity_dict['provisioning_templates'] = module.find_resources_by_name('provisioning_templates', entity_dict['provisioning_templates'], thin=True) parameters = entity_dict.get('parameters') changed, operating_system = module.ensure_entity('operatingsystems', entity_dict, entity) if operating_system: scope = {'operatingsystem_id': operating_system['id']} changed |= module.ensure_scoped_parameters(scope, entity, parameters) module.exit_json(changed=changed)
def main(): module = ForemanEntityApypieAnsibleModule( entity_spec=dict( name=dict(required=True), network_type=dict(choices=['IPv4', 'IPv6'], default='IPv4'), dns_primary=dict(), dns_secondary=dict(), domains=dict(type='entity_list', flat_name='domain_ids'), gateway=dict(), network=dict(required=True), cidr=dict(type='int'), mask=dict(), from_ip=dict(flat_name='from'), to_ip=dict(flat_name='to'), boot_mode=dict(choices=['DHCP', 'Static'], default='DHCP'), ipam=dict(choices=['DHCP', 'Internal DB'], default='DHCP'), dhcp_proxy=dict(type='entity', flat_name='dhcp_id'), tftp_proxy=dict(type='entity', flat_name='tftp_id'), discovery_proxy=dict(type='entity', flat_name='discovery_id'), dns_proxy=dict(type='entity', flat_name='dns_id'), remote_execution_proxies=dict(type='entity_list', flat_name='remote_execution_proxy_ids'), vlanid=dict(type='int'), mtu=dict(type='int'), 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), ), required_one_of=[['cidr', 'mask']], ) entity_dict = module.clean_params() module.connect() entity = module.find_resource_by_name('subnets', entity_dict['name'], failsafe=True) if not module.desired_absent: 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'] = str(IPNetwork('%s/%s' % (entity_dict['network'], entity_dict['cidr'])).netmask) if 'domains' in entity_dict: entity_dict['domains'] = module.find_resources('domains', entity_dict['domains'], thin=True) for feature in ('dhcp_proxy', 'tftp_proxy', 'discovery_proxy', 'dns_proxy'): if feature in entity_dict: entity_dict[feature] = module.find_resource_by_name('smart_proxies', entity_dict[feature], thin=True) if 'remote_execution_proxies' in entity_dict: entity_dict['remote_execution_proxies'] = module.find_resources_by_name('smart_proxies', entity_dict['remote_execution_proxies'], thin=True) 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) parameters = entity_dict.get('parameters') changed, subnet = module.ensure_entity('subnets', entity_dict, entity) if subnet: scope = {'subnet_id': subnet['id']} changed |= module.ensure_scoped_parameters(scope, entity, parameters) module.exit_json(changed=changed)