# Logging output from multiple hosts in an email body

**URL:** <https://forum.ansible.com/t/logging-output-from-multiple-hosts-in-an-email-body/33901>\
**Category:** Ansible Project\
**Created:** [September 26, 2020, 3:21am UTC](https://forum.ansible.com/t/logging-output-from-multiple-hosts-in-an-email-body/33901 "2020-09-26T03:21:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkl](https://avatars.discourse-cdn.com/v4/letter/r/e79b87/32.png) [@rkl](https://forum.ansible.com/u/rkl)\
**Post date:** [September 26, 2020, 3:21am UTC](https://forum.ansible.com/t/logging-output-from-multiple-hosts-in-an-email-body/33901/1 "2020-09-26T03:21:46Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![P\_R\_Music\_video\_s](https://avatars.discourse-cdn.com/v4/letter/p/eb8c5e/32.png) [@P\_R\_Music\_video\_s](https://forum.ansible.com/u/P_R_Music_video_s)\
**Post date:** [September 26, 2020, 6:45am UTC](https://forum.ansible.com/t/logging-output-from-multiple-hosts-in-an-email-body/33901/2 "2020-09-26T06:45:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 26, 2020, 9:27am UTC](https://forum.ansible.com/t/logging-output-from-multiple-hosts-in-an-email-body/33901/3 "2020-09-26T09:27:23Z")

</div>

> 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  
> &nbsp;&nbsp;&nbsp;&nbsp;shell: /u01/bin/emctl start agent  
> &nbsp;&nbsp;&nbsp;&nbsp;become: true  
> &nbsp;&nbsp;&nbsp;&nbsp;become\_user: ema  
> &nbsp;&nbsp;&nbsp;&nbsp;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

&nbsp;&nbsp;&nbsp;&nbsp;- mail:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body: \>-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ dict(ansible\_play\_hosts|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zip(ansible\_play\_hosts|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('extract', hostvars, 'emSt')))|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to\_nice\_yaml }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

---

<div class="post-metadata">

**Author:** ![rkl](https://avatars.discourse-cdn.com/v4/letter/r/e79b87/32.png) [@rkl](https://forum.ansible.com/u/rkl)\
**Post date:** [October 2, 2020, 8:44pm UTC](https://forum.ansible.com/t/logging-output-from-multiple-hosts-in-an-email-body/33901/4 "2020-10-02T20:44:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 3, 2020, 6:34pm UTC](https://forum.ansible.com/t/logging-output-from-multiple-hosts-in-an-email-body/33901/5 "2020-10-03T18:34:18Z")

</div>

> 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

&nbsp;&nbsp;&nbsp;&nbsp;- mail:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body: \>-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ dict(ansible\_play\_hosts|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zip(ansible\_play\_hosts|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('extract', hostvars, 'emSt')))|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to\_nice\_yaml }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
--- snap ---

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![rkl](https://avatars.discourse-cdn.com/v4/letter/r/e79b87/32.png) [@rkl](https://forum.ansible.com/u/rkl)\
**Post date:** [October 3, 2020, 7:46pm UTC](https://forum.ansible.com/t/logging-output-from-multiple-hosts-in-an-email-body/33901/6 "2020-10-03T19:46:41Z")

</div>

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.
