Replies: 3 comments
-
Please, use issues for issues and discussions for discussions. I have migrated the issue to a discussion myself. |
Beta Was this translation helpful? Give feedback.
-
So what you describe is already supported, however, you are proposing a standardized interface for this. I have a few concerns/questions:
Thanks for taking the time explaining the feature, I think we need to delve a bit into the details though and also see if the overall community thinks this is useful. Adding features have a cost in terms of testing and maintaining so the returns need to outweigh the costs. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the explanation on the Discussions tab, I haven't used it previously so didn't know that was an option, I'll post any other ideas I have over here.
# Adapted from the load_inventory function in nornir/init_nornir.py
def write_inventory(config: Config, inv : Inventory):
InventoryPluginRegister.auto_register()
inventory_plugin = InventoryPluginRegister.get_plugin(config.inventory.plugin)
inventory_writer = getattr(inventory_plugin, 'write', False)
if inventory_writer:
inventory_writer(inv)
else:
logger.error('Write method is not supported for the current inventory plugin.')
raise Exception("NotImplementedMethod") This would be called during during runtime (going back to my initial example workflow): from nornir import InitNornir, write_inventory
from nornir.core.inventory import Host
# Initialize Nornir
nr = InitNornir(config_file='config.yaml')
# ... Run some tasks, here, grab data necessary to identify R2
# Add R2 to current inventory
nr.inventory.hosts['R2'] = Host(name='R2', username='cisco', password='developer', hostname='192.168.0.2')
write_inventory(nr.config, nr.inventory)
|
Beta Was this translation helpful? Give feedback.
-
Please do not close issues before giving the one posting the opportunity to reply, as Github does not allow non-contributors to reopen issues closed by contributors (see here). I'm also pretty sure that contributors are not notified by messages on closed issues so there's the possibility discussion threads will go unanswered; this also leads to issues getting duplicated.
Anyway, this refers back to #764 with clarifications/corrections to my last reply. My idea was to have Inventory plugins be "read-write" in the sense that Nornir could use them to serialize the current runtime inventory back to its original format. Let's suppose I begin a network discovery workflow with a single host (using SimpleInventory as an example):
I run some CDP discovery on R1 and end up discovering R2. After some business logic I add R2 to my current inventory during runtime, using something such as:
I want some sort of "inventory dumping" mechanism that delegates the task of writing my current runtime inventory back to its original data format. Something such as:
Would call some logic from SimpleInventory, which would result in the original hosts.yaml being modified to:
Of course, it's important that this "serialization" be delegated to Inventory plugins because how this serialization must be done depends on the inventory source; updating devices on Netbox when using nornir_netbox is different from updating the yaml files for SimpleInventory.
I believe this could be achieved by implementing some sort of
dump_inventory()
method on the Nornir class, this method would call back to an Inventory Plugins' method responsible for writing to the data source. This would require inventory plugins to implement the business logic to update their respective data source, but would make nornir much more dynamic in how it handles inventories as these could then be updated automatically after some runtime manipulation. Maintainers of inventory plugins could choose not to implement this feature, of course, in which case the feature wouldn't be supported for that plugin but everything else would still work as expected. Users would still always have the possibility of implementing this inventory dump themselves if they so need or desire, but to me it makes sense that nornir could provide this as a feature.Just thought I'd give my 10 cents and bring up the discussion, since it could simplify some cool network as code and version control scenarios where inventory sources are updated automagically after detecting some changes to the network.
Beta Was this translation helpful? Give feedback.
All reactions