Initializing Nornir

Easiest way of initializing nornir is with the function InitNornir.

With InitNornir you can initialize nornir with a configuration file, with code or with a combination of both.

Let’s start with a configuration file:

  1. [2]:
  1. %highlight_file config.yaml
  1. [2]:
  1. 1 ---
  2. 2 inventory:
  3. 3 plugin: SimpleInventory
  4. 4 options:
  5. 5 host_file: "inventory/hosts.yaml"
  6. 6 group_file: "inventory/groups.yaml"
  7. 7 defaults_file: "inventory/defaults.yaml"
  8. 8 runner:
  9. 9 plugin: threaded
  10. 10 options:
  11. 11 num_workers: 100

Now to create the nornir object:

  1. [3]:
  1. from nornir import InitNornir
  2. nr = InitNornir(config_file="config.yaml")

You can also initialize nornir programmatically without a configuration file:

  1. [4]:
  1. from nornir import InitNornir
  2. nr = InitNornir(
  3. runner={
  4. "plugin": "threaded",
  5. "options": {
  6. "num_workers": 100,
  7. },
  8. },
  9. inventory={
  10. "plugin": "SimpleInventory",
  11. "options": {
  12. "host_file": "inventory/hosts.yaml",
  13. "group_file": "inventory/groups.yaml"
  14. },
  15. },
  16. )

Or with a combination of both methods:

  1. [5]:
  1. from nornir import InitNornir
  2. nr = InitNornir(
  3. config_file="config.yaml",
  4. runner={
  5. "plugin": "threaded",
  6. "options": {
  7. "num_workers": 50,
  8. },
  9. },
  10. )
  1. [6]:
  1. nr.config.runner.options["num_workers"]
  1. [6]:
  1. 50