def main(): argument_spec = dict( description=dict(type='str'), resource_type=dict(type='str', choices=[ 'Vm', 'ContainerNode', 'MiqServer', 'Host', 'Storage', 'EmsCluster', 'ExtManagementSystem', 'MiddlewareServer' ]), expression_type=dict(type='str', default='hash', choices=['miq', 'hash']), expression=dict(type='dict'), options=dict(type='dict'), enabled=dict(type='bool'), state=dict(required=False, default='present', choices=['present', 'absent']), ) # add the manageiq connection arguments to the arguments argument_spec.update(manageiq_argument_spec()) module = AnsibleModule(argument_spec=argument_spec, required_if=[('state', 'present', [ 'description', 'resource_type', 'expression', 'enabled', 'options' ]), ('state', 'absent', ['description'])]) state = module.params['state'] description = module.params['description'] manageiq = ManageIQ(module) manageiq_alerts = ManageIQAlerts(manageiq) existing_alert = manageiq.find_collection_resource_by( "alert_definitions", description=description) # we need to add or update the alert if state == "present": alert = manageiq_alerts.create_alert_dict(module.params) if not existing_alert: # an alert with this description doesn't exist yet, let's create it res_args = manageiq_alerts.add_alert(alert) else: # an alert with this description exists, we might need to update it res_args = manageiq_alerts.update_alert(existing_alert, alert) # this alert should not exist elif state == "absent": # if we have an alert with this description, delete it if existing_alert: res_args = manageiq_alerts.delete_alert(existing_alert) else: # it doesn't exist, and that's okay msg = "Alert '{description}' does not exist in ManageIQ" msg = msg.format(description=description) res_args = dict(changed=False, msg=msg) module.exit_json(**res_args)
def main(): argument_spec = dict( name=dict(type='str'), resource_type=dict(type='str', choices=[ 'Vm', 'ContainerNode', 'MiqServer', 'Host', 'Storage', 'EmsCluster', 'ExtManagementSystem', 'MiddlewareServer' ]), alerts=dict(type='list'), notes=dict(type='str'), state=dict(default='present', choices=['present', 'absent']), ) # add the manageiq connection arguments to the arguments argument_spec.update(manageiq_argument_spec()) module = AnsibleModule(argument_spec=argument_spec, required_if=[('state', 'present', ['name', 'resource_type']), ('state', 'absent', ['name'])]) state = module.params['state'] name = module.params['name'] manageiq = ManageIQ(module) manageiq_alert_profiles = ManageIQAlertProfiles(manageiq) existing_profile = manageiq.find_collection_resource_by( "alert_definition_profiles", name=name) # we need to add or update the alert profile if state == "present": if not existing_profile: # a profile with this name doesn't exist yet, let's create it res_args = manageiq_alert_profiles.add_profile(module.params) else: # a profile with this name exists, we might need to update it res_args = manageiq_alert_profiles.update_profile( existing_profile, module.params) # this alert profile should not exist if state == "absent": # if we have an alert profile with this name, delete it if existing_profile: res_args = manageiq_alert_profiles.delete_profile(existing_profile) else: # This alert profile does not exist in ManageIQ, and that's okay msg = "Alert profile '{name}' does not exist in ManageIQ" msg = msg.format(name=name) res_args = dict(changed=False, msg=msg) module.exit_json(**res_args)
def main(): argument_spec = dict( name=dict(type='str'), resource_type=dict(type='str', choices=['Vm', 'ContainerNode', 'MiqServer', 'Host', 'Storage', 'EmsCluster', 'ExtManagementSystem', 'MiddlewareServer']), alerts=dict(type='list'), notes=dict(type='str'), state=dict(default='present', choices=['present', 'absent']), ) # add the manageiq connection arguments to the arguments argument_spec.update(manageiq_argument_spec()) module = AnsibleModule(argument_spec=argument_spec, required_if=[('state', 'present', ['name', 'resource_type']), ('state', 'absent', ['name'])]) state = module.params['state'] name = module.params['name'] manageiq = ManageIQ(module) manageiq_alert_profiles = ManageIQAlertProfiles(manageiq) existing_profile = manageiq.find_collection_resource_by("alert_definition_profiles", name=name) # we need to add or update the alert profile if state == "present": if not existing_profile: # a profile with this name doesn't exist yet, let's create it res_args = manageiq_alert_profiles.add_profile(module.params) else: # a profile with this name exists, we might need to update it res_args = manageiq_alert_profiles.update_profile(existing_profile, module.params) # this alert profile should not exist if state == "absent": # if we have an alert profile with this name, delete it if existing_profile: res_args = manageiq_alert_profiles.delete_profile(existing_profile) else: # This alert profile does not exist in ManageIQ, and that's okay msg = "Alert profile '{name}' does not exist in ManageIQ" msg = msg.format(name=name) res_args = dict(changed=False, msg=msg) module.exit_json(**res_args)
def main(): actions = {'present': 'assign', 'absent': 'unassign', 'list': 'list'} argument_spec = dict( policy_profiles=dict(type='list'), resource_name=dict(required=True, type='str'), resource_type=dict(required=True, type='str', choices=manageiq_entities().keys()), state=dict(required=False, type='str', choices=['present', 'absent', 'list'], default='present'), ) # add the manageiq connection arguments to the arguments argument_spec.update(manageiq_argument_spec()) module = AnsibleModule( argument_spec=argument_spec, required_if=[('state', 'present', ['policy_profiles']), ('state', 'absent', ['policy_profiles'])], ) policy_profiles = module.params['policy_profiles'] resource_type_key = module.params['resource_type'] resource_name = module.params['resource_name'] state = module.params['state'] # get the action and resource type action = actions[state] resource_type = manageiq_entities()[resource_type_key] manageiq = ManageIQ(module) # query resource id, fail if resource does not exist resource_id = manageiq.find_collection_resource_or_fail( resource_type, name=resource_name)['id'] manageiq_policies = ManageIQPolicies(manageiq, resource_type, resource_id) if action == 'list': # return a list of current profiles for this object current_profiles = manageiq_policies.query_resource_profiles() res_args = dict(changed=False, profiles=current_profiles) else: # assign or unassign the profiles res_args = manageiq_policies.assign_or_unassign_profiles( policy_profiles, action) module.exit_json(**res_args)
def main(): actions = {'present': 'assign', 'absent': 'unassign', 'list': 'list'} argument_spec = dict( policy_profiles=dict(type='list'), resource_name=dict(required=True, type='str'), resource_type=dict(required=True, type='str', choices=manageiq_entities().keys()), state=dict(required=False, type='str', choices=['present', 'absent', 'list'], default='present'), ) # add the manageiq connection arguments to the arguments argument_spec.update(manageiq_argument_spec()) module = AnsibleModule( argument_spec=argument_spec, required_if=[ ('state', 'present', ['policy_profiles']), ('state', 'absent', ['policy_profiles']) ], ) policy_profiles = module.params['policy_profiles'] resource_type_key = module.params['resource_type'] resource_name = module.params['resource_name'] state = module.params['state'] # get the action and resource type action = actions[state] resource_type = manageiq_entities()[resource_type_key] manageiq = ManageIQ(module) # query resource id, fail if resource does not exist resource_id = manageiq.find_collection_resource_or_fail(resource_type, name=resource_name)['id'] manageiq_policies = ManageIQPolicies(manageiq, resource_type, resource_id) if action == 'list': # return a list of current profiles for this object current_profiles = manageiq_policies.query_resource_profiles() res_args = dict(changed=False, profiles=current_profiles) else: # assign or unassign the profiles res_args = manageiq_policies.assign_or_unassign_profiles(policy_profiles, action) module.exit_json(**res_args)
def main(): argument_spec = dict( userid=dict(required=True, type='str'), name=dict(), password=dict(no_log=True), group=dict(), email=dict(), state=dict(choices=['absent', 'present'], default='present'), update_password=dict(choices=['always', 'on_create'], default='always'), ) # add the manageiq connection arguments to the arguments argument_spec.update(manageiq_argument_spec()) module = AnsibleModule( argument_spec=argument_spec, ) userid = module.params['userid'] name = module.params['name'] password = module.params['password'] group = module.params['group'] email = module.params['email'] state = module.params['state'] manageiq = ManageIQ(module) manageiq_user = ManageIQUser(manageiq) user = manageiq_user.user(userid) # user should not exist if state == "absent": # if we have a user, delete it if user: res_args = manageiq_user.delete_user(user) # if we do not have a user, nothing to do else: res_args = dict( changed=False, msg="user %s: does not exist in manageiq" % (userid)) # user shoult exist if state == "present": # if we have a user, edit it if user: res_args = manageiq_user.edit_user(user, name, group, password, email) # if we do not have a user, create it else: res_args = manageiq_user.create_user(userid, name, group, password, email) module.exit_json(**res_args)
def main(): actions = {'present': 'assign', 'absent': 'unassign', 'list': 'list'} module = AnsibleModule( argument_spec=dict( manageiq_connection=dict(required=True, type='dict', options=manageiq_argument_spec()), tags=dict(type='list'), resource_name=dict(required=True, type='str'), resource_type=dict(required=True, type='str', choices=manageiq_entities().keys()), state=dict(required=False, type='str', choices=['present', 'absent', 'list'], default='present'), ), required_if=[('state', 'present', ['tags']), ('state', 'absent', ['tags'])], ) tags = module.params['tags'] resource_type_key = module.params['resource_type'] resource_name = module.params['resource_name'] state = module.params['state'] # get the action and resource type action = actions[state] resource_type = manageiq_entities()[resource_type_key] manageiq = ManageIQ(module) # query resource id, fail if resource does not exist resource_id = query_resource_id(manageiq, resource_type, resource_name) manageiq_tags = ManageIQTags(manageiq, resource_type, resource_id) if action == 'list': # return a list of current tags for this object current_tags = manageiq_tags.query_resource_tags() res_args = dict(changed=False, tags=current_tags) else: # assign or unassign the tags res_args = manageiq_tags.assign_or_unassign_tags(tags, action) module.exit_json(**res_args)
def main(): # initialize arguments argument_spec = dict(request_id=dict(type='str', required=True), message=dict(type='str', required=True), manageiq_connection=dict(type='dict', required=True)) # add the manageiq connection arguments to the arguments argument_spec.update(manageiq_argument_spec()) module = AnsibleModule(argument_spec=argument_spec) request_id = module.params['request_id'] message = module.params['message'] manageiq = ManageIQ(module) manageiq_request_message = ManageIQRequestMessage(manageiq) res_args = manageiq_request_message.update(request_id, message) module.exit_json(**res_args)
def main(): argument_spec = dict( description=dict(type='str'), resource_type=dict(type='str', choices=['Vm', 'ContainerNode', 'MiqServer', 'Host', 'Storage', 'EmsCluster', 'ExtManagementSystem', 'MiddlewareServer']), expression_type=dict(type='str', default='hash', choices=['miq', 'hash']), expression=dict(type='dict'), options=dict(type='dict'), enabled=dict(type='bool'), state=dict(require=False, default='present', choices=['present', 'absent']), ) # add the manageiq connection arguments to the arguments argument_spec.update(manageiq_argument_spec()) module = AnsibleModule(argument_spec=argument_spec, required_if=[('state', 'present', ['description', 'resource_type', 'expression', 'enabled', 'options']), ('state', 'absent', ['description'])]) state = module.params['state'] description = module.params['description'] manageiq = ManageIQ(module) manageiq_alerts = ManageIQAlerts(manageiq) existing_alert = manageiq.find_collection_resource_by("alert_definitions", description=description) # we need to add or update the alert if state == "present": alert = manageiq_alerts.create_alert_dict(module.params) if not existing_alert: # an alert with this description doesn't exist yet, let's create it res_args = manageiq_alerts.add_alert(alert) else: # an alert with this description exists, we might need to update it res_args = manageiq_alerts.update_alert(existing_alert, alert) # this alert should not exist elif state == "absent": # if we have an alert with this description, delete it if existing_alert: res_args = manageiq_alerts.delete_alert(existing_alert) else: # it doesn't exist, and that's okay msg = "Alert '{description}' does not exist in ManageIQ" msg = msg.format(description=description) res_args = dict(changed=False, msg=msg) module.exit_json(**res_args)
def main(): argument_spec = dict( description=dict(required=True, type='str'), state=dict(choices=['absent', 'present'], default='present'), role_id=dict(required=False, type='int'), role=dict(required=False, type='str'), tenant_id=dict(required=False, type='int'), tenant=dict(required=False, type='str'), managed_filters=dict(required=False, type='dict'), managed_filters_merge_mode=dict(required=False, choices=['merge', 'replace'], default='replace'), belongsto_filters=dict(required=False, type='list', elements='str'), belongsto_filters_merge_mode=dict(required=False, choices=['merge', 'replace'], default='replace'), ) # add the manageiq connection arguments to the arguments argument_spec.update(manageiq_argument_spec()) module = AnsibleModule( argument_spec=argument_spec ) description = module.params['description'] state = module.params['state'] role_id = module.params['role_id'] role_name = module.params['role'] tenant_id = module.params['tenant_id'] tenant_name = module.params['tenant'] managed_filters = module.params['managed_filters'] managed_filters_merge_mode = module.params['managed_filters_merge_mode'] belongsto_filters = module.params['belongsto_filters'] belongsto_filters_merge_mode = module.params['belongsto_filters_merge_mode'] manageiq = ManageIQ(module) manageiq_group = ManageIQgroup(manageiq) group = manageiq_group.group(description) # group should not exist if state == "absent": # if we have a group, delete it if group: res_args = manageiq_group.delete_group(group) # if we do not have a group, nothing to do else: res_args = dict( changed=False, msg="group %s: does not exist in manageiq" % description) # group should exist if state == "present": tenant = manageiq_group.tenant(tenant_id, tenant_name) role = manageiq_group.role(role_id, role_name) norm_managed_filters = manageiq_group.normalize_user_managed_filters_to_sorted_dict(managed_filters, module) # if we have a group, edit it if group: res_args = manageiq_group.edit_group(group, description, role, tenant, norm_managed_filters, managed_filters_merge_mode, belongsto_filters, belongsto_filters_merge_mode) # if we do not have a group, create it else: res_args = manageiq_group.create_group(description, role, tenant, norm_managed_filters, belongsto_filters) group = manageiq.client.get_entity('groups', res_args['group_id']) group.reload(expand='resources', attributes=['miq_user_role_name', 'tenant', 'entitlement']) res_args['group'] = manageiq_group.create_result_group(group) module.exit_json(**res_args)
def main(): argument_spec = dict(name=dict(required=True, type='str'), description=dict(required=True, type='str'), parent_id=dict(required=False, type='int'), parent=dict(required=False, type='str'), state=dict(choices=['absent', 'present'], default='present'), quotas=dict(type='dict', default={})) # add the manageiq connection arguments to the arguments argument_spec.update(manageiq_argument_spec()) module = AnsibleModule(argument_spec=argument_spec) name = module.params['name'] description = module.params['description'] parent_id = module.params['parent_id'] parent = module.params['parent'] state = module.params['state'] quotas = module.params['quotas'] manageiq = ManageIQ(module) manageiq_tenant = ManageIQTenant(manageiq) parent_tenant, tenant = manageiq_tenant.tenant(name, parent_id, parent) # tenant should not exist if state == "absent": # if we have a tenant, delete it if tenant: res_args = manageiq_tenant.delete_tenant(tenant) # if we do not have a tenant, nothing to do else: if parent_id: msg = "tenant '%s' with parent_id %i does not exist in manageiq" % ( name, parent_id) else: msg = "tenant '%s' with parent '%s' does not exist in manageiq" % ( name, parent) res_args = dict(changed=False, msg=msg) # tenant should exist if state == "present": # if we have a tenant, edit it if tenant: res_args = manageiq_tenant.edit_tenant(tenant, name, description) # if we do not have a tenant, create it else: res_args = manageiq_tenant.create_tenant(name, description, parent_tenant) tenant = manageiq.client.get_entity('tenants', res_args['tenant_id']) # quotas as supplied and we have a tenant if quotas: tenant_quotas_res = manageiq_tenant.update_tenant_quotas( tenant, quotas) if tenant_quotas_res['changed']: res_args['changed'] = True res_args['tenant_quotas_msg'] = tenant_quotas_res['msg'] tenant.reload(expand='resources', attributes=['tenant_quotas']) res_args['tenant'] = manageiq_tenant.create_tenant_response( tenant, parent_tenant) module.exit_json(**res_args)
def main(): zone_id = None endpoints = [] argument_spec = dict( state=dict(choices=['absent', 'present', 'refresh'], default='present'), name=dict(required=True), zone=dict(default='default'), provider_region=dict(), host_default_vnc_port_start=dict(), host_default_vnc_port_end=dict(), subscription=dict(), project=dict(), azure_tenant_id=dict(aliases=['keystone_v3_domain_id']), tenant_mapping_enabled=dict(default=False, type='bool'), api_version=dict(choices=['v2', 'v3']), type=dict(choices=supported_providers().keys()), ) # add the manageiq connection arguments to the arguments argument_spec.update(manageiq_argument_spec()) # add the endpoint arguments to the arguments argument_spec.update(endpoint_list_spec()) module = AnsibleModule( argument_spec=argument_spec, required_if=[('state', 'present', ['provider']), ('state', 'refresh', ['name'])], required_together=[[ 'host_default_vnc_port_start', 'host_default_vnc_port_end' ]], ) name = module.params['name'] zone_name = module.params['zone'] provider_type = module.params['type'] raw_endpoints = module.params provider_region = module.params['provider_region'] host_default_vnc_port_start = module.params['host_default_vnc_port_start'] host_default_vnc_port_end = module.params['host_default_vnc_port_end'] subscription = module.params['subscription'] uid_ems = module.params['azure_tenant_id'] project = module.params['project'] tenant_mapping_enabled = module.params['tenant_mapping_enabled'] api_version = module.params['api_version'] state = module.params['state'] manageiq = ManageIQ(module) manageiq_provider = ManageIQProvider(manageiq) provider = manageiq_provider.provider(name) # provider should not exist if state == "absent": # if we have a provider, delete it if provider: res_args = manageiq_provider.delete_provider(provider) # if we do not have a provider, nothing to do else: res_args = dict(changed=False, msg="provider %s: does not exist in manageiq" % (name)) # provider should exist if state == "present": # get data user did not explicitly give if zone_name: zone_id = manageiq_provider.zone_id(zone_name) # if we do not have a provider_type, use the current provider_type if provider and not provider_type: provider_type = manageiq_provider.class_name_to_type( provider['type']) # check supported_providers types if not provider_type: manageiq_provider.module.fail_json( msg="missing required argument: provider_type") # check supported_providers types if provider_type not in supported_providers().keys(): manageiq_provider.module.fail_json( msg="provider_type %s is not supported" % (provider_type)) # build "connection_configurations" objects from user requested endpoints # "provider" is a required endpoint, if we have it, we have endpoints if raw_endpoints.get("provider"): endpoints = manageiq_provider.build_connection_configurations( provider_type, raw_endpoints) # if we have a provider, edit it if provider: res_args = manageiq_provider.edit_provider( provider, name, provider_type, endpoints, zone_id, provider_region, host_default_vnc_port_start, host_default_vnc_port_end, subscription, project, uid_ems, tenant_mapping_enabled, api_version) # if we do not have a provider, create it else: res_args = manageiq_provider.create_provider( name, provider_type, endpoints, zone_id, provider_region, host_default_vnc_port_start, host_default_vnc_port_end, subscription, project, uid_ems, tenant_mapping_enabled, api_version) # refresh provider (trigger sync) if state == "refresh": if provider: res_args = manageiq_provider.refresh(provider, name) else: res_args = dict(changed=False, msg="provider %s: does not exist in manageiq" % (name)) module.exit_json(**res_args)
def main(): zone_id = None endpoints = [] module = AnsibleModule( argument_spec=dict( manageiq_connection=dict(required=True, type='dict', options=manageiq_argument_spec()), state=dict(choices=['absent', 'present'], default='present'), name=dict(required=True), zone=dict(default='default'), provider_region=dict(), type=dict(choices=supported_providers().keys()), endpoints=dict(type='dict', options=endpoint_list_spec()), ), required_if=[ ('state', 'present', ['endpoints'])], ) name = module.params['name'] zone_name = module.params['zone'] provider_type = module.params['type'] raw_endpoints = module.params['endpoints'] provider_region = module.params['provider_region'] state = module.params['state'] manageiq = ManageIQ(module) manageiq_provider = ManageIQProvider(manageiq) provider = manageiq_provider.provider(name) # provider should not exist if state == "absent": # if we have a provider, delete it if provider: res_args = manageiq_provider.delete_provider(provider) # if we do not have a provider, nothing to do else: res_args = dict( changed=False, msg="provider %s: does not exist in manageiq" % (name)) # provider should exist if state == "present": # get data user did not explicitly give if zone_name: zone_id = manageiq_provider.zone_id(zone_name) # if we do not have a provider_type, use the current provider_type if provider and not provider_type: provider_type = manageiq_provider.class_name_to_type(provider['type']) # check supported_providers types if not provider_type: manageiq_provider.module.fail_json( msg="missing required argument: provider_type") # check supported_providers types if provider_type not in supported_providers().keys(): manageiq_provider.module.fail_json( msg="provider_type %s is not supported" % (provider_type)) # build "connection_configurations" objects from user requsted endpoints if raw_endpoints: endpoints = manageiq_provider.build_connection_configurations(provider_type, raw_endpoints) # if we have a provider, edit it if provider: res_args = manageiq_provider.edit_provider(provider, name, provider_type, endpoints, zone_id, provider_region) # if we do not have a provider, create it else: res_args = manageiq_provider.create_provider(name, provider_type, endpoints, zone_id, provider_region) module.exit_json(**res_args)