NAPALM for Network Engineers
Candidate Configuration and compare_config()
What you'll learn
- get_config()'s real, normalized return shape
- load_merge_candidate() vs load_replace_candidate()
- compare_config()'s real signature and real diff output
- How this connects to dry-run (Phase 2.8) and check mode (Phase 3.0) as the same underlying principle
Listen to a guided explanation of this lesson.
Spoken narration isn't supported in this browser.
get_config(): three configs, normalized
def get_config(
self, retrieve: str = "all", full: bool = False,
sanitized: bool = False, format: str = "text",
) -> ConfigDict: ...
# ConfigDict's real fields, verified via inspect:
# running: str, startup: str, candidate: strVerified directly. get_config() is itself a getter (its name starts with get_) -- same normalization principle as Lesson 3, applied to configuration text instead of facts/interfaces.
get_config keys: ['running', 'candidate', 'startup']
get_config['running'] (truncated): hostname SW1
interface GigabitEthernet0/Real, verified output. Unlike get_facts()/get_interfaces(), get_config()'s values are configuration text, not further-structured fields -- normalized at the level of 'which config are you asking for,' not the text's internal syntax.
Loading a candidate: merge vs. replace
def load_merge_candidate(self, filename=None, config=None) -> None: ...
def load_replace_candidate(self, filename=None, config=None) -> None: ...Verified directly. Both stage a candidate configuration without touching the running config -- merge adds/changes just the given lines; replace treats the given config as the device's entire intended configuration.
driver.load_merge_candidate(config="vlan 20\n name VOICE\n")
diff = driver.compare_config()
print(repr(diff))Staging a candidate never touches the device's actual running configuration -- confirmed directly: nothing changes until commit_config() (Lesson 5).
compare_config(): the diff, before anything changes
def compare_config(self) -> str: ...Verified directly against IOSDriver (the real driver class), not just NAPALM's mock -- takes no arguments. A candidate must already be loaded via load_merge_candidate()/load_replace_candidate() first.
'+vlan 20\n+ name VOICE'Real, verified diff output -- a plain string, unified-diff-style, showing exactly what would change if committed.
The same principle, a third real implementation
Reliable Automation (Phase 2.8) taught dry-run mode by hand: check what would change before changing it. Ansible (Phase 3.0) offered the same guarantee through --check mode. compare_config() is NAPALM's own real implementation of the identical principle -- see the diff, review it, then decide whether to commit or discard (Lesson 5).
Key takeaways
- get_config() normalizes which configuration you're asking for (running/startup/candidate as a fixed three-key dict) -- the text itself stays vendor-specific syntax.
- load_merge_candidate() stages additive changes; load_replace_candidate() stages a full intended-state replacement -- verified as genuinely different operations, not interchangeable.
- compare_config() takes no arguments on the real driver interface and returns a real diff string -- verified directly, never invented.
- compare_config() is dry-run mode (Phase 2.8) and check mode (Phase 3.0)'s underlying principle, implemented natively by NAPALM -- review the diff before Lesson 5's commit or discard.