Ansible API 2.3.2.0 标签已传递给 playbook 但没有 运行 带有标签的任务

Ansible API 2.3.2.0 tags passed to playbook but doesn't run task with tag

我成功通过了标签,因为剧本只是 运行我分配的任务 tags: always

分配我传递的标签的任务没有运行。

三个测试任务:

  - name: debug if tag was provided
    debug: var=tags
    tags: always

  - name: if tag restart was provided
    debug: var=tags
    tags: restart

  - name: if no tag was provided
    debug: var=tags

当我将 'restart' 作为标签传递时,只有第一个任务 运行,当没有提供标签时,所有三个任务 运行。我希望中间人在提供重启时 运行。

有没有办法查看剧本将什么视为标签?

调试的输出是:

{
    "tags": "VARIABLE IS NOT DEFINED!"
}

这是我通过 api

发送标签的方式
Options = namedtuple('Options', ['connection',  'forks', 'become', 'become_method', 'become_user', 'check',
                                 'listhosts', 'listtasks', 'listtags', 'syntax', 'module_path', 'diff', 'tags'])
options = Options(connection='ssh', forks=5, become=None, become_method=None, become_user=None, check=False,
                  listhosts=False, listtasks=False, listtags=False, syntax=False, module_path="", diff=True, tags=_tag)

PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager,
                            loader=loader, options=options, passwords=passwords)

_tag是一个字符串

always 是一个 special tag

There is a special always tag that will always run a task, unless specifically skipped (--skip-tags always)

如果您不希望第一个任务使用不同的标签名称 运行 如果您指定 restart 标签。

Is there a way to see what the playbook is seeing as the tag?

从 Ansible 2.5 开始(目前作为 GitHub 上的 devel 分支可用)您可以使用新的 magic variable ansible_run_tags.


例如 ansible-playbook playbook.yml --tags restart:

TASK [debug if tag was provided] ***************************************************************************************************************
ok: [localhost] => {
    "ansible_run_tags": [
        "restart"
    ]
}

TASK [if tag restart was provided] *************************************************************************************************************
ok: [localhost] => {
    "ansible_run_tags": [
        "restart"
    ]
}

当您不为ansible-playbook提供标签时,此变量的值为all:

TASK [debug if tag was provided] ***************************************************************************************************************
ok: [localhost] => {
    "ansible_run_tags": [
        "all"
    ]
}

TASK [if tag restart was provided] *************************************************************************************************************
ok: [localhost] => {
    "ansible_run_tags": [
        "all"
    ]
}

TASK [if no tag was provided] ******************************************************************************************************************
ok: [localhost] => {
    "ansible_run_tags": [
        "all"
    ]
}

看起来问题是我将标签作为字符串传递,而 API 将其作为列表读取。当我将标签设置为 python 集时,运行 没有问题

TASK = os.environ['TASK'].lower()
if TASK in authorized_tags:
    _tasks = 'none'
    _tag = {TASK}
    execute_playbook()