Ejemplo n.º 1
0
def add_vlan(task, vlan_id, vlan_name):
    vlan_config = [f"vlan {vlan_id}", f"name {vlan_name}"]

    if task.host.platform == 'nxos':
        show_vlan = f"show vlan id {vlan_id}"
    if task.host.platform == 'eos':
        show_vlan = f"show vlan {vlan_id}"

    r = task.run(task=networking.netmiko_send_command,
                 command_string=show_vlan,
                 use_textfsm=True)

    result = Result(host=task.host)
    result.changed = False

    if isinstance(r.result, str) and 'not found' in r.result:
        ### create new vlan
        task.run(task=networking.netmiko_send_config,
                 config_commands=vlan_config)
        result.changed = True

    if isinstance(r.result, list):
        ### vlan exists, check name
        if r.result[0]['name'] != vlan_name:
            ### update vlan name
            task.run(task=networking.netmiko_send_config,
                     config_commands=vlan_config)
            result.changed = True

    return result
Ejemplo n.º 2
0
def add_vlan(task, vlan_id, vlan_name, dry_run=False):
    vlan_config = f"vlan {vlan_id}\nname {vlan_name}\n"

    if task.host.platform == 'nxos':
        show_vlan = f"show vlan id {vlan_id}"
    if task.host.platform == 'eos':
        show_vlan = f"show vlan {vlan_id}"

    r = task.run(task=networking.netmiko_send_command,
                 command_string=show_vlan,
                 use_textfsm=True)

    result = Result(host=task.host)
    result.changed = False
    result.failed = False
    result.result = f"Vlan {vlan_id} already set up correctly"

    if isinstance(r.result, str) and 'not found' in r.result:
        ### create new vlan
        config_result = task.run(task=networking.napalm_configure,
                                 configuration=vlan_config,
                                 dry_run=dry_run)
        if dry_run:
            result.changed = False
            result.result = f"DRYRUN: Vlan {vlan_id} not found - create it from scratch"
        else:
            result.changed = config_result.result.changed
            result.result = f"Vlan {vlan_id} not found - create it from scratch"

    if isinstance(r.result, str) and 'Invalid' in r.result:
        result.failed = True
        result.result = f"Vlan {vlan_id} is invalid"

    if isinstance(r.result, list):
        ### vlan exists, check name
        current_name = r.result[0]['name']
        if current_name != vlan_name:
            ### update vlan name
            config_result = task.run(task=networking.napalm_configure,
                                     configuration=vlan_config,
                                     dry_run=dry_run)
            if dry_run:
                result.changed = False
                result.result = f"DRYRUN: Vlan {vlan_id} - wrong name ({current_name}) - updating to {vlan_name}"
            else:
                result.changed = config_result.result.changed
                result.result = f"Vlan {vlan_id} - wrong name ({current_name}) - updating to {vlan_name}"

    return result
Ejemplo n.º 3
0
def get_config(task):
    r = task.run(task=networking.napalm_get,
        getters=["config"],
        getters_options={"config": {"retrieve":"running"}})

    result = Result(host=task.host)
    result.changed = False
    result.failed = False
    result.result = r.result
    return result
Ejemplo n.º 4
0
def set_config(task, config):
    r = task.run(task=networking.napalm_configure,
                 configuration=config,
                 dry_run=True)
    from pprint import pprint as pp
    pp(r)

    result = Result(host=task.host)
    result.changed = r.changed
    result.failed = r.failed
    result.result = r.result
    return r
    return result