include介绍
如果诸位有在shell或者python文件中调用其它文件内函数的操作,那就很容易理解include(也算类似nginx主配置文件中的include)。include的出现可以我们更加简洁且条理清晰的去编写playbook。用一个总的yaml去包含所有子yaml,呈现在用户眼前的只有主yaml中寥寥几行include。
include历史用法
-
引用其它yaml:搭建LNMP需要mysql,装jira/confluence等也需要,so把mysql单独拎出来。(感觉此例不太恰当,emmm,反正就这意思)。
0 20:26:46 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat common1.yaml - yum: name=mysql 0 20:26:50 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat lnmp.yaml --- - hosts: ck-node1 tasks: - include: /server/ops_ansible/common1.yaml - name: deploy lnmp yum: name=nginx,php-fpm 0 20:26:52 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook lnmp.yaml
-
引用其它yaml时传入变量。
0 20:29:32 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat common2.yaml - debug: msg: "{{var1}}" - debug: msg: "{{var2}}" 0 20:29:38 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test1.yaml --- - hosts: ck-node1 tasks: - include: /server/ops_ansible/common2.yaml var1=bob var2=zoe 0 20:29:41 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test1.yaml
-
针对某个include打标签。
0 10:08:01 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat common3.yaml - debug: msg: "task1 load common3.yml" 0 10:08:05 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat common4.yaml - debug: msg: "task2 load common4.yml" 0 10:08:06 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test3.yaml --- - hosts: ck-node1 tasks: - name: task1 include: /server/ops_ansible/common3.yaml tags: t1 - name: task2 include: /server/ops_ansible/common4.yaml tags: t2 0 10:08:10 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook -t t2 test3.yaml
-
为include添加条件判断和循环。
0 10:16:03 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat common5.yaml - debug: msg: "task load common5.yml" 0 10:16:52 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test4.yaml --- - hosts: ck-node1 tasks: - include: /server/ops_ansible/common5.yaml when: 5>2 - include: /server/ops_ansible/common5.yaml loop: [1,2,3] 0 10:16:58 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test4.yaml
-
当外层test.yaml文件和内层common.yaml文件都存在loop循环的时候,内层item默认会调用自己的loop,如果想要调用外层的loop,需要用loop_var指定。
0 10:33:42 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat common6.yaml - debug: msg: "{{item}} -- task load common6.yml" loop: [a,b,c] 0 10:33:43 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test5.yaml --- - hosts: ck-node1 tasks: - include: /server/ops_ansible/common6.yaml loop: [1,2,3] 0 10:33:46 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test5.yaml # 指定调用外层文件的loop。 0 10:36:34 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat common7.yaml - debug: msg: "{{outer_item}} -- {{item}} -- task load common6.yml" loop: [a,b,c] 0 10:36:38 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test6.yaml --- - hosts: ck-node1 tasks: - include: /server/ops_ansible/common7.yaml loop: [1,2,3] loop_control: loop_var: outer_item 0 10:36:40 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test6.yaml
include_tasks用法
-
新用法包含关系用include_tasks来替代include,理解了上面的include,include_task也基本掌握了。
0 10:43:02 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat common.yaml - debug: msg: "task1 load common.yml" -------------------------------------------------------------------------------------------------------- # include --- - hosts: ck-node1 tasks: - debug: msg: "test task1" - include: common.yaml - debug: msg: "test task2" -------------------------------------------------------------------------------------------------------- # include_tasks --- - hosts: ck-node1 tasks: - debug: msg: "test task1" - include_tasks: common.yaml # include_tasks会打印出common文件的绝对路径,include不会。 - debug: msg: "test task2" # 上面的写法等于省略了file参数,还原写法如下: - include_tasks: file: /server/ops_ansible/common.yaml
-
include_tasks+tags
0 13:24:28 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat common.yaml - debug: msg: "task1 load common.yml" -------------------------------------------------------------------------------------------------------- # 举例一 ## include --- - hosts: ck-node1 tasks: - debug: msg: "test task1" - include: common.yaml tags: t1 - debug: msg: "test task2" -------------------------------------------------------------------------------------------------------- ## include_tasks --- - hosts: ck-node1 tasks: - debug: msg: "test task1" - include_tasks: common.yaml tags: t1 - debug: msg: "test task2" ## 说明:比执行结果会发现include会执行引用的common任务,而include_tasks则不会。说明include_tasks下的tags只用作用于自身,不作用与包含的任务。 # 举例二:如果想要例1的tags也作用于包含的任务,需要接住apply参数。 --- - hosts: ck-node1 tasks: - debug: msg: "test task1" - include_tasks: file: common.yaml apply: tags: t1 tags: always - debug: msg: "test task2" tag: t2 ## 说明:例2的结果说明apply中的tags作用于include_tasks所包含的任务,且如果想要apply中的tags生效,还必须包含always标签。
import_playbook用法
使用”include”关键字除了能引用任务列表,还能引用整个playbook。在ansible 2.8版本之后,统一使用“import_playbook”模块替代“include”模块用来引入playbook,”include”关键字的引用整个playbook特性已被弃用。
-
示例如下
0 14:33:04 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test1.yaml --- - hosts: ck-node1 tasks: - debug: msg: "task in test1.yaml" - import_playbook: test2.yaml 0 14:33:10 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test2.yaml --- - hosts: ck-node1 tasks: - debug: msg: "task load test2.yaml" 0 14:33:11 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test1.yaml
-
import_playbook其它用法和include基本一样,不再赘述。
写作不易,转载请注明出处,谢谢~~