ansible中的include, include_tasks 和 import_tasks 的差别
原文链接 https://drmingdrmer.github.io/tech/devops/2018/02/03/ansible-import-include.html
注:以下为加速网络访问所做的原文缓存,经过重新格式化,可能存在格式方面的问题,或偶有遗漏信息,请以原文为准。
<!-- mdtoc start -->
<!-- mdtoc end -->
include_tasks 和 import_tasks
<!--excerpt-->
include
被deprecated了. 建议使用include_tasks
和 import_tasks
.
include_tasks
是动态的: 在运行时展开. when只应用一次. 被include的文件名可以使用变量.import_tasks
是静态的: 在加载时展开. when在被import的文件里的每个task, 都会重新检查一次. 因为是加载时展开的, 文件名的变量不能是动态设定的.When using static includes, ensure that any variables used in their names are defined in vars/vars_files or extra-vars passed in from the command line. Static includes cannot use variables from inventory sources like group or host vars.
<!--more-->
例子
x.yml
:
- hosts: 127.0.0.1
gather_facts: False
tasks:
- set_fact: mode=1
- include_tasks: y.yml
when: mode == "1"
- set_fact: mode=1
- import_tasks: y.yml
when: mode == "1"
y.yml
:
- set_fact: mode="2"
- debug:
msg: >
Display in only `include_tasks`.
`include_tasks` does NOT re-evaluate `mode` for every step.
`import_tasks` DOES re-evaluate condition.
运行ansible-play -b x.yml
后:
debug 的message只在include_tasks
里被执行了.
第2个import_tasks
中的debug被skip掉了.
因为mode
被改变之后, include_tasks
不会重新evaluate mode,import_tasks
会根据变化后的mode值重新evaluate每个task的条件.
示例结果:
TASK [set_fact] *******************************************************
ok: [127.0.0.1]
TASK [include_tasks] **************************************************
included: /root/devops/ansible/y.yml for 127.0.0.1
TASK [set_fact] *******************************************************
ok: [127.0.0.1]
TASK [debug] **********************************************************
ok: [127.0.0.1] => {
"msg": "Display in only `include_tasks`.
`include_tasks` does NOT re-evaluate mode for every step.
`import_tasks` DOES re-evaluate condition\n"
}
TASK [set_fact] *******************************************************
ok: [127.0.0.1]
TASK [set_fact] *******************************************************
ok: [127.0.0.1]
TASK [debug] **********************************************************
skipping: [127.0.0.1]