netdevops / hier_config

Hierarchical Configuration
MIT License
128 stars 24 forks source link

interface VRF #115

Closed milan2655 closed 10 months ago

milan2655 commented 1 year ago

Hallo!

where I try to change the vrf of an interface, ip_address gets deleted and would need to be reapplied.

how to achieve this with hier_config?

Running:

interface Vlan480
 vrf forwarding TEST
 ip address 1.1.1.1 255.255.255.252

Intended:

interface Vlan480
 ip address 1.1.1.1 255.255.255.252

Result:

interface Vlan480
  no vrf forwarding TEST
jtdub commented 10 months ago

Hi @milan2655 ,

This is a scenario where the out-of-the-box hier_config does not work. Hier_config is agnostic to the OS, so it isn't going to understand that the ip address has to be re-applied when an interface adds or removes a vrf.

In this instance, you will need to create a remediation plan that you can side load. In the past, I've done this by creating python functions for specific use cases and calling the python functions as part of the remediation workflow.

Here is a basic example:

>>> host = Host("example.rtr", "ios")
>>> 
>>> running = """
... interface Vlan480
...  vrf forwarding TEST
...  ip address 1.1.1.1 255.255.255.252
... """
>>> generated = """
... interface Vlan480
...  ip address 1.1.1.1 255.255.255.252
... """
>>> 
>>> host.load_running_config(running)
>>> host.load_generated_config(generated)
>>> 
>>> interface_cfg = host.generated_config.get_child("startswith", "interface Vlan480")
>>> interface_cfg.children
[HConfigChild(HConfigChild, ip address 1.1.1.1 255.255.255.252)]
>>> remediation = host.remediation_config()
>>> remediation.children
[HConfigChild(HConfig, interface Vlan480)]
>>> remediation_interface = remediation.get_child("startswith", "interface Vlan480")
>>> remediation_interface
HConfigChild(HConfig, interface Vlan480)
>>> for line in interface_cfg.all_children():
...     remediation_interface.add_child(line.text)
... 
HConfigChild(HConfigChild, ip address 1.1.1.1 255.255.255.252)
>>> print(host.remediation_config_filtered_text({},{}))
interface Vlan480
  no vrf forwarding TEST
  ip address 1.1.1.1 255.255.255.252