Skip to main content

Quickstart

This page takes you from an installed package to a first run on your own machine. You need no cluster and no network device: the example task does not connect to anything.

1. Write an inventory​

Create hosts.yaml with three hosts:

hosts.yaml
r1:
hostname: 192.0.2.1
r2:
hostname: 192.0.2.2
r3:
hostname: 192.0.2.3

2. Select the runner in the Nornir configuration​

Create config.yaml. The runner section is the only part that names nornir-ray:

config.yaml
inventory:
plugin: SimpleInventory
options:
host_file: hosts.yaml

runner:
plugin: ray

If you already have a Nornir project, this is the one change to make: replace the runner section of your configuration.

3. Write and run a script​

hello.py
from nornir import InitNornir
from nornir.core.task import Result, Task


def hello(task: Task) -> Result:
return Result(host=task.host, result=f"hello from {task.host.name}")


nr = InitNornir(config_file="config.yaml")
result = nr.run(task=hello)

for name, host_results in result.items():
top = host_results[0]
location = getattr(top, "ray", None)
print(name, "|", top.result, "| worker:", location.worker_id if location else None)

nr.runner.close()
python hello.py

The script prints one line for each host. The worker identifier at the end of the line shows which worker ran the host:

r1 | hello from r1 | worker: 5f0c…/2
r2 | hello from r2 | worker: 5f0c…/0
r3 | hello from r3 | worker: 5f0c…/2

What happened​

  1. No address of a cluster is set, so the runner started a local Ray on your machine.
  2. The runner started four worker processes, the default.
  3. The runner gave each host to one worker. Each worker ran hello for its hosts and sent the results back.
  4. nr.runner.close() stopped the workers and the local Ray. The runner also stops them when the script exits.

Select the runner in code instead​

You can set the runner in the script and leave the configuration as it is:

from nornir import InitNornir
from nornir_ray import RayRunner

nr = InitNornir(config_file="config.yaml")
nr = nr.with_runner(RayRunner(workers_per_node=4))
result = nr.run(task=hello)

Next steps​