def export_policy_to_file(self, export_file): policy_lists = PolicyLists(self.session, self.host, self.port) policy_definitions = PolicyDefinitions(self.session, self.host, self.port) local_policy = LocalPolicy(self.session, self.host, self.port) central_policy = CentralPolicy(self.session, self.host, self.port) policy_lists_list = policy_lists.get_policy_list_list() policy_definitions_list = policy_definitions.get_policy_definition_list( ) central_policies_list = central_policy.get_central_policy_list() local_policies_list = local_policy.get_local_policy_list() policy_export = { 'vmanage_policy_lists': policy_lists_list, 'vmanage_policy_definitions': policy_definitions_list, 'vmanage_central_policies': central_policies_list, 'vmanage_local_policies': local_policies_list } if export_file.endswith('.json'): with open(export_file, 'w') as outfile: json.dump(policy_export, outfile, indent=4, sort_keys=False) elif export_file.endswith(('.yaml', 'yml')): with open(export_file, 'w') as outfile: yaml.dump(policy_export, outfile, default_flow_style=False) else: raise Exception("File format not supported")
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_policy_definition_lists = PolicyDefinitions(vmanage.auth, vmanage.host) vmanage.result['policy_definitions'] = vmanage_policy_definition_lists.get_policy_definition_list(vmanage.params['type']) vmanage.exit_json(**vmanage.result)
def __init__(self, session, host, port=443): """Initialize Files object with session parameters. Args: session (obj): Requests Session object host (str): hostname or IP address of vManage port (int): default HTTPS 443 """ self.session = session self.host = host self.port = port self.base_url = f'https://{self.host}:{self.port}/dataservice/' self.device_templates = DeviceTemplates(self.session, self.host, self.port) self.feature_templates = FeatureTemplates(self.session, self.host, self.port) self.template_data = TemplateData(self.session, self.host, self.port) self.policy_data = PolicyData(self.session, self.host, self.port) self.policy_lists = PolicyLists(self.session, self.host, self.port) self.policy_definitions = PolicyDefinitions(self.session, self.host, self.port) self.local_policy = LocalPolicy(self.session, self.host, self.port) self.central_policy = CentralPolicy(self.session, self.host, self.port) self.vmanage_device = Device(self.session, self.host, self.port)
def __init__(self, session, host, port=443): """Initialize Localized Policy object with session parameters. Args: session (obj): Requests Session object host (str): hostname or IP address of vManage port (int): default HTTPS 443 """ self.session = session self.host = host self.port = port self.base_url = f'https://{self.host}:{self.port}/dataservice/' self.policy_definitions = PolicyDefinitions(self.session, self.host)
def definition(ctx, name, json, definition_type): #pylint: disable=unused-argument """ Show policy definition information """ policy_definitions = PolicyDefinitions(ctx.auth, ctx.host, ctx.port) policy_data = PolicyData(ctx.auth, ctx.host, ctx.port) pp = pprint.PrettyPrinter(indent=2) if name: policy_definition_dict = policy_definitions.get_policy_definition_dict(definition_type) if name in policy_definition_dict: policy_definition = policy_data.export_policy_definition_list(policy_definition_dict[name]['type'].lower()) # list_keys(policy_definition['definition']) pp.pprint(policy_definition) else: policy_definition_list = policy_data.export_policy_definition_list(definition_type) pp.pprint(policy_definition_list)
def __init__(self, session, host, port=443): """Initialize Policy Method object with session parameters. Args: session (obj): Requests Session object host (str): hostname or IP address of vManage port (int): default HTTPS 443 """ self.session = session self.host = host self.port = port self.base_url = f'https://{self.host}:{self.port}/dataservice/' self.policy_lists = PolicyLists(self.session, self.host, self.port) self.policy_definitions = PolicyDefinitions(self.session, self.host, self.port) self.local_policy = LocalPolicy(self.session, self.host, self.port) self.central_policy = CentralPolicy(self.session, self.host, self.port) self.security_policy = SecurityPolicy(self.session, self.host, self.port)
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 import_policy_from_file(self, file, update=False, check_mode=False, push=False): vmanage_policy_lists = PolicyLists(self.session, self.host, self.port) vmanage_policy_definitions = PolicyDefinitions(self.session, self.host, self.port) vmanage_central_policy = CentralPolicy(self.session, self.host, self.port) vmanage_local_policy = LocalPolicy(self.session, self.host, self.port) policy_list_updates = [] policy_definition_updates = [] central_policy_updates = [] local_policy_updates = [] # Read in the datafile if not os.path.exists(file): raise Exception('Cannot find file {0}'.format(file)) with open(file) as f: if file.endswith('.yaml') or file.endswith('.yml'): policy_data = yaml.safe_load(f) else: policy_data = json.load(f) # Separate the feature template data from the device template data if 'vmanage_policy_lists' in policy_data: policy_list_data = policy_data['vmanage_policy_lists'] else: policy_list_data = [] if 'vmanage_policy_definitions' in policy_data: policy_definition_data = policy_data['vmanage_policy_definitions'] else: policy_definition_data = [] if 'vmanage_central_policies' in policy_data: central_policy_data = policy_data['vmanage_central_policies'] else: central_policy_data = [] if 'vmanage_local_policies' in policy_data: local_policy_data = policy_data['vmanage_local_policies'] else: local_policy_data = [] policy_list_updates = vmanage_policy_lists.import_policy_list_list( policy_list_data, check_mode=check_mode, update=update, push=push) vmanage_policy_lists.clear_policy_list_cache() policy_definition_updates = vmanage_policy_definitions.import_policy_definition_list( policy_definition_data, check_mode=check_mode, update=update, push=push) central_policy_updates = vmanage_central_policy.import_central_policy_list( central_policy_data, check_mode=check_mode, update=update, push=push) local_policy_updates = vmanage_local_policy.import_local_policy_list( local_policy_data, check_mode=check_mode, update=update, push=push) return { 'policy_list_updates': policy_list_updates, 'policy_definition_updates': policy_definition_updates, 'central_policy_updates': central_policy_updates, 'local_policy_updates': local_policy_updates }