netbox_tasks
This task plugin is to sync information between Netbox and Nornir host.
Software versions tested:
Netbox v3.2.9, pynetbox 6.6.2
netbox_tasks sample usage
Sample code to run netbox_tasks
task:
from nornir_salt.plugins.tasks import netbox_tasks
from nornir import InitNornir
nr = InitNornir(config_file="nornir_config.yaml")
res = nr.run(
task=netbox_tasks,
task_name="sync_from",
via="dev"
)
All netbox tasks return Nornir Result object with “result” set to string describing task results and with “status” key set to either True or False depending on task success.
netbox_tasks returns
Returns requests result string in XML or JSON format.
netbox_tasks reference
- nornir_salt.plugins.tasks.netbox_tasks.netbox_tasks(task: nornir.core.task.Task, task_name: str = 'dir', **kwargs) nornir.core.task.Result
Task call Netbox related task functions, used mainly by Salt-Nornir proxy minion module.
- Parameters
task_name – name of task to run
kwargs – any
**kwargs
to use with syn method method
Supported tasks names:
dir
- lists available task namessync_from
- usessync_device_from_netbox
task plugin to synchronizedata from Netbox to Nornir host’s inventory
sync_to
- usessync_device_to_netbox
task plugin to synchronizedata from Nornir host’s inventory to Netbox
- nornir_salt.plugins.tasks.netbox_tasks.sync_device_from_netbox(task: nornir.core.task.Task, via: str = 'default', data_key: str = 'netbox') dict
Function to sync data from Netbox to host’s inventory using host’s name (not hostname) to retrieve respective Netbox device entry data.
- Parameters
via – name of netbox instance connection parameters
data_key – Nornir inventory data key name to save host’s Netbox inventory
- Return dict
Nornir results object with operation result and
status
attribute containingTrue
if data synced andFalse
if device not found
- nornir_salt.plugins.tasks.netbox_tasks.sync_device_to_netbox(task: nornir.core.task.Task, via: str = 'default', data_key: str = 'netbox') dict
Function to sync Nornir hosts inventory into Netbox device.
- Parameters
via – name of netbox instance connection parameters
data_key – Nornir inventory data key name with Netbox inventory
If no device with given name exists in Netbox, it will be created.
All parameters under host.data.netbox synced to Netbox. In addition these parameters added if they are missing from host.data.netbox
name - set to host name
Sample Nornir hosts
data_key=netbox
data populated manually or by usingsync_device_from_netbox
task:nornir: hosts: ceos1: data: netbox: airflow: label: Front to rear value: front-to-rear asset_tag: null cluster: null comments: '' config_context: domain_name: lab.io lo0_ip: 4.3.2.1 syslog_servers: - 10.0.0.3 - 10.0.0.4 created: '2022-08-21T02:26:49.631041Z' custom_fields: {} device_role: display: router id: 1 name: router slug: router url: http://192.168.64.200:8000/api/dcim/device-roles/1/ device_type: display: FakeNOS Arista cEOS id: 1 manufacturer: display: FakeNOS id: 1 name: FakeNOS slug: fakenos url: http://192.168.64.200:8000/api/dcim/manufacturers/1/ model: FakeNOS Arista cEOS slug: arista-ceos url: http://192.168.64.200:8000/api/dcim/device-types/1/ display: ceos1 face: label: Front value: front id: 2 last_updated: '2022-08-23T06:42:33.513505Z' local_context_data: domain_name: lab.io lo0_ip: 4.3.2.1 syslog_servers: - 10.0.0.3 - 10.0.0.4 location: null name: ceos1 parent_device: null platform: display: FakeNOS Arista cEOS id: 1 name: FakeNOS Arista cEOS slug: fakenos-arista-ceos url: http://192.168.64.200:8000/api/dcim/platforms/1/ position: 41 primary_ip: address: 4.3.2.1/24 display: 4.3.2.1/24 family: 4 id: 3 url: http://192.168.64.200:8000/api/ipam/ip-addresses/3/ primary_ip4: address: 4.3.2.1/24 display: 4.3.2.1/24 family: 4 id: 3 url: http://192.168.64.200:8000/api/ipam/ip-addresses/3/ primary_ip6: null rack: display: R101 id: 1 name: R101 url: http://192.168.64.200:8000/api/dcim/racks/1/ serial: '' site: display: SALT-NORNIR-LAB id: 1 name: SALT-NORNIR-LAB slug: salt-nornir-lab url: http://192.168.64.200:8000/api/dcim/sites/1/ status: label: Active value: active tags: [] tenant: null url: http://192.168.64.200:8000/api/dcim/devices/2/ vc_position: null vc_priority: null virtual_chassis: null
Above data automatically transformed to data below prior to pushing to Netbox making sure to remove read-only keys and resolve key to values:
nornir: hosts: ceos1: data: netbox: airflow: front-to-rear asset_tag: null cluster: null comments: '' config_context: domain_name: lab.io lo0_ip: 4.3.2.1 syslog_servers: - 10.0.0.3 - 10.0.0.4 custom_fields: {} device_role: name: router slug: router device_type: manufacturer: name: FakeNOS slug: fakenos model: FakeNOS Arista cEOS slug: arista-ceos face: front local_context_data: domain_name: lab.io lo0_ip: 4.3.2.1 syslog_servers: - 10.0.0.3 - 10.0.0.4 location: null name: ceos1 parent_device: null platform: name: FakeNOS Arista cEOS slug: fakenos-arista-ceos position: 41 primary_ip: address: 4.3.2.1/24 primary_ip4: address: 4.3.2.1/24 primary_ip6: null rack: name: R101 serial: '' site: name: SALT-NORNIR-LAB slug: salt-nornir-lab status: active tags: [] tenant: null vc_position: null vc_priority: null virtual_chassis: null
If the goal is to sync Nornir inventory with Netbox devices this is the minimum set of mandatory fields to be populated into Nornir hosts’ data:
nornir: hosts: ceos2: data: netbox: device_role: slug: router device_type: slug: arista-ceos site: slug: salt-nornir-lab status: active
Where device type, role and site must refer to valid Netbox entities.
Sample code to run
sync_device_to_netbox
task:from nornir_salt.plugins.tasks import sync_device_to_netbox from nornir import InitNornir nr = InitNornir(config_file="nornir_config.yaml") res = nr.run(task=sync_device_to_netbox)