标签:ansible
task include files
在不同tasks之间plays和playbooks可以重复调用。include files可以达成目的
系统通过使用include task来完美实现role定义,记住,playbook中的play最终
目的是映射系统群到多个roles中
cat tasks/foo.yml
---
# possibly saved as tasks/foo.yml
- name: placeholder foo
command: /bin/foo
- name: placeholder bar
command: /bin/bar
include指令类似如下,可以像普通tasks命令一样在playbook中混合使用
tasks:
- include: tasks/foo.yml
你也可以传输变量到includes指令,称之为parameterized include
如何分发多个WordPress实例,我可以包涵所有WordPress命令到一个WordPress.yml
文件
tasks:
- include: wordpress.yml wp_user=timmy
- include: wordpress.yml wp_user=alice
- include: wordpress.yml wp_user=bob
如果你使用的是ansible1.4以上版本,include语法简化了匹配roles,同时允许
传递参数列表和字典:
tasks:
- { include: wordpress.yml,wp_user:timmy,ssh_keys:[‘keys/one.txt‘,‘keys/two.txt‘]}
也可以这样引用它们{{wp_user}}
从1.0开始,ansible还支持另外一种变量传参到include files的方式-结构化变量
方法如下
tasks:
- include: wordpress.yml
vars:
wp_user: timmy
ssh_keys:
- keys/one.txt
- keys/two.txt
playbooks也同时可以include引用其它playbooks
Includes 功能也可以被用在用 handlers 区域,例如,如果你希望定义如何重启apache,你只需要定义一个playbook,只需要做一次.编辑类似如下样例的 handers.yml:
---
# this might be in a file like handlers/handlers.yml
- name: restart apache
service: name=apache state=restarted
然后像如下方式在 main playbook 的询问引用play即可:
handlers:
- include: handlers/handlers.yml
Includes也可以在常规不包含 included 的tasks和handlers文件中混合引用. Includes常被用作将一个playbook文件中的命令导入到另外一个playbook.这种方式允许我们定义由其它playbooks组成的顶层playbook(top-level playbook).
- name: this is a play at the top level of a file
hosts: all
remote_user: root
tasks:
- name: say hi
tags: foo
shell: echo "hi..."
- include: load_balancers.yml
- include: webservers.yml
- include: dbservers.yml
本文出自 “八英里” 博客,谢绝转载!
ansible学习笔记9-playbooks之include
标签:ansible
原文地址:http://5921271.blog.51cto.com/5911271/1774757