Nornir文档翻译----第二节 任务

Tasks

#首先我们写如下脚本,初始化对象,稍后会用到
from nornir import InitNornir
from nornir_utils.plugins.functions import print_result

nr = InitNornir(config_file="config.yaml")

#筛选对象简化输出
nr = nr.filter(site="cmh", role="host")

  现在已经知道了如何初始化nornir和使用inventory,下面让我们看下如何使用nornir在hosts上执行任务。
  一个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.hostname} says hello world! "
    )

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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Tasks 还可以通过其他任何参数来扩展它的功能。例如:

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

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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

注意我们传递了一个name的参数去run函数。这个参数可以允许我们为这个task起个名字,如果没有指定的话就用函数名代替。

Grouping tasks

一个task可以调用其他的tasks。这是很有用的可以让你通过一些小的模块来构建复杂的功能。为了说明这个我们来定义一个task:

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

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!",
    )

    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!”,第二次调用我们编码说“bye!”
  2. 当我们调用count函数时我们传递了一个参数,这个参数我们在父task中已经赋值。这样我们就搞好了我们感兴趣的部分。
  3. 最后我们返回一个Result对象,这个Result对象携带了一些关于整个工作流的有用信息。
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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

你可能感兴趣的:(Nornir文档翻译----第二节 任务)