def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(type=dict(type='str', required=False, default='all'),
                         )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(
        changed=False,
        original_message='',
        message=''
    )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True,
                           )
    vmanage = Vmanage(module)
    vmanage_local_policy = LocalPolicy(vmanage.auth, vmanage.host)

    vmanage.result['local_policy'] = vmanage_local_policy.get_local_policy_dict()

    vmanage.exit_json(**vmanage.result)
def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(
        file=dict(type='str', required=True),
        update=dict(type='bool', required=False, default=False),
        type=dict(type='str',
                  required=False,
                  choices=['feature', 'device'],
                  default=None),
        name_list=dict(type='list', required=False, default=[]),
    )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, original_message='', message='')

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )
    vmanage = Vmanage(module)
    vmanage_files = Files(vmanage.auth, vmanage.host)
    vmanage_files.export_attachments_to_file(
        vmanage.params['file'], name_list=vmanage.params['name_list'])

    vmanage.exit_json(**vmanage.result)
def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(organization=dict(type='str'),
                         vbond=dict(type='str'),
                         vbond_port=dict(type='int', default=12346),
                         root_cert=dict(type='str'),
                         push=dict(type='bool'))

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )
    vmanage = Vmanage(module)
    vmanage_certificate = Certificate(vmanage.auth, vmanage.host)

    vmanage.result['what_changed'] = []

    if vmanage.params['push']:
        vmanage_certificate.push_certificates()

    if vmanage.result['what_changed']:
        vmanage.result['changed'] = True

    vmanage.exit_json(**vmanage.result)
Esempio n. 4
0
def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(state=dict(type='str',
                                    choices=['absent', 'present'],
                                    default='present'),
                         name=dict(type='str', alias='templateName'),
                         description=dict(type='str',
                                          alias='templateDescription'),
                         templates=dict(type='str', alias='generalTemplates'),
                         device_type=dict(type='list', alias='deviceType'),
                         config_type=dict(type='list', alias='configType'),
                         update=dict(type='bool', defaut=True),
                         factory_default=dict(type='bool',
                                              alias='factoryDefault'),
                         aggregate=dict(type='list'),
                         push=dict(type='bool', default=False))

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )
    vmanage = Vmanage(module)
    vmanage_device_templates = DeviceTemplates(vmanage.auth, vmanage.host)
    vmanage_template_data = TemplateData(vmanage.auth, vmanage.host)

    # Always as an aggregate... make a list if just given a single entry
    if vmanage.params['aggregate']:
        device_template_list = vmanage.params['aggregate']
    else:
        if vmanage.params['state'] == 'present':
            device_template_list = [{
                'templateName':
                vmanage.params['name'],
                'templateDescription':
                vmanage.params['description'],
                'deviceType':
                vmanage.params['device_type'],
                'configType':
                vmanage.params['config_type'],
                'factoryDefault':
                vmanage.params['factory_default'],
                'generalTemplates':
                vmanage.params['templates'],
            }]
        else:
            device_template_list = [{
                'templateName': vmanage.params['name'],
                'state': 'absent'
            }]

    device_template_updates = []
    if vmanage.params['state'] == 'present':
        device_template_updates = vmanage_template_data.import_device_template_list(
            device_template_list,
            check_mode=module.check_mode,
            update=vmanage.params['update'],
            push=vmanage.params['push'])
        if device_template_updates:
            vmanage.result['changed'] = True
    else:
        device_template_dict = vmanage_device_templates.get_device_template_dict(
            factory_default=True, remove_key=False)
        for device_template in device_template_list:
            if device_template['templateName'] in device_template_dict:
                if not module.check_mode:
                    vmanage_device_templates.delete_device_template(
                        device_template_dict[
                            device_template['templateName']]['templateId'])
                vmanage.result['changed'] = True

    vmanage.result['updates'] = device_template_updates
    vmanage.exit_json(**vmanage.result)
