在播放过程中不识别可见标签?


$ ansible --version
ansible 2.10.8

我有一个叫做site.yml的剧本,它只包含其他剧本。有些剧本,我只希望ron当我使用--tags update标签,但有些剧本,我想运行所有的时间,但只有某些任务标记适当。在下面的示例中,我希望prepare-update.yml剧本上的所有任务仅在update标签上运行,而我想一直运行postgresql.yml剧本。然后我想在我的postgresql角色

中控制一些任务是否运行。
- import_playbook: prepare-update.yml
tags: update
- import_playbook: postgresql.yml

posgresql.yml剧本只包含一个postgresql角色

---
- hosts: "{{ var_host | default('exa_site') }}"
become_method: runas
roles:
- postgresql

现在我的postgresql角色的tasks/main.yml

---
# tasks file for postgresql
- include_tasks: install_postgresql.yml
- include_tasks: run_postgresql_queries.yml
- include_tasks: install_pgadmin4.yml

和我的install_postgresql.yml任务有这些任务。正如你所看到的,我使用了3个不同的标签,即new_install,updateuninstall

- block:
- name: Download app
win_get_url: ...
- name: Install app
win_shell: ...
tags: new_install
- name: Start service
win_service: ...
tags:
- new_install
- update
- block:
- name: Stop service
win_service: ...
- name: Delete service
win_file: ...
tags: uninstall

当我运行以下命令时,我希望一些任务是基于它们的标签运行的。

$ ansible-playbook -i hosts.yml -e var_host=my_host --tags new_install site.yml

然而,我只是得到这个,没有运行。

PLAY [10.227.x.x] *******************************************************
TASK [Gathering Facts] **************************************************
ok: [10.227.x.x]
PLAY RECAP **************************************************************
10.227.x.x               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

为什么我的任务标记为new_install根本没有运行?

这是因为您的include_tasks被跳过,因为它们不包含任何与new_install匹配的标记。

如果你不想在include_tasks上重复你的低级标签,你可以做的是用特殊的标签always来标记它们。

那么,main。角色的Yml任务变为:

## Using a block, so we can tag them all at once.
- block:
- include_tasks: install_postgresql.yml
- include_tasks: run_postgresql_queries.yml
- include_tasks: install_pgadmin4.yml
tags: always
另一种方法是导入因为这是import_*include_*模块之间的主要区别。

还是main。角色的Yml任务更改为:

- import_tasks: install_postgresql.yml
- import_tasks: run_postgresql_queries.yml
- import_tasks: install_pgadmin4.yml

注意两个模块简介中措辞的不同:

  • import_tasks:导入要添加到当前剧本以供后续执行的任务列表。<一口>
  • include_tasks:包含一个文件,其中包含当前剧本中要执行的任务列表。<一口>

因此,当一个"盲目地"导入以进一步执行时,另一个包含它们并同时运行它们。
考虑到这一点,如果缺少适当的标记,后者include_tasks将被跳过,这是有意义的,当第一个将把任务放在剧本中,然后,在剧本执行时,如果您期望的话,将对导入任务上的标记进行评估。

(解决方案在另一个答案中描述为@β.εηοιτ.βε)

要理解这个问题的逻辑,有两点很重要:

  1. 引用自include:动态重用

"可行的过程包括文件和角色,因为它们出现在剧本中。">

  1. 引用自

"…include_*任务上的标记仅应用于包含本身,而不适用于包含的文件或角色中的任何任务。">

因此,当声明——tags时,(在命令行或其他地方)如果标签(在此include_*语句中声明)不匹配,则播放将跳过任何include_*语句。换句话说,在这种情况下,戏剧在到达include语句之前不知道include语句中有什么,并且永远不会知道,因为它将被跳过。

最新更新