This repository contains example automation jobs designed for use within Netpicker, our all-in-one network automation platform.
Netpicker automation jobs are Python functions that leverage the power of Netmiko to interact with network devices. You can define jobs to perform configuration changes, execute show commands, gather data, and integrate with Netpicker's workflow engine.
To define an automation job, use the @job
decorator from the comfy.automate
library on a Python function.
from comfy.automate import job
import logging
# Target Juniper platforms (Junos) using Netmiko platform type wildcard
@job(platform='juniper_junos*')
def set_ntp_server_juniper_junos(device, ntp_server: str):
"""
Configures the NTP server on a Juniper device and commits the change.
Args:
device: The Netpicker device object (contains IP, platform, etc.).
ntp_server (str): The IP address or hostname of the NTP server.
"""
logging.info(f"Starting NTP configuration job on device {device.ipaddress}")
# Commands to send in configuration mode
config_commands = [
f"set system ntp server {ntp_server}",
"commit" # Necessary for Junos to apply changes
]
try:
# device.cli provides access to Netmiko methods
# send_config_set enters config mode, sends commands, and exits
result = device.cli.send_config_set(config_commands)
logging.info(f"Configuration successful: {result}")
# Return the result for use in Netpicker workflows
return result
except Exception as e:
logging.error(f"Error configuring NTP on {device.ipaddress}: {e}")
# Optionally re-raise or return an error indicator
raise
platform
: Specifies the target device platform(s).- Accepts a string or a list of strings.
- These strings correspond to Netmiko platform types. Examples:
'cisco_ios'
,'arista_eos'
,'juniper_junos'
. - You can use an asterisk (
*
) as a wildcard (e.g.,'cisco*'
matchescisco_ios
,cisco_xe
,cisco_xr
, etc.).
device
: The first argument is always the Netpickerdevice
object. It contains attributes likename
,ipaddress
,platform
,tags
, etc.- Custom Arguments: Any subsequent arguments (like
ntp_server: str
in the example) are defined by you. These become parameters that must be provided when running the job via Netpicker.
Netpicker provides access to Netmiko's capabilities through the device.cli
object.
- Use
device.cli.send_config_set([...])
to send configuration commands. - How it works: Netmiko typically automatically enters configuration mode on the device, sends the list of commands you provide, and then exits configuration mode.
- Important: For many platforms (like Cisco IOS/NX-OS, Arista EOS), changes made in configuration mode are not saved automatically. You usually need to include a command like
'write memory'
or'copy running-config startup-config'
in your command list if you want the changes to persist after a reboot. - For platforms like Juniper Junos, a
'commit'
command is required within the configuration session to apply the changes, as shown in the example. - Reference: Netmiko
send_config_set
documentation
- Use
device.cli.send_command("...")
to execute operational or show commands (e.g.,'show version'
,'show interfaces'
). - This method does not enter configuration mode.
- The output of the command is returned as a string.
- Reference: Netmiko
send_command
documentation
- Error Handling: Network operations can fail. Wrap
device.cli
calls intry...except
blocks to catch potential Netmiko exceptions (e.g., timeouts, authentication errors) and log them appropriately. - Timeouts: Be mindful of command execution time. Netmiko has default timeouts, but complex commands might take longer. While direct timeout adjustment might not be exposed via
device.cli
, structuring jobs efficiently helps. For long-running commands, consider alternative approaches if possible. - Platform Differences: Always consult the Netmiko documentation and test thoroughly, as command syntax and behavior (especially regarding configuration saving) vary significantly between vendors and platforms.
- Idempotency: Design your configuration jobs to be idempotent where possible. This means running the job multiple times should result in the same final state without causing errors or unintended changes.
- Use Python's standard
logging
module (import logging
). - Call
logging.info()
,logging.warning()
,logging.error()
,logging.debug()
as needed. - Logs are captured and visible within the Netpicker job execution details.
- You can
return
a value (like theresult
fromsend_config_set
orsend_command
) from your job function. - Returned values can be captured by Netpicker and used in subsequent steps within a workflow, enabling conditional logic or data passing.
For more details on Netpicker, visit netpicker.io. For in-depth Netmiko information, refer to the official Netmiko documentation.