Closed Oussema-99 closed 9 months ago
FYI, NBInventory
is deprecated and you should be using NetboxInventory2 instead.
https://github.com/wvandeun/nornir_netbox
You probably would need to show more of your code and how you are trying to validate whether threading is working.
Thank you for the tip, I just upgraded to the recommended plugin. The issue does persist though.
This is the nr.run() task
forti_devices = nr.filter(F(platform=FORTI_PLATFORM)) # Filter for Fortinet devices
forti_devices.run(task=backup_with_fortiapi,
state_commands=state_commands.get("forti_commands"),
output_path=directory_path,
credentials=credentials)
The task calls upon this function:
def backup_with_fortiapi(task, state_commands: dict, output_path: str, credentials: str):
device = str(task.host) # hostname
if not credentials.get(device):
logging.error(f"Couldn't find {device}'s credentials, Skipping to next device")
return False
ip = task.host.hostname
filename = f"{output_path}{device}"
port = "443" # default port
if credentials.get(device).get("port"): port = credentials.get(device).get("port") # use different port if needed
fortinet_device = APIDevice(host=ip, port=port, ssl_verify=False)
config_response = fortinet_device.api_request(path="/api/v2/monitor/system/config/backup", # obtain config
method="GET",
headers={'Authorization': f'Bearer {credentials.get(device).get("token")}'},
params={"scope": "global"})
if config_response:
write_text(data=config_response.text, file=f"{filename}_config.txt")
logging.info(f"{ip} - Performed Configuration Backup")
device_state = {}
for key, api_path in state_commands.items(): # loop through device state commands
logging.info(f"{ip} - Executing FortiAPI for '{api_path}'...")
response = fortinet_device.api_request(path=api_path,
method="GET",
headers={'Authorization': f'Bearer {credentials.get(device).get("token")}'})
if response:
device_state[key] = response.json().get("results") # example: {"interfaces": {...}}
if device_state:
write_to_json(data=device_state, json_file=f"{filename}_state.json")
logging.info(f"{ip} - Performed Device State Backup")
Upon checking the logs, I can see that the execution is being sequentially since the 2nd firewall is only being hit after the 1st one already finished execution:
10:18:09 nornir.core INFO Running task 'backup_with_fortiapi' with args {'state_commands': {'address_objects': '/api/v2/cmdb/firewall/address', 'interfaces': '/api/v2/cmdb/system/interface'}, 'output_path': 'output/06_06_2023_10:18:05/',}}
10:18:10 root WARNING 'https://96.64.3.241:443/api/v2/monitor/system/config/backup' Bad Status Code '403'
10:18:10 root INFO 96.64.3.241 - Executing FortiAPI for '/api/v2/cmdb/firewall/address'...
10:18:10 root INFO 'https://96.64.3.241:443/api/v2/cmdb/firewall/address' - 'GET' Operation successful
10:18:10 root INFO 96.64.3.241 - Executing FortiAPI for '/api/v2/cmdb/system/interface'...
10:18:12 root INFO 'https://96.64.3.241:443/api/v2/cmdb/system/interface' - 'GET' Operation successful
10:18:12 root INFO 96.64.3.241 - Performed Device State Backup
10:18:14 root INFO 'https://51.24.47.1:443/api/v2/monitor/system/config/backup' - 'GET' Operation successful
10:18:14 root INFO 51.24.47.1 - Performed Configuration Backup
10:18:14 root INFO 51.24.47.1 - Executing FortiAPI for '/api/v2/cmdb/firewall/address'...
10:18:15 root INFO 'https://51.24.47.1:443/api/v2/cmdb/firewall/address' - 'GET' Operation successful
10:18:15 root INFO 51.24.47.1 - Executing FortiAPI for '/api/v2/cmdb/system/interface'...
10:18:15 root INFO 'https://51.24.47.1:443/api/v2/cmdb/system/interface' - 'GET' Operation successful
10:18:16 root INFO 51.24.47.1 - Performed Device State Backup
I changed the device info for security
Update: I tried different stuff, it seems the first API request was failing and causing delays that made it seems one device was executing before the other. It's Solved now thanks a lot and sorry for the trouble :)
Hello,
I'm trying to use Nornir with multiple threads using Netbox inventory plugin. I currently have this configuration in place:
But I can't really see any threading taking place with my tasks. Any ideas?
Note: The tasks are running my own Netmiko and requests functions, could that be the reason?