在Ansible中使用include和import模块

时间:2020-01-09 10:34:03  来源:igfitidea点击:

在本节中,我们将使用include和import模块添加另一个任务或者将playbook完成到另一个playbook中。

包含模块

使用include模块将任务添加到playbook中

我们可以创建单独的yml文件,该文件只包含使用YAML语法的任务,YAML语法可以通过include模块在playbook中使用。我创建了这个"tasks-1.yml"文件,包含以下内容:

[ansible@controller ~]$ cat tasks-1.yml
--
 - name: Play 1 - Task 2
   debug:
     msg: Play 1 - Task 2
...

在这个例子中,我用"---"来标记剧本的开始,用"…"来标记剧本的结束,因为这个yml文件将包含在另一个剧本中,所以建议标记剧本的开始和结束

现在我有了父playbook include_tasks.yml它将包含我们的tasks-1.yml

[ansible@controller ~]$ cat include_tasks.yml
--
 - name: Working with include_tasks module
   hosts: localhost
   gather_facts: false
   tasks:
     - name: Play 1 - Task 1
       debug:
         msg: "Play 1 - Task 1"
     - include: tasks-1.yml

现在让我们来执行这个剧本:

[ansible@controller ~]$ ansible-playbook include_tasks.yml
PLAY [Working with include_tasks module] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ***
TASK [Play 1 - Task 1] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
ok: [localhost] => {
    "msg": "Play 1 - Task 1"
}
TASK [Play 1 - Task 2] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
ok: [localhost] => {
    "msg": "Play 1 - Task 2"
}
PLAY RECAP ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

因此,两个任务,即从"tasks-1.yml"中包含的任务和作为剧本一部分的另一个任务都成功地执行了。如果我们注意到"output is not showing any indications that any include was used"或者所有任务都是剧本的一部分。

使用include模块将一个完整的剧本添加到另一个剧本中

Playbook文件可以包括其他整个Playbook文件。这种结构有助于将几个独立的剧本组合成一个更大、更全面的剧本。Playbook包含比任务包含更原始一些。包含剧本时不能执行变量替换,不能应用条件,也不能应用标记。要包含的剧本文件在执行时也必须存在。

其中:我创建了一个新的剧本包括_剧本.yml它使用include模块导入另一个剧本,即"play2.yml",并使用include添加另一个剧本

说明:

注意语法,我们定义了空的"-"(连字符),它表示playbook包含一个列表项,其中当前playbook是列表项之一,而我们使用另一个连字符在playbook的末尾添加了另一个列表项。

--
# The minus in YAML indicates a list item. The playbook contains
# a list of plays, with each play being a dictionary

   name: include playbook
   hosts: localhost
   gather_facts: false
   tasks:
     - name: Play 1 - Task 1
       debug:
         msg: "Play 1 - Task 1"
     - include: tasks-1.yml
- include: play2.yml
...

提示:

在Ansible 2.4之前,playbook包含是使用include关键字实现的,但是,这一点已被弃用,将在Ansible 2.8中删除,因此不应使用它

这是"play2.yml"的内容`

[ansible@controller ~]$ cat play2.yml
--
 - name: Play 2
   hosts: localhost
   gather_facts: false
   tasks:
     - name: Play 2 - Task 1
       debug:
         msg: "Play 2 - Task 1"
...

现在我们将执行"include"_剧本.yml`剧本:

[ansible@controller ~]$ ansible-playbook include_playbook.yml
[DEPRECATION WARNING]: 'include' for playbook includes. You should use 'import_playbook' instead. This feature will
be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in
ansible.cfg.
PLAY [include playbook] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ****
TASK [Play 1 - Task 1] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
ok: [localhost] => {
    "msg": "Play 1 - Task 1"
}
TASK [Play 1 - Task 2] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
ok: [localhost] => {
    "msg": "Play 1 - Task 2"
}
PLAY [Play 2] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **
TASK [Play 2 - Task 1] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
ok: [localhost] => {
    "msg": "Play 2 - Task 1"
}
PLAY RECAP ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

我们收到一个不推荐使用的警告,因为不再推荐使用include playbook,我们应该改用"import_playbook"模块。但是playbook已经成功地执行了,来自这两个PLAY的任务都已经执行了。

包含\ \u任务模块

接下来,我们将使用同一组playbook和tasks-1.yml文件分析"include_tasks"模块。但我们将把include替换为"include_tasks"module inside"include"_任务.yml`剧本

--
 - name: Working with include_tasks module
   hosts: localhost
   gather_facts: false
   tasks:
     - name: Play 1 - Task 1
       debug:
         msg: "Play 1 - Task 1"
     - include_tasks: tasks-1.yml

现在让我们来执行这个剧本:

[ansible@controller ~]$ ansible-playbook include_tasks.yml
PLAY [Working with include_tasks module] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ***
TASK [Play 1 - Task 1] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
ok: [localhost] => {
    "msg": "Play 1 - Task 1"
}
TASK [include_tasks] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** ***
included: /home/ansible/tasks-1.yml for localhost
TASK [Play 1 - Task 2] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
ok: [localhost] => {
    "msg": "Play 1 - Task 2"
}
PLAY RECAP ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

从输出中可以看到,include任务被算作另一个任务,总共执行了3个任务,包括一个包含的任务。任务2通知操作员由playbook-include模块执行的"include_tasks"活动。

导入任务并导入剧本模块

现在在本节中,我们将在同一重头戏中使用"import_tasks"和"import_playbook"模块。早些时候,我们在使用include添加playbook时收到了一个不推荐使用的警告,因此我们将使用相同的示例,但这次使用"import_tasks"和"import_playbook":

--
# The minus in YAML indicates a list item. The playbook contains
# a list of plays, with each play being a dictionary

   name: import playbook
   hosts: localhost
   gather_facts: false
   tasks:
     - name: Play 1 - Task 1
       debug:
         msg: "Play 1 - Task 1"
     - import_tasks: tasks-1.yml
- import_playbook: play2.yml
...

你可以检查我们的import_playbook.yml文件,其中我使用相同的列表格式,添加了一个连字符,然后在末尾添加了"import_playbook",以便在同一个play中导入"play2.yml"。我们还使用"import_tasks"来导入PLAY-1中tasks-1.yml的内容

让我们执行这个剧本并检查输出:

[ansible@controller ~]$ ansible-playbook import_playbook.yml
PLAY [import playbook] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
TASK [Play 1 - Task 1] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
ok: [localhost] => {
    "msg": "Play 1 - Task 1"
}
TASK [Play 1 - Task 2] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
ok: [localhost] => {
    "msg": "Play 1 - Task 2"
}
PLAY [Play 2] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **
TASK [Play 2 - Task 1] ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
ok: [localhost] => {
    "msg": "Play 2 - Task 1"
}
PLAY RECAP ** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** *****
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

因此,这次不再发出警告,PLAYBOOK中的所有任务都已成功执行。对于执行的导入操作,我们也没有看到任何其他任务。