You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
processors are very powerful but one limitation is that is doesn't seem possible to know the (number of) hosts actually in the run, as the list according to on_good and on_failed is calculated (https://github.com/nornir-automation/nornir/blob/v3.4.1/nornir/core/__init__.py#L118) after task_started() is called, could it be possible to have access to this information (as well as on_good and on_failed and maybe raise_on_error)?
One hacky way to get this informations is:
importloggingfromdataclassesimportdataclassfromnornir.core.processorimportProcessorlogger=logging.getLogger(__name__)
@dataclassclass_NornirRunVars:
on_good: boolon_failed: boolraise_on_error: bool|NoneclassProgressProcessor(Processor):
deftask_started(self, task):
_run_vars=self._get_nornir_run_vars()
if_run_vars.on_good:
self.run_hosts=len(task.nornir.inventory.hosts)
else:
self.run_hosts=len(task.nornir.data.failed_hosts)
ifnot_run_vars.on_failed:
self.run_hosts-=len(task.nornir.data.failed_hosts)
# [..]def_get_nornir_run_vars(self) ->_NornirRunVars:
""" Get the run() `on_good`, `on_failed` and `raise_on_error` variables, to recalculate `run_on` Issue for a better way: https://github.com/nornir-automation/nornir/issues/885 """try:
# https://stackoverflow.com/questions/15608987/how-can-i-access-variables-from-the-caller-even-if-it-isnt-an-enclosing-scopeimportinspect# Find the frame containing these variablesrequired_vars= ("task", "on_good", "on_failed", "raise_on_error")
# Iterate on some frames, skipping the known not relevant onesframe=inspect.currentframe()
foriinrange(6): # only allow 6 backifframeisNone:
raiseRuntimeError('cannot inspect stack frames')
ifi>2: # 0 is self, 1 is self.task_started(), 2 is Processors().task_started()ifall(varinframe.f_localsforvarinrequired_vars):
return_NornirRunVars(
on_good=frame.f_locals["on_good"],
on_failed=frame.f_locals["on_failed"],
raise_on_error=frame.f_locals["raise_on_error"],
)
frame=frame.f_back# Not foundraiseAttributeError('vars not found in outer scope')
exceptExceptionase:
logger.exception(e)
# Assume default valuesreturn_NornirRunVars(
on_good=True,
on_failed=False,
raise_on_error=None,
)
Thanks
The text was updated successfully, but these errors were encountered:
The problem is that data resides in completely different parts of the stack. An easier solution would be to pass that information explicitly to your processor.
Hi,
processors are very powerful but one limitation is that is doesn't seem possible to know the (number of) hosts actually in the run, as the list according to
on_good
andon_failed
is calculated (https://github.com/nornir-automation/nornir/blob/v3.4.1/nornir/core/__init__.py#L118) aftertask_started()
is called, could it be possible to have access to this information (as well ason_good
andon_failed
and mayberaise_on_error
)?One hacky way to get this informations is:
Thanks
The text was updated successfully, but these errors were encountered: