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 |
|---|---|---|
|
true |
The pattern can contain named components, but |
|
true |
|
|
false |
The |
|
false |
A partial match is not enough: |
|
false |
The negated |
|
true |
|
|
false |
No config line starts with |
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}
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.