Ansible回调插件:如何获取扩展了任务的任务名称? [英] Ansible callback plugin: how to get a task name with variables in it expanded?
问题描述
下面有一个剧本,其中包含一个任务,该任务的名称中包含变量.
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 }}
在命令行上运行此命令时,stdout将显示任务名称,其中变量已解析/扩展为其值,即this is a task to echo test
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.
The simplest way to accomplish this is to use task._uuid
as reference (see profile_tasks plugin as example.
这篇关于Ansible回调插件:如何获取扩展了任务的任务名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!