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 templates(ctx, input_file, check, update, diff, name, template_type): """ Import templates from file """ vmanage_files = Files(ctx.auth, ctx.host, ctx.port) pp = pprint.PrettyPrinter(indent=2) click.echo(f'Importing templates from {input_file}') result = vmanage_files.import_templates_from_file( input_file, update=update, check_mode=check, name_list=name, template_type=template_type) print( f"Feature Template Updates: {len(result['feature_template_updates'])}") if diff: for diff_item in result['feature_template_updates']: click.echo(f"{diff_item['name']}:") pp.pprint(diff_item['diff']) print(f"Device Template Updates: {len(result['device_template_updates'])}") if diff: for diff_item in result['device_template_updates']: click.echo(f"{diff_item['name']}:") pp.pprint(diff_item['diff'])
def policies(ctx, input_file, check, update, push, diff): """ Import policies from file """ vmanage_files = Files(ctx.auth, ctx.host) pp = pprint.PrettyPrinter(indent=2) click.echo(f"{'Checking' if check else 'Importing'} policies from {input_file}") result = vmanage_files.import_policy_from_file(input_file, update=update, check_mode=check, push=push) print(f"Policy List Updates: {len(result['policy_list_updates'])}") if diff: for diff_item in result['policy_list_updates']: click.echo(f"{diff_item['name']}:") pp.pprint(diff_item['diff']) print(f"Policy Definition Updates: {len(result['policy_definition_updates'])}") if diff: for diff_item in result['policy_definition_updates']: click.echo(f"{diff_item['name']}:") pp.pprint(diff_item['diff']) print(f"Central Policy Updates: {len(result['central_policy_updates'])}") if diff: for diff_item in result['central_policy_updates']: click.echo(f"{diff_item['name']}:") pp.pprint(diff_item['diff']) print(f"Local Policy Updates: {len(result['local_policy_updates'])}") if diff: for diff_item in result['local_policy_updates']: click.echo(f"{diff_item['name']}:") pp.pprint(diff_item['diff']) print(f"Security Policy Updates: {len(result['security_policy_updates'])}") if diff: for diff_item in result['security_policy_updates']: click.echo(f"{diff_item['name']}:") pp.pprint(diff_item['diff'])
def policies(ctx, export_file): """ Export policies to file """ vmanage_files = Files(ctx.auth, ctx.host) click.echo(f'Exporting policies to {export_file}') vmanage_files.export_policy_to_file(export_file)
def attachments(ctx, device_type, name, output_file): """ Export attachments to file """ num = 0 vmanage_files = Files(ctx.auth, ctx.host, ctx.port) if device_type == 'controllers': if name: click.echo(f'Exporting controller attachment(s) {",".join(name)} to {output_file}') num = vmanage_files.export_attachments_to_file(output_file, name_list=name, device_type=device_type) else: click.echo(f'Exporting controller attachment to {output_file}') num = vmanage_files.export_attachments_to_file(output_file, device_type=device_type) elif device_type == 'vedges': if name: click.echo(f'Exporting vedge attachment(s) {",".join(name)} to {output_file}') num = vmanage_files.export_attachments_to_file(output_file, name_list=name, device_type=device_type) else: click.echo(f'Exporting vedge attachment to {output_file}') num = vmanage_files.export_attachments_to_file(output_file, device_type=device_type) else: if name: click.echo(f'Exporting attachment(s) {",".join(name)} to {output_file}') num = vmanage_files.export_attachments_to_file(output_file, name_list=name, device_type=device_type) else: click.echo(f'Exporting attachment to {output_file}') num = vmanage_files.export_attachments_to_file(output_file, device_type=device_type) click.echo(f"Exported {num} attachments")
def attachments(ctx, input_file, check, update, name, template_type): """ Import attachments from file """ vmanage_files = Files(ctx.auth, ctx.host, ctx.port) click.echo(f'Importing attachments from {input_file}') result = vmanage_files.import_attachments_from_file( input_file, update=update, check_mode=check, name_list=name, template_type=template_type) print(f"Attachment Updates: {len(result['updates'])}") for host, failure in result['failures'].items(): click.secho(f"{host}: {failure}", err=True, fg='red')
pp = pprint.PrettyPrinter(indent=2) session = Authentication(host=vmanage_host, port=vmanage_port, user=username, password=password).login() with open("sdwanlab.j2") as f: policy_template = Template(f.read()) policy_def = policy_template.render(config=policy_variables) with open("sdwanlab_temp.json", "w") as f: f.write(policy_def) vmanage_files = Files(session, vmanage_host) result = vmanage_files.import_policy_from_file("sdwanlab_temp.json", update=True) print(f"Policy List Updates: {len(result['policy_list_updates'])}") print( f"Policy Definition Updates: {len(result['policy_definition_updates'])}" ) print( f"Central Policy Updates: {len(result['central_policy_updates'])}") print(f"Local Policy Updates: {len(result['local_policy_updates'])}") os.remove("sdwanlab_temp.json") except Exception as e:
def templates(ctx, template_type, name, export_file): """ Export templates to file """ vmanage_files = Files(ctx.auth, ctx.host) if template_type == 'device': if name: click.echo( f'Exporting device template(s) {",".join(name)} to {export_file}' ) vmanage_files.export_templates_to_file(export_file, name_list=name, template_type='device') else: click.echo(f'Exporting device templates to {export_file}') vmanage_files.export_templates_to_file(export_file, template_type='device') elif template_type == 'feature': if name: click.echo( f'Exporting feature template(s) {",".join(name)} to {export_file}' ) vmanage_files.export_templates_to_file(export_file, name_list=name, template_type='feature') else: click.echo(f'Exporting feature templates to {export_file}') vmanage_files.export_templates_to_file(export_file, template_type='feature') else: if name: raise click.ClickException("Must specify template type with name") click.echo(f'Exporting templates to {export_file}') vmanage_files.export_templates_to_file(export_file)