Aggregating debug msg output

Taking, e.g., the following simple playbook:

Hello,


  • hosts: all
    gather_facts: false
    serial: 1

tasks:

  • name: system date
    command: date
    register: date_out

  • name: print system date
    debug:
    msg: “Hostname is: {{ inventory_hostname }} and System date is: {{ date_out.stdout_lines[0] }}”

Rather than the msg output is interspersed thoughout, I’d like to aggregate all of the debug messages from the various hosts into consecutive lines, like this:

ok: [host1] => (item=Thu Sep 16 12:41:03 EDT 2021) => {
“msg”: “Hostname is: host1 and System date is: Thu Sep 16 12:41:03 EDT 2021”
}

ok: [host2] => (item=Thu Sep 16 12:41:03 EDT 2021) => {
“msg”: “Hostname is: host2 and System date is: Thu Sep 16 12:41:03 EDT 2021”
}

I hope I’m clear on my ask. Is this possible and, if so, how do I do it?

Thanks!

Not sure whether I’ll got the point. However, using a template file you could collect the fact sysdate on every host and print the hostname and all sysdate in a task delegated to localhost.

… additions to playbook …

  • set_fact: sysdate={{ date_out.stdout_lines[0] }}

  • name: dump to /tmp/DUMP
    template:
    src: dump.j2
    dest: /tmp/DUMP
    delegate_to: localhost

Template named “dump.j2” as referred in playbook. It’s located in same directory as the playbook.

{% for host in ansible_play_hosts %}
Hostname is: {{ host }} and System date is: {{ sysdate }}
{% endfor %}

There is also a possibility to loop over the hosts in a delegated_to task, but I don’t remember the syntax at the moment.

BR,
Roland

Thanks, Roland. The fact is, I’d like to “aggregate” (for lack of a better term) any debug msg output, regardless of what information the playbook is trying to gather. The one I posted above is for descriptive purposes. Rather than have output of the playbook look like this:

PLAY [all] *********************************************************************

TASK [system date] *************************************************************
Thursday 16 September 2021 11:49:07 -0400 (0:00:00.037) 0:00:00.037 ****
changed: [host1]

TASK [print system date] *******************************************************
Thursday 16 September 2021 11:49:08 -0400 (0:00:00.889) 0:00:00.927 ****
ok: [host1] => {
“msg”: “Hostname is: host1 and System date is: Thu Sep 16 11:49:08 EDT 2021”
}

PLAY [all] *********************************************************************

TASK [system date] *************************************************************

Save all your output to the file and assemble it using assemble module.

If I’m correctly understanding what it is you want I suggest that you look into the “throttle” keyword. It can be used for serial like behavior but on individual tasks or blocks.

//Oskar