> For the complete documentation index, see [llms.txt](https://jncia-workbook.gitbook.io/workbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jncia-workbook.gitbook.io/workbook/junos-basics/21-reducing-repetitive-configuration.md).

# 21) Reducing Repetitive Configuration

### Pre-Work

Load the config called **repetitive.config.cfg** using the **load\_config\_on\_nodes.py** script.

```
python3 load_config_on_nodes.py --lab_dir three-routers --config_filename repetitive.config.cfg
```

### Lab

Connect to R1, and achieve the following:

* Each GigE interface should have `interface-type p2p` under OSPF
  * `ge-0/0/3` on R1 should be excluded. It should have the default network-type of `broadcast`.
* The `Lo0` interface should automatically have `passive` under OSPF

Apply this in such a way that new GigE interfaces automatically have `interface-type p2p`, without explicitly configuring it.

### Answer

<details>

<summary>Expand to reveal</summary>

To achieve this, we must use a configuration group.

```
set groups OSPF_GROUP protocols ospf area 0.0.0.0 interface <ge*> interface-type p2p 
set groups OSPF_GROUP protocols ospf area 0.0.0.0 interface lo0.0 passive

set protocols ospf apply-groups OSPF_GROUP
set protocols ospf area 0.0.0.0 interface ge-0/0/3 apply-groups-except OSPF_GROUP
```

</details>

### Explanation

<details>

<summary>Expand to reveal</summary>

Configuration groups allow us to easily apply configuration that is reused. A common use case might be to apply interface settings to all physical interfaces on a device.&#x20;

\
The other benefit of configuration groups is adherence to a configuration standard. For example, in this lab, we are applying `passive` to lo0.0. (Note that this isn't really doing anything. There is no possibility for the device to form an adjacency over lo0.0 anyways, it is just used as an example). Even though applying `passive` to lo0.0 isn't a repetitive configuration, it still is a way to ensure all devices have the same configuration. You would just copy+paste the group configuration to all devices.

\
To match a wildcard interface name, such as "ge-\*", we must put the regex in angled brackets:

```
set groups OSPF_GROUP protocols ospf area 0.0.0.0 interface <ge*>
```

> However, `<` and `>` have a special meaning when used under the `groups` hierarchy. In the `groups` hierarchy, you must enclose in angle brackets any term using a wildcard pattern \<pattern> to differentiate it from other wildcards in the configuration file.

Source: <https://www.juniper.net/documentation/us/en/software/junos/cli/topics/topic-map/configuration-groups-usage.html>

\
The group is applied at a particular hierarchy using the command `apply-groups <group name>`.

\
If you want any configuration underneath this hierarchy to be *excluded* from inheriting the group configuration, you use `apply-groups-except`.

\
Note that the inherited configuration will not be displayed by default when you `show configuration`:

```
admin@R1> show configuration protocols ospf    
apply-groups OSPF_GROUP;
area 0.0.0.0 {
    interface ge-0/0/0.0;
    interface ge-0/0/1.0;
    interface ge-0/0/2.0;
    interface ge-0/0/3.0 {
        apply-groups-except OSPF_GROUP;
    }
    interface lo0.0;
}
```

\
You can use **| display inheritance** to see what was inherited:

```
admin@R1> show configuration protocols ospf | display inheritance 
area 0.0.0.0 {
    interface ge-0/0/0.0 {
        ##
        ## 'p2p' was inherited from group 'OSPF_GROUP'
        ##
        interface-type p2p;
    }
    interface ge-0/0/1.0 {
        ##
        ## 'p2p' was inherited from group 'OSPF_GROUP'
        ##
        interface-type p2p;
    }
    interface ge-0/0/2.0 {
        ##
        ## 'p2p' was inherited from group 'OSPF_GROUP'
        ##
        interface-type p2p;
    }
    interface ge-0/0/3.0 {
        apply-groups-except OSPF_GROUP;
    }
    interface lo0.0 {
        ##
        ## 'passive' was inherited from group 'OSPF_GROUP'
        ##
        passive;
    }
}
```

\
Add `no-comments` to remove all the comment lines:

```
admin@R1> show configuration protocols ospf | display inheritance no-comments 
area 0.0.0.0 {
    interface ge-0/0/0.0 {
        interface-type p2p;
    }
    interface ge-0/0/1.0 {
        interface-type p2p;
    }
    interface ge-0/0/2.0 {
        interface-type p2p;
    }
    interface ge-0/0/3.0 {
        apply-groups-except OSPF_GROUP;
    }
    interface lo0.0 {
        passive;
    }
}

```

\
The same principle happens with displaying as set commands. By default the inherited config is not displayed:

```
admin@R1> show configuration protocols ospf | display set 
set protocols ospf apply-groups OSPF_GROUP
set protocols ospf area 0.0.0.0 interface ge-0/0/0.0
set protocols ospf area 0.0.0.0 interface ge-0/0/1.0
set protocols ospf area 0.0.0.0 interface ge-0/0/2.0
set protocols ospf area 0.0.0.0 interface ge-0/0/3.0 apply-groups-except OSPF_GROUP
set protocols ospf area 0.0.0.0 interface lo0.
```

\
But you can `| display inheritence` with `| display set` as well:

```
admin@R1> show configuration protocols ospf | display set | display inheritance 
set protocols ospf area 0.0.0.0 interface ge-0/0/0.0 interface-type p2p
set protocols ospf area 0.0.0.0 interface ge-0/0/1.0 interface-type p2p
set protocols ospf area 0.0.0.0 interface ge-0/0/2.0 interface-type p2p
set protocols ospf area 0.0.0.0 interface ge-0/0/3.0 apply-groups-except OSPF_GROUP
set protocols ospf area 0.0.0.0 interface lo0.0 passive
```

\
Also note that you can apply multiple groups to the same hierarchy, which is unlike IOS-XR. When doing this, the first group listed has priority in case there is overlapping configuration.

</details>

### Further Reading

<https://www.juniper.net/documentation/us/en/software/junos/junos-overview/cli/topics/concept/junos-software-configuration-groups-understanding.html>

<https://www.juniper.net/documentation/us/en/software/junos/cli/topics/topic-map/configuration-groups-usage.html>

<https://www.sunsetlearning.com/wp-content/uploads/2016/07/Configuration-Groups-Juniper-Blog.pdf>

<https://www.reddit.com/r/Juniper/comments/q87xv5/pros_and_cons_of_using_applygroups/>

<https://www.reddit.com/r/Juniper/comments/18aqfm7/configuration_groups_for_voipdata_interfaces/>
