参考文章:http://www.zsythink.net/archives/2962
通过include,我们可以在一个playbook中包含另一个文件,以便实现我们刚才所描述的效果,这篇文章我们就来了解一下"include"的用法
# cat install_MysqlAndPhp.yml
- yum:
name: mysql
state: present
- yum:
name: php-fpm
state: present
如上例所示,两个task被提取到了install_MysqlAndPhp.yml文件中,当我们需要安装mysql和php-fpm时,只需要调用此yml文件即可,那么怎样调用这个文件呢?方法如下,我们只要把lamp.yml和lnmp.yml修改为如下模样即可
# cat lamp.yml
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- include: install_MysqlAndPhp.yml
- yum:
name: httpd
state: present
# cat lnmp.yml
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- include: install_MysqlAndPhp.yml
- yum:
name: nginx
state: present
正如你所看到的,我们使用了include模块,引用了install_MysqlAndPhp.yml文件,当我们引用此文件时,install_MysqlAndPhp.yml文件中的tasks都会在被引用处执行,这就是include的用法,是不是很简单,没错,include模块可以指定一个文件,这个文件中的内容是一个任务列表(一个或多个任务),当使用include模块引用对应的文件时,文件中的任务会在被引用处执行,就好像写在被引用处一样。
引用handlers
# cat test_include.yml
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- file:
path: /opt/ttt
state: touch
notify: test include handlers
handlers:
- name: test include handlers
include: include_handler.yml
# cat include_handler.yml
- debug:
msg: "task1 of handlers"
- debug:
msg: "task2 of handlers"
- debug:
msg: "task3 of handlers"
在一个playbook中引用另一个playbook
# cat lamp.yml
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- include: install_MysqlAndPhp.yml
- yum:
name: httpd
state: present
- include: lnmp.yml
调用参数
在使用"函数"或者"方法"时,可能会需要传入一些"参数",以便更加灵活的根据实际情况作出对应的处理
# cat test_include1.yml
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- include: in.yml
test_var1=hello
test_var2=test
# cat in.yml
- debug:
msg: "{{ test_var1 }}"
- debug:
msg: "{{ test_var2 }}"
如上例所示,在in.yml文件中一共有两个debug任务,这两个任务分别需要两个变量,在in.yml中并未定义任何变量,而是在test_include1.yml中使用include模块引用in.yml时,传入了两个参数,这两个参数的名字与变量名相同,执行上例playbook,可以看到in.yml中的两个任务都正常输出了,这就是向include文件传参的方法,是不是很容易,除了上述方法,我们还能够使用vars关键字,以key: value变量的方式传入参数变量,示例如下
tasks:
- include: in.yml
vars:
test_var1: hello
test_var2: test
通过vars关键字也能够传入结构稍微复杂的变量数据,以便在包含的文件中使用,示例如下
# cat test_include1.yml
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- include: in.yml
vars:
users:
bob:
gender: male
lucy:
gender: female
# cat in.yml
- debug:
msg: "{{ item.key}} is {{ item.value.gender }}"
loop: "{{ users | dict2items }}"
我们也可以对include添加条件判断,还可以对include进行循环操作
---
#a.yaml
- name: installation mysql
hosts: test3
gather_facts: no
tasks:
- include: b.yaml
when: 2 > 1
- include: b.yaml
loop:
- 1
- 2
- 3
#b.yaml
- debug:
msg: "task1 in in3.yml"
- debug:
msg: "task2 in in3.yml"
# B.yml
- debug:
msg: "{{item}}--task1 in B.yml"
- debug:
msg: "{{item}}--task2 in B.yml"
include_tasks
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "test task"
tags: t0
- include_tasks:
file: in.yml
apply:
tags: t1,always
tags: always
import_tasks
如果想要包含引用一个任务列表,也可以使用"import_tasks"关键字
例子
# cat intest1.yml
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "test task"
- import_tasks: in.yml
# cat in.yml
- debug:
msg: "task1 in in.yml"
- debug:
msg: "task2 in in.yml"
"import_tasks"和"include_tasks"到底有什么不同之处呢?它们的不同之处在于,"import_tasks"是静态的,"include_tasks"是动态的
如果想要对包含的任务列表进行循环操作,则只能使用"include_tasks"关键字,不能使用"import_tasks"关键字,"import_tasks"并不支持循环操作,
也就是说,使用"loop"关键字或"with_items"关键字对include文件进行循环操作时,只能配合"include_tasks"才能正常运行
import_playbook
在上篇文章中我们还提到,使用"include"关键字除了能够引用任务列表,还能够引用整个playbook,在之后的版本中,如果想要引入整个playbook,则需要使用"import_playbook"模块代替"include"模块,因为在2.8版本以后,使用"include"关键字引用整个playbook的特性将会被弃用
# cat intest6.yml
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "test task in intest6.yml"
- import_playbook: intest7.yml
# cat intest7.yml
---
- hosts: test70
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "test task in intest7.yml"