def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(
        device=dict(type='str', aliases=['device', 'host-name']),
        gather_subset=dict(type='list', options=subset_options),
    )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, original_message='', message='')

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )
    vmanage = Vmanage(module)
    vmanage_device = Device(vmanage.auth, vmanage.host)
    vmanage_monitor = MonitorNetwork(vmanage.auth, vmanage.host)

    if vmanage.params['device']:
        # If we are passed in a device, we return specific facts about that device

        device_facts = {}
        # Check to see if we were passed in a device IP address or a device name
        try:
            system_ip = ipaddress.ip_address(vmanage.params['device'])
            device_status = vmanage_device.get_device_status(system_ip)
        except ValueError:
            device_status = vmanage_device.get_device_status(
                vmanage.params['device'], key='host-name')
            system_ip = device_status['system-ip']

        if device_status:
            if vmanage.params['gather_subset']:
                requested_subsets = vmanage.params['gather_subset']
            else:
                requested_subsets = subset_options
            device_facts['status'] = device_status
            if 'config' in requested_subsets:
                device_facts['config'] = vmanage_device.get_device_config(
                    device_status['device-type'], system_ip)
            if 'omp' in requested_subsets:
                omp_routes_received = vmanage_monitor.get_omp_routes_received(
                    system_ip)
                omp_routes_advertised = vmanage_monitor.get_omp_routes_advertised(
                    system_ip)
                omp_routes = {
                    'received': omp_routes_received,
                    'advertised': omp_routes_advertised,
                }
                omp = {'routes': omp_routes}
                device_facts['omp'] = omp
            if 'control' in requested_subsets:
                control_connections = vmanage_monitor.get_control_connections(
                    system_ip)
                control_connections_history = vmanage_monitor.get_control_connections_history(
                    system_ip)
                control = {
                    'connections': control_connections,
                    'connections_history': control_connections_history
                }
                device_facts['control'] = control
        vmanage.result['device_facts'] = device_facts
    else:
        if vmanage.params['gather_subset']:
            vmanage.fail_json(
                msg=
                "gather_subset argument can only be secified with device argument",
                **vmanage.result)
        # Otherwise, we return facts for all devices sorted by device type
        vmanage.result['vedges'] = vmanage_device.get_device_config_list(
            'vedges')
        vmanage.result['controllers'] = vmanage_device.get_device_config_list(
            'controllers')

    vmanage.exit_json(**vmanage.result)
Esempio n. 6
0
def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(organization=dict(type='str'),
                         vbond=dict(type='str'),
                         vbond_port=dict(type='str', default='12346'),
                         root_cert=dict(type='str'),
                         ca_type=dict(type='str'))

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )
    vmanage = Vmanage(module)
    vmanage_settings = Settings(vmanage.auth, vmanage.host)
    vmanage_certificate = Certificate(vmanage.auth, vmanage.host)
    vmanage.result['what_changed'] = []

    if vmanage.params['organization']:
        current = vmanage_settings.get_vmanage_org()
        if vmanage.params['organization'] != current:
            vmanage.result['what_changed'].append('organization')
            if not module.check_mode:
                vmanage_settings.set_vmanage_org(
                    vmanage.params['organization'])

    if vmanage.params['vbond']:
        current = vmanage_settings.get_vmanage_vbond()
        if vmanage.params['vbond'] != current['domainIp'] or vmanage.params[
                'vbond_port'] != current['port']:
            vmanage.result['what_changed'].append('vbond')
            if not module.check_mode:
                vmanage_settings.set_vmanage_vbond(
                    vmanage.params['vbond'], vmanage.params['vbond_port'])

    if vmanage.params['ca_type']:
        current = vmanage_settings.get_vmanage_ca_type()
        if vmanage.params['ca_type'] != current:
            vmanage.result['what_changed'].append('ca_type')
            if not module.check_mode:
                vmanage_settings.set_vmanage_ca_type(vmanage.params['ca_type'])

    if vmanage.params['root_cert']:
        current = vmanage_certificate.get_vmanage_root_cert()
        if vmanage.params['root_cert'] not in current:
            vmanage.result['what_changed'].append('root_cert')
            if not module.check_mode:
                vmanage_settings.set_vmanage_root_cert(
                    vmanage.params['root_cert'])

    if vmanage.result['what_changed']:
        vmanage.result['changed'] = True

    vmanage.exit_json(**vmanage.result)
def run_module():
    # define available arguments/parameters a user can pass to the module
    # we only support the 'data' type currently, it will be fixed when we migrate to the SDK
    argument_spec = vmanage_argument_spec()
    argument_spec.update(
        state=dict(type='str',
                   choices=['absent', 'present'],
                   default='present'),
        aggregate=dict(type='list'),
        name=dict(type='str'),
        description=dict(type='str'),
        type=dict(type='str', required=False, choices=['data']),
        sequences=dict(type='list'),
        default_action=dict(type='dict', alias='defaultAction'),
        update=dict(type='bool', default=False),
        push=dict(type='bool', default=False),
    )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )
    vmanage = Vmanage(module)
    vmanage_policy_definitions = PolicyDefinitions(vmanage.auth, vmanage.host)

    # Always as an aggregate... make a definition if just given a single entry
    if vmanage.params['aggregate']:
        policy_definition_list = vmanage.params['aggregate']
    else:
        policy_definition_list = [{
            "name":
            vmanage.params['name'],
            "description":
            vmanage.params['description'],
            "type":
            vmanage.params['type'],
            "sequences":
            vmanage.params['sequences'],
            "defaultAction":
            vmanage.params['default_action']
        }]

    # Import site lists
    policy_definition_updates = []
    if vmanage.params['state'] == 'present':
        policy_list_updates = vmanage_policy_definitions.import_policy_definition_list(
            policy_definition_list,
            check_mode=module.check_mode,
            update=vmanage.params['update'],
            push=vmanage.params['push'])
        if policy_list_updates:
            vmanage.result['changed'] = True
    else:
        policy_definition_dict = vmanage_policy_definitions.get_policy_definition_dict(
            vmanage.params['type'], remove_key=False)
        for policy_definition in policy_definition_list:
            if policy_definition['name'] in policy_definition_dict:
                diff = list(dictdiffer.diff({}, policy_definition))
                policy_definition_updates.append({
                    'name':
                    policy_definition['name'],
                    'diff':
                    diff
                })
                if not module.check_mode:
                    vmanage_policy_definitions.delete_policy_definition(
                        policy_definition['type'].lower(),
                        policy_definition['listId'])
                vmanage.result['changed'] = True

    vmanage.params['updates'] = policy_definition_updates
    vmanage.exit_json(**vmanage.result)
