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
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
def create_result( result_content: Any, host: str = "", destination: Optional[str] = None, failed: bool = False, exception: Optional[BaseException] = None, **kwargs: Any ) -> Result: result = Result(host=Host(name=host), destination=destination, **kwargs) result.result = result_content result.failed = failed result.exception = exception return result
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