• Working With Playbooks--Creating Reusable Playbooks


    Creating Reusable Playbooks

    创建可重用的剧本

    While it is possible to write a playbook in one very large file (and you might start out learning playbooks this way), eventually you’ll want to reuse files and start to organize things. In Ansible, there are three ways to do this: includes, imports, and roles.

    虽然可以在一个非常大的文件中编写一个剧本(并且你可能会开始以这种方式学习剧本),但最终你会想要重用文件并开始组织事情。在Ansible中,有三种方法可以做到这一点:包括,导入和角色。

    Includes and imports (added in Ansible version 2.4) allow users to break up large playbooks into smaller files, which can be used across multiple parent playbooks or even multiple times within the same Playbook.

    包含和导入(在Ansible 2.4版中添加)允许用户将大型剧本分解为较小的文件,这些文件可以在多个父级剧本中使用,甚至可以在同一个Playbook中多次使用。

    Roles allow more than just tasks to be packaged together and can include variables, handlers, or even modules and other plugins. Unlike includes and imports, roles can also be uploaded and shared via Ansible Galaxy.

    角色不仅可以将任务打包在一起,还可以包含变量,处理程序,甚至模块和其他插件。与包含和导入不同,角色也可以通过Ansible Galaxy上传和共享。

    Dynamic vs. Static

    动态与静态

    Ansible has two modes of operation for reusable content: dynamic and static.

    Ansible有两种可重用内容的操作模式:动态和静态。

    In Ansible 2.0, the concept of dynamic includes was introduced. Due to some limitations with making all includes dynamic in this way, the ability to force includes to be static was introduced in Ansible 2.1. Because the include task became overloaded to encompass both static and dynamic syntaxes, and because the default behavior of an include could change based on other options set on the Task, Ansible 2.4 introduces the concept of include vs. import.

    在Ansible 2.0中,引入动态包含的概念由于以这种方式使所有包括动态的一些限制,在Ansible 2.1中引入了强制包括静态的能力因为include任务变得过载以包含静态和动态语法,并且因为include的默认行为可能会根据Task上设置的其他选项而改变,所以Ansible 2.4引入了include vs import. 的概念

    If you use any import* Task (import_playbookimport_tasks, etc.), it will be static. If you use any include* Task (include_tasksinclude_role, etc.), it will be dynamic.

    如果您使用任何import*任务(import_playbookimport_tasks等),它将是静态的如果您使用任何include*任务(include_tasksinclude_role等),它将是动态的

    The bare include task (which was used for both Task files and Playbook-level includes) is still available, however it is now considered deprecated.

    include任务(用于任务文件和Playbook级别包括)仍然可用,但现在它被认为已弃用

    Differences Between Static and Dynamic

    静态和动态之间的差异

    The two modes of operation are pretty simple:

    这两种操作模式非常简单:

    • Ansible pre-processes all static imports during Playbook parsing time. 
    • Ansible在Playbook解析时间内预处理所有静态导入。
    • Dynamic includes are processed during runtime at the point in which that task is encountered.
    • 动态包含在运行时在遇到该任务的点处理。

    When it comes to Ansible task options like tags and conditional statements (when:):

    当涉及Ansible任务选项tags和条件语句(when:)时:

    • For static imports, the parent task options will be copied to all child tasks contained within the import.
    • 对于静态导入,父任务选项将复制到导入中包含的所有子任务。
    • For dynamic includes, the task options will only apply to the dynamic task as it is evaluated, and will not be copied to child tasks.
    • 对于动态包含,任务选项在评估时应用于动态任务,并且不会复制到子任务。
    Roles are a somewhat special case. Prior to Ansible 2.3, roles were always statically included via the special roles: option for a given play and were always executed first before any other play tasks (unless pre_tasks were used). Roles can still be used this way, however, Ansible 2.3 introduced the include_role option to allow roles to be executed inline with other tasks.
    
    角色是一种特殊情况。在Ansible 2.3之前,角色总是静态地包含在roles:给定游戏的特殊选项中,并且总是在任何其他游戏任务之前首先执行(除非pre_tasks被使用)。角色仍然可以这种方式使用,但是,Ansible 2.3引入了include_role允许角色与其他任务一起执行的选项。

    Tradeoffs and Pitfalls Between Includes and Imports

    包含和进口之间的权衡和陷阱

    Using include* vs. import* has some advantages as well as some tradeoffs which users should consider when choosing to use each:

    使用include*vs. import*具有一些优势以及用户在选择使用每个时应考虑的一些权衡:

    The primary advantage of using include* statements is looping. When a loop is used with an include, the included tasks or role will be executed once for each item in the loop.

    使用include*语句的主要优点是循环。当循环与include一起使用时,包含的任务或角色将对循环中的每个项执行一次。

    Using include* does have some limitations when compared to import* statements:

    include*import*语句相比,使用确实有一些限制:

    • Tags which only exist inside a dynamic include will not show up in --list-tags output.
    • 仅存在于动态包含内的标记将不会显示在--list-tags输出中。
    • Tasks which only exist inside a dynamic include will not show up in --list-tasks output.
    • 仅存在于动态包含内的任务不会显示在--list-tasks输出中。
    • You cannot use notify to trigger a handler name which comes from inside a dynamic include (see note below).
    • 您不能使用notify触发来自动态包含内的处理程序名称(请参阅下面的注释)。
    • You cannot use --start-at-task to begin execution at a task inside a dynamic include.
    • 您不能使用--start-at-task在动态包含内的任务开始执行。

    Using import* can also have some limitations when compared to dynamic includes:

    import*与动态包含相比,使用也可能有一些限制:

    • As noted above, loops cannot be used with imports at all.
    • 如上所述,循环根本不能与导入一起使用。
    • When using variables for the target file or role name, variables from inventory sources (host/group vars, etc.) cannot be used.
    • 将变量用于目标文件或角色名称时,不能使用库存源(主机/组变量等)中的变量。
    1 Note:
    2 
    3 Regarding the use of notify for dynamic tasks: it is still possible to trigger the dynamic include itself, which would result in all tasks within the include being run.
    4 
    5 关于notify动态任务的使用:仍然可以触发动态包含本身,这将导致包含内的所有任务被运行。
  • 相关阅读:
    How to Change Error Message Colors in Windows 10 PowerShell Console
    How to expand Azure VM OS Disk
    Ubuntu terminal colors
    Docker proxy
    如何对mRemoteNG在进行Linux终端访问时自定义配色
    100阶乘末尾有多少个零
    几道c/c++练习题
    M-BM-
    从FGUI中取一张图片并返回一个Sprite
    为什么手游选择lua热更新方案。
  • 原文地址:https://www.cnblogs.com/cevinchen/p/9610170.html
Copyright © 2020-2023  润新知