ansible学习之include介绍

为了避免重复写相同功能的yaml,我们可以把相同功能的任务单写出来,然后再去引用。类似于程序语言中的函数,使用的时候调用出来。ansible里通过include调用。

1.include

在一个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

handlers中也可以使用include
handlers:
  - name: test include handlers
    include: include_handler.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 }}"

include结合  loop_control: loop_var: outer_item 可以展示外层循环的值
# cat A.yml
---
- hosts: test70
  remote_user: root
  gather_facts: no
  tasks:
  - include: B.yml
    loop:
    - 1
    - 2
    loop_control:
      loop_var: outer_item
 
# cat B.yml
- debug:
    msg: "{{outer_item}}--{{item}}--task in B.yml"
  loop:
  - a
  - b
  - c

 

2.include_tasks

include 基于tag执行的话 会执行第二个yaml里面的内容,include_tasks不会执行第二个yaml里面的内容。
---
- hosts: 192.168.10.20
  remote_user: root
  gather_facts: no
  tasks:
    - debug:  
        msg: "task1"
    - include_tasks: 
        file: in.yaml
      tags: t1
    - debug:
        msg: "task_last"

cat in.yaml 
- debug: 
    msg: "task1 in in.yaml"
- debug:
    msg: "task2 in in.yaml"

[root@ansible ansible]# ansible-playbook intest.yaml --tags t1

PLAY [192.168.10.20] *****************************************************************************************************************************************************************************************

TASK [include_tasks] *****************************************************************************************************************************************************************************************
included: /etc/ansible/in.yaml for 192.168.10.20

PLAY RECAP ***************************************************************************************************************************************************************************************************
192.168.10.20              : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
标签只会对"include_tasks"任务本身生效,而不会对其中包含的任务生效。,要是想执行加载的yaml里面的任务的话,需要用apply参数

[root@ansible ansible]# cat intest2.yaml
---
- hosts: 192.168.10.20
  remote_user: root
  gather_facts: no
  tasks:
  - debug: msg="thisis task1"
  - include_tasks:
         file: in.yaml
         apply: 
          tags: 
           - t1  
    tags: always 
  - debug: msg="task3" 

结果:
[root@ansible ansible]# ansible-playbook intest2.yaml --tags t1

TASK [include_tasks] *****************************************************************************************************************************************************************************************
included: /etc/ansible/in.yaml for 192.168.10.20

TASK [debug] *************************************************************************************************************************************************************************************************
ok: [192.168.10.20] => {
    "msg": "task1 in in.yaml"
}

TASK [debug] *************************************************************************************************************************************************************************************************
ok: [192.168.10.20] => {
    "msg": "task2 in in.yaml"
}

如上结果可得,当使用include_tasks引用其他yaml,使用tag时,要想tag里面引用的任务野被执行的话,需使用apply结合always(tags声明always)才可以。

3.import_tasks

"import_tasks"模块并不会像"include_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的文件在playbook被加载时就展开了(是预处理的)。

"动态"的意思就是被include的文件在playbook运行时才会被展开(是实时处理的)。

由于"include_tasks"是动态的,所以,被include的文件的文件名可以使用任何变量替换。

由于"import_tasks"是静态的,所以,被include的文件的文件名不能使用动态的变量替换。

如果想要对包含的任务列表进行循环操作,则只能使用"include_tasks"关键字。

当对"include_tasks"使用when进行条件判断时,when对应的条件只会应用于"include_tasks"任务本身,当执行被包含的任务时,不会对这些被包含的任务重新进行条件判断。

当对"import_tasks"使用when进行条件判断时,when对应的条件会应用于被include的文件中的每一个任务,当执行被包含的任务时,会对每一个被包含的任务进行同样的条件判断。

4.import_playbook

使用"include"关键字除了能够引用任务列表,还能够引用整个playbook,在之后的版本中,如果想要引入整个playbook,则需要使用"import_playbook"模块代替"include"模块,因为在2.8版本以后,使用"include"关键字引用整个playbook的特性将会被弃用。
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: