Skip to main content

hasBlockMatch

hasBlockMatch(config: List<ConfigLine>, pattern: PatternBlocks<T>) : Bool

Returns true if the block pattern pattern fully matches the config lines in config and their children, and false otherwise.

hasBlockMatch(config, pattern) returns true exactly when blockMatches(config, pattern) returns at least one record. hasBlockMatch stops after it finds one match, so it does less work than blockMatches. It does not return the data extracted by the pattern. Use blockMatches to get that data.

See Block Patterns for the block pattern syntax, and Details on Patterns for the syntax of each pattern line.

Matching rules​

hasBlockMatch matches pattern lines to config lines with the same rules as blockMatches. See blockMatches matching rules.

Null inputs​

If config, any item in config, or pattern is null, the query fails with the error First argument to “hasBlockMatch” was null; consider adding an isPresent() condition. (or Second argument for pattern). The config field of a device's files is nullable. Use isPresent to exclude null values before calling hasBlockMatch.

Examples​

Each example matches a pattern against the following configuration, which is the same configuration used in the blockMatches examples:

hostname r1
interface GigabitEthernet1
description uplink
ip address 10.0.0.1 255.255.255.0
ip address 10.0.1.1 255.255.255.0 secondary
interface GigabitEthernet2
ip address 10.0.2.1 255.255.255.0
shutdown
interface GigabitEthernet3
shutdown
router bgp 65000
neighbor 10.0.0.2 remote-as 65001

In the pattern column, each pattern line is shown on its own line, indented as in the pattern.

pattern Result Remarks
interface {name:string}
true

The pattern can contain named components, but hasBlockMatch does not return their values.

interface
shutdown
true

GigabitEthernet2 and GigabitEthernet3 both match. One match is enough to return true.

interface GigabitEthernet1
shutdown
false

The shutdown pattern line must match a child of the matched interface GigabitEthernet1 line, and that block has no shutdown line.

interface GigabitEthernet1
description uplink
shutdown
false

A partial match is not enough: GigabitEthernet1 matches two of the three pattern lines. Use blockDiff to see which lines are missing.

interface GigabitEthernet2
{%-%} shutdown
false

The negated shutdown line requires that GigabitEthernet2 has no shutdown line, and it has one.

interface GigabitEthernet3
shutdown
{%unmatched-config:forbid%}
true

{%unmatched-config:forbid%} requires every child of the matched interface line to match a pattern line. GigabitEthernet3 has only the shutdown child.

router ospf
false

No config line starts with router ospf.

Example query​

The following query lists the devices that do not have the required NTP server configured.

pattern = ```
ntp server 10.0.0.1
```;

foreach device in network.devices
where isPresent(device.files.config)
where !hasBlockMatch(device.files.config, pattern)
select {device: device.name}
Deprecated Bag overload

hasBlockMatch also accepts a Bag<ConfigLine> as its first argument. A Bag has no defined order, so this overload produces a warning. Use orderBy() or order() to convert the argument to a List<ConfigLine>. In a future release, the warning will become a type-checking error.

See also​

Guides​

Types​

Functions​

  • blockMatches: Gets every complete match of a block pattern against a list of config lines.
  • blockDiff: Gets the closest partial match of a block pattern, including the pattern lines that are missing and the config lines that are extraneous.
  • blockPattern: Creates a block pattern from a string.
  • withOption: Changes how a block pattern handles unmatched config lines and partial line matches.