2

Is it possible to add a description for each task in a playbook?

I'm aware of the name keyword but I'd like to give a detailed description of the same. I'd use a callback plugin later to fetch the description of the task and use it for logging purposes.

Example:

- name: Perform X
  description: Perform X by doing A,B,C which is generally done for Y,Z
  module_name:
    module_parameter:
  ...

Is there any hack or workaround to do this?

U880D
  • 8,601
  • 6
  • 24
  • 40
Ehack2
  • 49
  • 2
  • 3
  • 2
    I like to understand the advantage of your requirement over an already available possibility like `-name: Perform X (Desc: By by doing A,B,C which is generally done for Y,Z)`, because you could fetch the name later too. – U880D Oct 08 '21 at 10:33
  • @U880D So basically I'm writing a role which audits the OS and writes it in a CSV file. I would like to have it in a title and description format rather than giving a multiline detailed name. Having such a big name would take a lot of space in the console and I'd have to parse it later to get the description. – Ehack2 Oct 08 '21 at 11:19

1 Answers1

2

Is there any hack or workaround to do this?

Yes, there is.

According How to do multiline shell script in Ansible

Ansible uses YAML syntax in its playbooks. YAML has a number of block operators ...

the | character will assign the value Show task name\nDescription: Provides a description of the task\n to the name key.

- name: |
    Show task name
    Descriptipon: Provides a description of the task
  debug:
    msg: "Hello World!"
  tags: multiline

resulting in an output of

TASK [Show task name
Description: Provides a description of the task] ********************************************************************************************************
ok: ...

In respect to the comment

I would like to have it in a title and description format ...

a construct of

- name: |
    Show task name]
    DESC [Provides a description of the task
  debug:
    msg: "Hello World!"
  tags: multiline

produce an output

TASK [Show task name]
DESC [Provides a description of the task] ********************************************************************************************************
ok: ...

... than giving a multiline detailed name. Having such a big name would take a lot of space in the console

But you are having still multiple lines which takes space in the console and in any case you have to parse it. This would also be true for your initial requirement.

U880D
  • 8,601
  • 6
  • 24
  • 40
  • 1
    Thank you! I think this works. Also it should be possible to write a callback plugin through which we can alter the way it prints on the console right? – Ehack2 Oct 08 '21 at 12:36
  • 1
    This is so cool! Thanks. I was looking for something exactly like what you described here. And it worked perfectly for me! – gjjhhh_ May 31 '23 at 03:24