NAPALM Documentation
repository·develop·Indexed 25 days ago
https://github.com/napalm-automation/napalmNetwork Automation and Programmability Abstraction Layer with Multivendor support. NAPALM is a Python library providing a unified API to interact with various network vendor devices, including Arista EOS, Cisco IOS, IOS-XR, NX-OS, and Juniper JunOS. It enables consistent data retrieval, configuration manipulation, and integration with automation frameworks like Ansible, SaltStack, and StackStorm.
What's inside NAPALM
- NAPALM (Network Automation and Programmability Abstraction Layer with Multivendor support) is a Python library that provides a unified API to interact with different network device Operating Systems. It allows you to use a consistent set of functions to connect to devices, manipulate configurations, and retrieve data across multiple vendors.
Integrate NAPALM with automation frameworks
developNAPALM can be integrated into several major automation frameworks:
- Ansible: Use the
napalm-ansiblemodules to leverage the NAPALM API within Ansible playbooks. - SaltStack: NAPALM is natively integrated into SaltStack (since release
Carbon). For setup recommendations, refer tonapalm-salt. - StackStorm: Use the
stackstorm-napalmintegration pack to use NAPALM within StackStorm workflows.
- Ansible: Use the
Prerequisites for NAPALM development and testing
developDepending on your use case (especially if you are working with virtualized network environments), you may need the following tools installed on your system:
- Python: The core programming language.
- pip: The recommended tool for installing Python packages.
- VirtualBox: A software virtualization tool used for running virtual machines.
- Vagrant: A command-line utility for managing the lifecycle of virtual machines (useful for setting up lab environments).
How the mock driver resolves data files
developThe
mockdriver determines which file to read based on the function name and its position in the call stack.Call Stack Rules:
device.open()counts as the first command in the stack.- Subsequent method calls start numbering from 1.
- For standard methods, the filename pattern is
{method_name}.{call_index}. - For
device.cli(commands)calls, the pattern iscli.{call_index}.{sanitized_command}.{command_index}.
Data Formats:
- Standard methods expect a JSON file containing the desired return value.
clicommands expect a plain text file containing the raw command output.- To simulate errors, provide a JSON file defining the exception type and its arguments.
Understand NXOS configuration merging behavior
developMerges in the NXOS driver are implemented by applying configuration lines one by one. Unlike configuration replacement, merges do not use the checkpoint/rollback functionality and are therefore not atomic.
Diffs for merges are generated using the
Netutilslibrary to compare the candidate configuration against the running configuration offline. The resulting diff consists of the lines present in the merge candidate config.Handle missing custom methods across different OSs
developBecause custom methods are not part of the base NAPALM driver classes, attempting to call a custom method on an OS that does not implement it will result in an ungraceful failure. To handle this, you should explicitly raise
NotImplementedErrorin your custom driver implementations for operating systems where the method is not supported.from napalm.ios.ios import IOSDriver class CustomIOSDriver(IOSDriver): """Custom NAPALM Cisco IOS Handler.""" def get_my_banner(self): raise NotImplementedErrorUse XML Configuration with IOS-XR NETCONF
developUsingconfig_encoding="xml"with theiosxr_netconfdriver is considered experimental. There is a high probability that XML configurations may not work correctly, and only small subsections of the configuration might be successfully modified via merge operations. For stability, CLI configuration is recommended.Enable Configuration Rollback on Cisco IOS
developTo enable the 'Configuration Rollback Confirmed Change' feature (auto-rollback on error), the IOS
archivefunctionality must be enabled and configured to use a local filesystem (e.g.,flash:orbootflash:).Example configuration on the device:
archive path flash:archive write-memoryIf you wish to explicitly disable auto-rollback, pass
auto_rollback_on_error=Falseas an optional argument when initializing the driver.archive path flash:archive write-memoryHow EOS handles Multi-line/HEREDOC commands
developEOS configuration is loaded via
pyeapi.eapilib.Node.run_commands(), which does not natively handle multi-line commands (e.g.,banner motd).NAPALM's
EOSDriver._load_config()helper function mitigates this by attempting to detect HEREDOC commands in the input configuration and converting them into a dictionary format that the eAPI understands.How Rollback works in EOS
developThe rollback feature in NAPALM for EOS is supported only when committing via the API.
Internal Mechanism:
- During a commit operation, the API executes:
copy startup-config flash:rollback-0. - During a rollback operation, the API executes:
configure replace flash:rollback-0.
Warning: Because rollback relies on these specific API-driven file operations, if you perform configuration changes outside of the NAPALM API, you must manually mark your last rollback point to ensure consistency.
- During a commit operation, the API executes:
Use NAPALM to validate network state and deployments
developNAPALM can be used to automate the validation of network state, ensuring that deployments match expected configurations.
Common use cases include:
- Automated Verification: Checking that BGP neighbors are configured and in the correct state (e.g., 'up').
- Inventory-Driven Validation: Building validator files dynamically from inventory data to verify network state against expectations without human intervention.
- Pre/Post Maintenance Validation: Writing manual validation files based on gathered network data and expectations before a maintenance window, then running them after changes to ensure the network state is exactly as intended. This replaces manual checks and one-off scripts.
How the NAPALM testing framework works
developNAPALM uses a centralized testing framework to ensure consistent functionality across all vendor drivers. The framework relies on shared tests defined in
napalm.base/test/getters.pyand uses mocked data to validate driver outputs.Key Features:
- Shared Tests: The same test suite is applied across all vendors.
- Multiple Test Cases: A single test function can run multiple scenarios (e.g., 'normal', 'no_peers', 'lots_of_peers') by looking up specific subdirectories in the mocked data folder.
- Automatic Skipping: Methods marked as
NotImplementedare automatically skipped by the framework. - Output Validation: The framework compares the actual driver output against expected results stored in the mocked data.
- Configurable Targets: You can switch between running against mocked data or a live device using environment variables.