Ansible 回调插件:如何获取其中包含变量的任务名称? [英] Ansible callback plugin: how to get a task name with variables in it expanded?

查看:22
本文介绍了Ansible 回调插件:如何获取其中包含变量的任务名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个游戏,其中包含一个名称中包含变量的任务.

I have a play below that contains a task with a name that has a variable in it.

hosts:        localhost
connection:   local
vars:
  a_variable: test
tasks:
  - name:     this is a task to echo {{ a_variable }}
    shell:    echo {{ a_variable }}

当它在命令行上运行时,标准输出显示任务名称,变量解析/扩展为它的值,即这是一个回显测试的任务

When this is run on the command line, the stdout shows the task name with variable resolved/expanded to its value which is this is a task to echo test

但是,当我尝试使用属性 task.name 或 result._task.name 访问回调插件中的任务名称时,返回的名称是 this is a task to echo {{ a_variable }} 变量未扩展的地方.

However, when I try to access the task name in the callback plugin using the properties task.name or result._task.name, the name returned is this is a task to echo {{ a_variable }} where the variable is not expanded.

如何获取打印在标准输出上并扩展变量的任务名称?

How can I get the task name as it is printed on stdout with the variable expanded?

推荐答案

模板化的任务名称仅在 v2_playbook_on_task_start 中可用.

Templated task name is only available inside v2_playbook_on_task_start.

如果您想在其他方法中使用模板化名称,您应该创建插件范围的列表/字典,在 v2_playbook_on_task_start 期间填充任务的名称,并在需要时稍后获取.

If you want to use templated name in other methods, you should create plugin-wide list/dict, populate tasks' names during v2_playbook_on_task_start and fetch it later, when needed.

最简单的方法是使用 task._uuid 作为参考(参见 profile_tasks 插件为例.

The simplest way to accomplish this is to use task._uuid as reference (see profile_tasks plugin as example.

这篇关于Ansible 回调插件:如何获取其中包含变量的任务名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