Ansible callback plugin: how to get a task name wi

2019-07-11 03:32发布

问题:

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

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?

回答1:

Templated task name is only available inside 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