Hierarchical Configuration (hier-config)

3.6.0 · active · verified Thu Apr 16

Hierarchical Configuration, or hier-config, is a Python library designed to query and compare network device configurations. It parses configuration text into a hierarchical tree to perform deterministic, line-level diffs that respect vendor syntax rules. This allows it to generate precise remediation commands to bring a device into compliance with an intended configuration. The library is actively maintained, with its current version being 3.6.0, and new releases occurring regularly.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load existing and intended device configurations, create hierarchical configuration objects, and then use `WorkflowRemediation` to generate the commands needed to bring the running configuration into compliance with the intended state. It simulates config files and cleans them up afterwards.

from hier_config import WorkflowRemediation, get_hconfig, Platform
from hier_config.utils import read_text_from_file
import os

# Create dummy config files for demonstration
running_config_content = '''
hostname DEVICE-RTR-01
!
interface GigabitEthernet0/1
 description Uplink to CORE
 ip address 10.0.0.1 255.255.255.0
 no shutdown
!
router bgp 65000
 neighbor 192.168.1.1 remote-as 65001
'''

intended_config_content = '''
hostname DEVICE-RTR-01-UPDATED
!
interface GigabitEthernet0/1
 description Uplink to CORE - Primary
 ip address 10.0.0.1 255.255.255.0
 no shutdown
!
router bgp 65000
 neighbor 192.168.1.1 remote-as 65001
 address-family ipv4 unicast
  neighbor 192.168.1.1 activate
'''

with open('running_config.conf', 'w') as f:
    f.write(running_config_content)

with open('intended_config.conf', 'w') as f:
    f.write(intended_config_content)

# Step 1: Load configurations from files
running_config_text = read_text_from_file('running_config.conf')
intended_config_text = read_text_from_file('intended_config.conf')

# Step 2: Create HConfig objects, specifying the device platform
running_hconfig = get_hconfig(Platform.CISCO_IOS, running_config_text)
intended_hconfig = get_hconfig(Platform.CISCO_IOS, intended_config_text)

# Step 3: Initialize WorkflowRemediation to compare and generate remediation
workflow = WorkflowRemediation(running_hconfig, intended_hconfig)

# Step 4: Print the remediation configuration
print('Remediation Configuration:')
for line in workflow.remediation_config:
    print(line.text)

# Clean up dummy files
os.remove('running_config.conf')
os.remove('intended_config.conf')

view raw JSON →