Suppress RpcError Exceptions Raised for Warnings in Junos PyEZ Applications
SUMMARY For certain operations in a Junos PyEZ application, you can suppress
RpcError
exceptions that are raised in response to
<rpc-error>
elements that have a severity of warning.
Junos PyEZ enables you to perform operational and configuration tasks on Junos
devices. In a Junos PyEZ application, when you call specific methods or execute
on-demand RPCs, Junos PyEZ sends the appropriate RPCs to the device to perform the
operation or retrieve the requested information. If the RPC reply contains
<rpc-error>
elements with a severity of warning or
higher, the Junos PyEZ application raises an RpcError
exception.
In certain cases, it might be necessary or desirable to suppress the
RpcError
exceptions that are raised in response to warnings.
You can instruct a Junos PyEZ application to suppress RpcError
exceptions that are raised for warnings by including the
ignore_warning
argument in the method call or RPC invocation.
The ignore_warning
argument takes a Boolean, a string, or a list of
strings. You can instruct the device to ignore all warnings or one or more specific
warnings.
You can use the ignore_warning
argument in the following
jnpr.junos.utils.config.Config
class methods:
-
commit()
-
diff()
-
load()
-
pdiff()
-
rollback()
You can also use ignore_warning
when you retrieve the configuration
and state data with the get()
RPC.
Ignore All Warnings
To instruct the application to ignore all warnings for an operation or RPC, include
the ignore_warning=True
argument in the method call or RPC
invocation. The following example ignores all warnings for the
load()
and commit()
methods:
from jnpr.junos import Device from jnpr.junos.utils.config import Config dev = Device(host='router1.example.com') dev.open() with Config(dev, mode='exclusive') as cu: cu.load(path='mx-config.conf', ignore_warning=True) cu.commit(ignore_warning=True) data = dev.rpc.get_configuration() print(etree.tostring(data, encoding='unicode')) dev.close()
If you include ignore_warning=True
and all of the
<rpc-error>
elements have a severity of warning, the
application ignores all warnings and does not raise an RpcError
exception. However, any <rpc-error>
elements with higher
severity levels will still raise exceptions.
Ignore Specific Warnings
To instruct the application to ignore specific warnings, set the
ignore_warning
argument to a string or a list of strings
containing the warnings to ignore. When ignore_warning
is set to a
string or list of strings, the string is used as a case-insensitive regular
expression. If a string contains only alphanumeric characters, it results in a
case-insensitive substring match. However, you can include any regular expression
pattern supported by the re
library to match warnings.
The following Junos PyEZ application ignores two specific warnings during the commit
operation. The application suppresses RpcError
exceptions if all of
the <rpc-error>
elements have a severity of warning and each
warning in the response matches one or more of the specified strings.
from jnpr.junos import Device from jnpr.junos.utils.config import Config commit_warnings = ['Advertisement-interval is less than four times', 'Chassis configuration for network services has been changed.'] dev = Device(host='router1.example.com') dev.open() with Config(dev, mode='exclusive') as cu: cu.load(path='mx-config.conf') cu.commit(ignore_warning=commit_warnings) dev.close()