Logging output from multiple hosts in an email body

Hi All, I am new to Ansible. I have this small task in a playbook that will be run against a bunch of linux servers.

  • name: emSt
    shell: /u01/bin/emctl start agent
    become: true
    become_user: ema
    register: emSt

Once the task is run against all the hosts, how can I log {{ansible_hostname}} and a successful message (wherever the task completed successfully) in the playbook and send that in a single email body? Thanks!

put {{inventory_hostname}} to get the name of the server in the body and {{rigister variable}} to send the standard output of the task

Hi All, I am new to Ansible. I have this small task in a playbook that will
be run against a bunch of linux servers.

- name: emSt
    shell: /u01/bin/emctl start agent
    become: true
    become_user: ema
    register: emSt

Once the task is run against all the hosts, how can I log
{{ansible_hostname}} and a successful message (wherever the task completed
successfully) in the playbook and send that in a single email body? Thanks!

Try this. The body is a dictionary of all hosts and 'emSt' values.
Fit the attributes to your needs

    - mail:
        host: localhost
        body: >-
          {{ dict(ansible_play_hosts|
                  zip(ansible_play_hosts|
                      map('extract', hostvars, 'emSt')))|
             to_nice_yaml }}
      delegate_to: localhost
      run_once: true

Thanks! However, that sends multiple emails from the playbook which is not what we are looking for. We expect to get a single summary email from playbook (after tasks are executed) with all the {{ inventory_hostname}} in the body of the email or a file containing the {{ inventory_hostname}} as an attachment to the email.

Thanks! However, that sends multiple emails from the playbook which is not what we are looking for. We expect to get a
single summary email from playbook (after tasks are executed) with all the {{ inventory_hostname}} in the body of the
email or a file containing the {{ inventory_hostname}} as an attachment to the email.

You better use the advice from Vladimir Botka:

--- snip ---
Try this. The body is a dictionary of all hosts and 'emSt' values.
Fit the attributes to your needs

    - mail:
        host: localhost
        body: >-
          {{ dict(ansible_play_hosts|
                  zip(ansible_play_hosts|
                      map('extract', hostvars, 'emSt')))|
             to_nice_yaml }}
      delegate_to: localhost
      run_once: true
--- snap ---

Regards
         Racke

Thanks Racke! As mentioned earlier, the emSt variable doesn’t contain any hostname details in it, so not sure if we can extract that from Vladimir’s sample code. Instead of using the emSt variable in the mail body, can we have the name of all the hosts (where the prior Ansible task was completed to start the OEM agent) and a message saying that “EM agent is started successfully” on the body of the summary email? Something like “EM agent is successfully started on {{ ansible_hostname}}” for all the hosts where the prior playbook task was executed.