def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(state=dict(type='str', choices=['absent', 'present', 'activated', 'deactivated'], default='present'),
                         name=dict(type='str', alias='policyName'),
                         description=dict(type='str', alias='policyDescription'),
                         definition=dict(type='str', alias='policyDefinition'),
                         type=dict(type='list', alias='policyType'),
                         wait=dict(type='bool', default=False),
                         update=dict(type='bool', default=False),
                         push=dict(type='bool', default=False),
                         aggregate=dict(type='list'),
                         )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(
        changed=False,
    )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=True,
                           )
    vmanage = Vmanage(module)
    vmanage_central_policy = CentralPolicy(vmanage.auth, vmanage.host)

    # Always as an aggregate... make a list if just given a single entry
    if vmanage.params['aggregate']:
        policy_list = vmanage.params['aggregate']
    else:
        if vmanage.params['state'] == 'present':
            policy_list = [
                {
                    'policyName': vmanage.params['name'],
                    'policyDescription': vmanage.params['description'],
                    'policyType': vmanage.params['type'],
                    'policyDefinition': vmanage.params['definition'],
                }
            ]
        else:
            policy_list = [
                {
                    'policyName': vmanage.params['name'],
                    'state': 'absent'
                }
            ]

    central_policy_updates = []
    if vmanage.params['state'] == 'present':
        central_policy_updates = vmanage_central_policy.import_central_policy_list(
            policy_list,
            check_mode=module.check_mode,
            update=vmanage.params['update'],
            push=vmanage.params['push']
        )
        if central_policy_updates:
            vmanage.result['changed'] = True
    elif vmanage.params['state'] == 'absent':
        central_policy_dict = vmanage_central_policy.get_central_policy_dict(remove_key=False)
        for policy in policy_list:
            if policy in central_policy_dict:
                if not module.check_mode:
                    vmanage_central_policy.delete_central_policy(central_policy_dict[policy['policyName']]['policyId'])
                vmanage.result['changed'] = True
    elif vmanage.params['state'] == 'activated':
        central_policy_dict = vmanage_central_policy.get_central_policy_dict(remove_key=False)
        if vmanage.params['name'] in central_policy_dict:
            if not central_policy_dict[vmanage.params['name']]['isPolicyActivated']:
                vmanage.result['changed'] = True
                if not module.check_mode:
                    action_id = vmanage_central_policy.activate_central_policy(vmanage.params['name'],
                                                                               central_policy_dict[vmanage.params['name']]['policyId'])
                    if action_id:
                        if vmanage.params['wait']:
                            vmanage_central_policy.waitfor_action_completion(action_id)
                    else:
                        vmanage.fail_json(msg='Did not get action ID after attaching device to template.')

        else:
            # TODO: The reference to 'policy' in the following line is wrong. What should it be?
            vmanage.fail_json(msg="Cannot find central policy {0}".format(vmanage.params['name']))
    if vmanage.params['state'] in ['absent', 'deactivated']:
        central_policy_dict = vmanage_central_policy.get_central_policy_dict(remove_key=False)
        if policy['policyName'] in central_policy_dict:
            if central_policy_dict[policy['policyName']]['isPolicyActivated']:
                vmanage.result['changed'] = True
                if not module.check_mode:
                    action_id = vmanage_central_policy.deactivate_central_policy(vmanage.params['name'])
                    if action_id:
                        if vmanage.params['wait']:
                            vmanage_central_policy.waitfor_action_completion(action_id)
                    else:
                        vmanage.fail_json(msg='Did not get action ID after attaching device to template.')
        else:
            vmanage.fail_json(msg="Cannot find central policy {0}".format(policy['policyName']))

    vmanage.params['updates'] = central_policy_updates
    vmanage.exit_json(**vmanage.result)
