Aggregating debug msg output

46 views
Skip to first unread message

Dimitri Yioulos

unread,
Sep 16, 2021, 1:26:27 PM9/16/21
to Ansible Project
Taking, e.g., the following simple playbook:

---

- 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!

Roland Müller

unread,
Sep 16, 2021, 3:46:29 PM9/16/21
to Ansible Project
Hello,

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
 

Dimitri Yioulos

unread,
Sep 16, 2021, 4:16:14 PM9/16/21
to Ansible Project
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] *************************************************************
---
Thursday 16 September 2021  11:49:08 -0400 (0:00:00.059)       0:00:00.986 ****
changed: [host2]

TASK [print system date] *******************************************************
Thursday 16 September 2021  11:49:09 -0400 (0:00:00.965)       0:00:01.951 ****
ok: [host2] => {
    "msg": "Hostname is: host2 and System date is: Thu Sep 16 11:49:09 EDT 2021"
}

PLAY RECAP *********************************************************************
~

I'd want the to look like this:

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

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

Thursday 16 September 2021  11:49:07 -0400 (0:00:00.037)       0:00:00.037 ****
changed: [host2]

TASK [print system date] *******************************************************
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"
}

PLAY RECAP *********************************************************************
~

Is that possible?

rajthecomputerguy

unread,
Sep 17, 2021, 12:34:15 AM9/17/21
to Ansible Project
Save all your output to the file and assemble it using assemble module. 

Oskar Almlöv

unread,
Sep 17, 2021, 2:06:00 AM9/17/21
to ansible...@googlegroups.com
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

From: ansible...@googlegroups.com <ansible...@googlegroups.com> on behalf of rajthecomputerguy <rajtheco...@gmail.com>
Sent: Friday, September 17, 2021 6:34:15 AM
To: Ansible Project <ansible...@googlegroups.com>
Subject: [ansible-project] Re: Aggregating debug msg output
 
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/49650888-ea0f-4de5-95bf-17eea22953c3n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages