Nornir Task 任务

现在,您知道如何初始化nornir并使用inventory了,让我们看看如何利用它来在host组上运行task。

task是可重用的代码段,可为单个host实现某些功能。用python术语来说,它是一个将Task作为第一个参数并返回Result的函数。

例如:

from nornir.core.task import Task, Result

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

要执行task,可以使用run方法:

result = nr.run(task=hello_world)
print_result(result)
hello_world*********************************************************************
* host1.cmh ** changed : False *************************************************
vvvv hello_world ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
host1.cmh says hello world!
^^^^ END hello_world ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* host2.cmh ** changed : False *************************************************
vvvv hello_world ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
host2.cmh says hello world!
^^^^ END hello_world ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

task还可以使用任意数量的参数来扩展其功能。例如:

def say(task: Task, text: str) -> Result:
    return Result(
        host=task.host,
        result=f"{task.host.name} says {text}"
    )

然后可以像以前一样调用它,但可以为extra参数指定值:

result = nr.run(
    name="Saying goodbye in a very friendly manner",
    task=say,
    text="buhbye!"
)
print_result(result)
Saying goodbye in a very friendly manner**************************************** * host1.cmh ** changed : False ************************************************* vvvv Saying goodbye in a very friendly manner ** changed : False vvvvvvvvvvvvvvv INFO host1.cmh says buhbye! ^^^^ END Saying goodbye in a very friendly manner ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * host2.cmh ** changed : False ************************************************* vvvv Saying goodbye in a very friendly manner ** changed : False vvvvvvvvvvvvvvv INFO host2.cmh says buhbye! ^^^^ END Saying goodbye in a very friendly manner ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

请注意,我们namerun函数传递了一个参数。此参数是参数,它使我们可以为task指定一个描述性名称。如果未指定,则使用函数名称。

分组task

一个task也可以调用其他task。这很有用,因为它可以允许您通过组合较小的构建块来构建更复杂的功能。为了说明这一点,让我们首先定义一个新task:

def count(task: Task, number: int) -> Result:
    return Result(
        host=task.host,
        result=f"{[n for n in range(0, number)]}"
    )

现在,让我们将其与say我们先前定义的功能结合起来,以实现更复杂的工作流程:

def greet_and_count(task: Task, number: int) -> Result:
    task.run(
        name="Greeting is the polite thing to do",
        task=say,
        text="hi!",
    )

    task.run(
        name="Counting beans",
        task=count,
        number=number,
    )
    task.run(
        name="We should say bye too",
        task=say,
        text="bye!",
    )

    # let's inform if we counted even or odd times
    even_or_odds = "even" if number % 2 == 1 else "odd"
    return Result(
        host=task.host,
        result=f"{task.host} counted {even_or_odds} times!",
    )

值得注意的几件事:

  1. 第一次调用该say函数时,我们将文本硬编码为“ hi!”。而第二次执行此操作(最后一个操作)时,我们将其硬编码为“再见!”
  2. 调用时count,将我们也指定的参数传递给父taskgreet_and_count。这样我们就可以使我们对动态感兴趣的零件
  3. 最后,我们返回一个Result对象,其中包含有关整个工作流程的一些有意义的信息

现在我们有了可以调用的分组task,就像其他任何常规task一样:

result = nr.run(
    name="Counting to 5 while being very polite",
    task=greet_and_count,
    number=5,
)
print_result(result)
Counting to 5 while being very polite*******************************************
* host1.cmh ** changed : False *************************************************
vvvv Counting to 5 while being very polite ** changed : False vvvvvvvvvvvvvvvvvv INFO
host1.cmh counted even times!
---- Greeting is the polite thing to do ** changed : False --------------------- INFO
host1.cmh says hi!
---- Counting beans ** changed : False ----------------------------------------- INFO
[0, 1, 2, 3, 4]
---- We should say bye too ** changed : False ---------------------------------- INFO
host1.cmh says bye!
^^^^ END Counting to 5 while being very polite ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* host2.cmh ** changed : False *************************************************
vvvv Counting to 5 while being very polite ** changed : False vvvvvvvvvvvvvvvvvv INFO
host2.cmh counted even times!
---- Greeting is the polite thing to do ** changed : False --------------------- INFO
host2.cmh says hi!
---- Counting beans ** changed : False ----------------------------------------- INFO
[0, 1, 2, 3, 4]
---- We should say bye too ** changed : False ---------------------------------- INFO
host2.cmh says bye!
^^^^ END Counting to 5 while being very polite ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

觉得文章有用?

点个广告表达一下你的爱意吧 !😁