參考文章:http://www.zsythink.net/archives/2962
通過include,我們可以在一個(gè)playbook中包含另一個(gè)文件,以便實(shí)現(xiàn)我們剛才所描述的效果,這篇文章我們就來了解一下"include"的用法
# cat install_MysqlAndPhp.yml
- yum:
name: mysql
state: present
- yum:
name: php-fpm
state: present
如上例所示,兩個(gè)task被提取到了install_MysqlAndPhp.yml文件中,當(dāng)我們需要安裝mysql和php-fpm時(shí),只需要調(diào)用此yml文件即可,那么怎樣調(diào)用這個(gè)文件呢?方法如下,我們只要把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文件,當(dāng)我們引用此文件時(shí),install_MysqlAndPhp.yml文件中的tasks都會(huì)在被引用處執(zhí)行,這就是include的用法,是不是很簡單,沒錯(cuò),include模塊可以指定一個(gè)文件,這個(gè)文件中的內(nèi)容是一個(gè)任務(wù)列表(一個(gè)或多個(gè)任務(wù)),當(dāng)使用include模塊引用對應(yīng)的文件時(shí),文件中的任務(wù)會(huì)在被引用處執(zhí)行,就好像寫在被引用處一樣。
引用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"
在一個(gè)playbook中引用另一個(gè)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
調(diào)用參數(shù)
在使用"函數(shù)"或者"方法"時(shí),可能會(huì)需要傳入一些"參數(shù)",以便更加靈活的根據(jù)實(shí)際情況作出對應(yīng)的處理
# 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文件中一共有兩個(gè)debug任務(wù),這兩個(gè)任務(wù)分別需要兩個(gè)變量,在in.yml中并未定義任何變量,而是在test_include1.yml中使用include模塊引用in.yml時(shí),傳入了兩個(gè)參數(shù),這兩個(gè)參數(shù)的名字與變量名相同,執(zhí)行上例playbook,可以看到in.yml中的兩個(gè)任務(wù)都正常輸出了,這就是向include文件傳參的方法,是不是很容易,除了上述方法,我們還能夠使用vars關(guān)鍵字,以key: value變量的方式傳入?yún)?shù)變量,示例如下
tasks:
- include: in.yml
vars:
test_var1: hello
test_var2: test
通過vars關(guān)鍵字也能夠傳入結(jié)構(gòu)稍微復(fù)雜的變量數(shù)據(jù),以便在包含的文件中使用,示例如下
# 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進(jìn)行循環(huán)操作
---
#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
如果想要包含引用一個(gè)任務(wù)列表,也可以使用"import_tasks"關(guān)鍵字
例子
# 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"是靜態(tài)的,"include_tasks"是動(dòng)態(tài)的
如果想要對包含的任務(wù)列表進(jìn)行循環(huán)操作,則只能使用"include_tasks"關(guān)鍵字,不能使用"import_tasks"關(guān)鍵字,"import_tasks"并不支持循環(huán)操作,
也就是說,使用"loop"關(guān)鍵字或"with_items"關(guān)鍵字對include文件進(jìn)行循環(huán)操作時(shí),只能配合"include_tasks"才能正常運(yùn)行
import_playbook
在上篇文章中我們還提到,使用"include"關(guān)鍵字除了能夠引用任務(wù)列表,還能夠引用整個(gè)playbook,在之后的版本中,如果想要引入整個(gè)playbook,則需要使用"import_playbook"模塊代替"include"模塊,因?yàn)樵?.8版本以后,使用"include"關(guān)鍵字引用整個(gè)playbook的特性將會(huì)被棄用
# 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"