def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(
        state=dict(type='str',
                   choices=['absent', 'present', 'activated', 'deactivated'],
                   default='present'),
        name=dict(type='str', alias='policyName'),
        description=dict(type='str', alias='policyDescription'),
        definition=dict(type='str', alias='policyDefinition'),
        type=dict(type='list', alias='policyType'),
        wait=dict(type='bool', default=False),
        update=dict(type='bool', default=False),
        push=dict(type='bool', default=False),
        aggregate=dict(type='list'),
    )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )
    vmanage = Vmanage(module)
    vmanage_local_policy = LocalPolicy(vmanage.auth, vmanage.host)
    policy_data = PolicyData(vmanage.auth, vmanage.host)

    # Always as an aggregate... make a list if just given a single entry
    if vmanage.params['aggregate']:
        policy_list = vmanage.params['aggregate']
    else:
        if vmanage.params['state'] == 'present':
            policy_list = [{
                'policyName': vmanage.params['name'],
                'policyDescription': vmanage.params['description'],
                'policyType': vmanage.params['type'],
                'policyDefinition': vmanage.params['definition'],
            }]
        else:
            policy_list = [{
                'policyName': vmanage.params['name'],
                'state': 'absent'
            }]

    local_policy_updates = []
    if vmanage.params['state'] == 'present':
        local_policy_updates = policy_data.import_local_policy_list(
            policy_list,
            check_mode=module.check_mode,
            update=vmanage.params['update'],
            push=vmanage.params['push'])
        if local_policy_updates:
            vmanage.result['changed'] = True
    elif vmanage.params['state'] == 'absent':
        local_policy_dict = vmanage_local_policy.get_local_policy_dict(
            remove_key=False)
        for policy in policy_list:
            if policy in local_policy_dict:
                if not module.check_mode:
                    vmanage_local_policy.delete_local_policy(
                        local_policy_dict[policy['policyName']]['policyId'])
                vmanage.result['changed'] = True

    vmanage.params['updates'] = local_policy_updates
    vmanage.exit_json(**vmanage.result)
Esempio n. 10
0
def run_module():
    # define available arguments/parameters a user can pass to the module
    argument_spec = vmanage_argument_spec()
    argument_spec.update(
        state=dict(type='str',
                   choices=['absent', 'present', 'activated', 'deactivated'],
                   default='present'),
        name=dict(type='str', alias='policyName'),
        description=dict(type='str', alias='policyDescription'),
        definition=dict(type='str', alias='policyDefinition'),
        type=dict(type='list', alias='policyType'),
        wait=dict(type='bool', default=False),
        update=dict(type='bool', default=False),
        push=dict(type='bool', default=False),
        aggregate=dict(type='list'),
    )

    # seed the result dict in the object
    # we primarily care about changed and state
    # change is if this module effectively modified the target
    # state will include any data that you want your module to pass back
    # for consumption, for example, in a subsequent task
    result = dict(changed=False, )

    # the AnsibleModule object will be our abstraction working with Ansible
    # this includes instantiation, a couple of common attr would be the
    # args/params passed to the execution, as well as if the module
    # supports check mode
    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
    )
    vmanage = Vmanage(module)
    vmanage_security_policy = SecurityPolicy(vmanage.auth, vmanage.host)
    policy_data = PolicyData(vmanage.auth, vmanage.host)

    # Always as an aggregate... make a list if just given a single entry
    if vmanage.params['aggregate']:
        policy_list = vmanage.params['aggregate']
    else:
        if vmanage.params['state'] == 'present':
            policy_list = [{
                'policyName': vmanage.params['name'],
                'policyDescription': vmanage.params['description'],
                'policyType': vmanage.params['type'],
                'policyDefinition': vmanage.params['definition'],
            }]
        else:
            policy_list = [{
                'policyName': vmanage.params['name'],
                'state': 'absent'
            }]

    security_policy_updates = []
    if vmanage.params['state'] == 'present':
        security_policy_updates = policy_data.import_security_policy_list(
            policy_list,
            check_mode=module.check_mode,
            update=vmanage.params['update'],
            push=vmanage.params['push'])
        if security_policy_updates:
            vmanage.result['changed'] = True
    elif vmanage.params['state'] == 'absent':
        security_policy_dict = vmanage_security_policy.get_security_policy_dict(
            remove_key=False)
        for policy in policy_list:
            if policy in security_policy_dict:
                if not module.check_mode:
                    vmanage_security_policy.delete_security_policy(
                        security_policy_dict[policy['policyName']]['policyId'])
                vmanage.result['changed'] = True

    vmanage.params['updates'] = security_policy_updates
    vmanage.exit_json(**vmanage.result)