ansible常用模块 -- include模块 -- 包括一个play或任务列表
include模块 – 包括一个play或任务列表
一、摘要
- 包含一个文件,其中包含当前剧本中要执行的剧本或任务的列表。
- 带有play列表的文件只能包含在顶层。任务列表只能包含在任务正常运行的地方(在play中)。
- 在Ansible 2.0之前,所有的include都是“静态的”,在剧本编译时执行。
- 静态包含不受大多数指令的约束。例如,将循环或条件应用于每个继承的任务。
- 从Ansible 2.0开始,任务包含是动态的,行为更像真实的任务。这意味着它们可以循环、跳过和使用来自任何源的变量。Ansible尝试自动检测,但你可以使用静态指令(这是在Ansible 2.1中添加的)来绕过自动检测。
- Windows目标也支持此模块。
二、参数
参数 | 选项/默认值 | 描述 |
---|---|---|
free-form | 这个模块允许您直接指定文件名,而不需要任何其他选项。 |
三、示例
- hosts: localhost
tasks:
- debug:
msg: play1
- name: Include a play after another play
include: otherplays.yaml
- hosts: all
tasks:
- debug:
msg: task1
- name: Include task list in play
include: stuff.yaml
- debug:
msg: task10
- hosts: all
tasks:
- debug:
msg: task1
- name: Include task list in play only if the condition is true
include: "{{ hostvar }}.yaml"
static: no
when: hostvar is defined
官方文档:https://docs.ansible.com/ansible/2.9/modules/include_module.html#include-module