Working With Playbooks--Creating Reusable Playbooks

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Working With Playbooks--Creating Reusable Playbooks相关的知识,希望对你有一定的参考价值。

?????????port   plugin   pack   ??????   eating   ??????   ??????   wrap   output   

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_playbook???import_tasks???????????????????????????????????????????????????include*?????????include_tasks???include_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???????????????????????????????????????????????????????????????????????????????????????????????????????????????

 

 

以上是关于Working With Playbooks--Creating Reusable Playbooks的主要内容,如果未能解决你的问题,请参考以下文章

Spring For Hadoop--Working With HBase

Working with forms

ImageSharp Working with images

Flink - Working with State

Working with Data Sources

[POC] Working with Blocks