# Sending email with results

**URL:** <https://forum.ansible.com/t/sending-email-with-results/33812>\
**Category:** Ansible Project\
**Created:** [September 15, 2020, 11:54am UTC](https://forum.ansible.com/t/sending-email-with-results/33812 "2020-09-15T11:54:48Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sandy\_Toshev](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Sandy\_Toshev](https://forum.ansible.com/u/Sandy_Toshev)\
**Post date:** [September 15, 2020, 11:54am UTC](https://forum.ansible.com/t/sending-email-with-results/33812/1 "2020-09-15T11:54:48Z")

</div>

Hi guys,

So I have been using the following simple playbook to keep a folder clean from files. Everything is working fine. However, I got into thinking that it will not hurt, if I get a notification, containing what has been deleted.  
So my question is - may I use the variable into a body of an email? I have tried to view the contents of the registered variable, and I cannot seem to find a way.  
How can I configure an email that will have something like "I have deleted the following files - file1, file2, etc.?

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 15, 2020, 12:14pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/2 "2020-09-15T12:14:31Z")

</div>

Use [https://docs.ansible.com/ansible/latest/modules/mail\_module.html](https://docs.ansible.com/ansible/latest/modules/mail_module.html)  
and set the body argument to an inline jinja template, then iterate  
over your "files\_to\_delete.files" variable.

- name: Sending an email  
&nbsp;&nbsp;mail:  
&nbsp;&nbsp;&nbsp;&nbsp;body: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Deleted some files - {% for f in files\_to\_delete.files %}f, {% endfor %}

etc etc

---

<div class="post-metadata">

**Author:** ![Sandy\_Toshev](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Sandy\_Toshev](https://forum.ansible.com/u/Sandy_Toshev)\
**Post date:** [September 15, 2020, 12:36pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/3 "2020-09-15T12:36:30Z")

</div>

Thank you for the fast response,

So I added the following at the end of the .yml file, but I guess I do not know the correct syntax, because I get an error.  
#added from suggestion

- name: Sending an email about it  
mail:  
body: |  
Deleted some files - {% for f in files\_to\_delete.files %}f, {% endfor %}

---

<div class="post-metadata">

**Author:** ![timothy\_pember](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@timothy\_pember](https://forum.ansible.com/u/timothy_pember)\
**Post date:** [September 15, 2020, 12:40pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/4 "2020-09-15T12:40:52Z")

</div>

You could generate the app specific password for gmail and use it to try again.

Thanks

---

<div class="post-metadata">

**Author:** ![Sandy\_Toshev](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Sandy\_Toshev](https://forum.ansible.com/u/Sandy_Toshev)\
**Post date:** [September 15, 2020, 12:45pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/5 "2020-09-15T12:45:39Z")

</div>

I do not understand your suggestion. I am using my gmail account password in the .yml file. Am I supposed to use some other?

---

<div class="post-metadata">

**Author:** ![timothy\_pember](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@timothy\_pember](https://forum.ansible.com/u/timothy_pember)\
**Post date:** [September 15, 2020, 12:57pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/6 "2020-09-15T12:57:51Z")

</div>

Believe me, I work for google (not gmail). Google generally doesn’t permit you to use the password directly. You must generate an app password for the third party application.

Thanks

---

<div class="post-metadata">

**Author:** ![Sandy\_Toshev](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Sandy\_Toshev](https://forum.ansible.com/u/Sandy_Toshev)\
**Post date:** [September 15, 2020, 1:32pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/7 "2020-09-15T13:32:50Z")

</div>

Well, tried it (generated an app password) but still getting the same error. I cannot seem to send any email via Ansible - tried just a simple one (no the loop that was suggested) without success. Tried my local mail-server as sender - again no success.  
Also in the app password page of my google account, I see that “last used” column is empty - as if I have never used the pass.  
Any suggestions?

---

<div class="post-metadata">

**Author:** ![timothy\_pember](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@timothy\_pember](https://forum.ansible.com/u/timothy_pember)\
**Post date:** [September 15, 2020, 1:35pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/8 "2020-09-15T13:35:47Z")

</div>

Can you configure the credentials with a regular mail client such as thunderbird to see if your gmail SMTP works correctly?

Thanks

---

<div class="post-metadata">

**Author:** ![Luca\_remix\_tj\_Lorenz](https://avatars.discourse-cdn.com/v4/letter/l/ea666f/32.png) [@Luca\_remix\_tj\_Lorenz](https://forum.ansible.com/u/Luca_remix_tj_Lorenz)\
**Post date:** [September 15, 2020, 1:41pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/9 "2020-09-15T13:41:45Z")

</div>

Hello,

you're missing an indentation on the line after

body: |

Luca

---

<div class="post-metadata">

**Author:** ![Sandy\_Toshev](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Sandy\_Toshev](https://forum.ansible.com/u/Sandy_Toshev)\
**Post date:** [September 15, 2020, 1:43pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/10 "2020-09-15T13:43:15Z")

</div>

🙂 - was just trying that - and my gmail works fine both with my password and with my app password. It is something Ansible related - I am digging into the 1st line of the error - ERROR! couldn’t resolve module/action ‘mail’. This often indicates a misspelling, missing collection, or incorrect module **path**.  
Maybe something’s wrong with the mail module. Looking on how to check instllation/view modules, etc.

---

<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:** [September 15, 2020, 1:45pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/11 "2020-09-15T13:45:22Z")

</div>

> Thank you for the fast response,
> 
> So I added the following at the end of the .yml file, but I guess I do not know the correct syntax, because I get an error.  
> #added from suggestion  
> - name: Sending an email about it  
> mail:  
> body: |  
> Deleted some files - {% for f in files\_to\_delete.files %}f, {% endfor %}  
> subject: Deleted files overview  
> host: smtp.gmail.com  
> port: 587  
> username: my\_user\_name@gmail.com  
> password: my\_very\_secure\_password  
> to: my\_private\_username@my\_private\_domain
> 
> ===  
> The error is as follows:  
> ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:  
> JSON: Expecting value: line 1 column 1 (char 0)
> 
> Syntax Error while loading YAML.  
> could not find expected ':'

Your indendation is wrong.

It should be like:

&nbsp;&nbsp;mail:  
&nbsp;&nbsp;&nbsp;&nbsp;body: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Deleted some files - {% for f in files\_to\_delete.files %}f, {% endfor %}

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

---

<div class="post-metadata">

**Author:** ![Sandy\_Toshev](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Sandy\_Toshev](https://forum.ansible.com/u/Sandy_Toshev)\
**Post date:** [September 15, 2020, 2:13pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/12 "2020-09-15T14:13:50Z")

</div>

Thanks for all the help, but I cannot seem to get it to work… below is my .yml file… and the error. If anyone feels to help, please do 🙂 thanks in advance.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 15, 2020, 2:16pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/13 "2020-09-15T14:16:28Z")

</div>

Please use the EXACT code I sent in my first message.  
As Stefan pointed out your indentation is wrong.  
FYI indentation means the number of spaces, which matters.  
And very likely the Gmail as well - but that has been said enough  
times now I think.

---

<div class="post-metadata">

**Author:** ![Luca\_remix\_tj\_Lorenz](https://avatars.discourse-cdn.com/v4/letter/l/ea666f/32.png) [@Luca\_remix\_tj\_Lorenz](https://forum.ansible.com/u/Luca_remix_tj_Lorenz)\
**Post date:** [September 15, 2020, 2:17pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/14 "2020-09-15T14:17:46Z")

</div>

Hello,

seems like you're not reading the suggestions we sent you. So read  
this accurately:

&nbsp;&nbsp;- name: Sending mail  
&nbsp;&nbsp;&nbsp;&nbsp;mail:  
&nbsp;&nbsp;&nbsp;&nbsp;host: smtp.gmail.com  
&nbsp;&nbsp;&nbsp;&nbsp;port: 587  
&nbsp;&nbsp;&nbsp;&nbsp;username: my\_user\_name@gmail.com  
&nbsp;&nbsp;&nbsp;&nbsp;password: my\_pass  
&nbsp;&nbsp;&nbsp;&nbsp;body: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sender: my\_user\_name@gmail.com  
&nbsp;&nbsp;&nbsp;&nbsp;Deleted some files - {% for f in files\_to\_delete.files %}f, {% endfor %}

as you can notice, sender is indented by \*two spaces\*. That's the  
error ansible is reporting you. Your playbook is an invalid yaml,  
that's why doesn't work.

Luca

---

<div class="post-metadata">

**Author:** ![Sandy\_Toshev](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Sandy\_Toshev](https://forum.ansible.com/u/Sandy_Toshev)\
**Post date:** [September 15, 2020, 2:30pm UTC](https://forum.ansible.com/t/sending-email-with-results/33812/15 "2020-09-15T14:30:38Z")

</div>

Thanks again,

I read them all and tried different _spaces_ configs still nothing.  
OK, you have done enough (and more) - so I will continue reading on yaml syntax and get back to sending mails with Ansible.

---

<div class="post-metadata">

**Author:** ![Sandy\_Toshev](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Sandy\_Toshev](https://forum.ansible.com/u/Sandy_Toshev)\
**Post date:** [September 18, 2020, 6:18am UTC](https://forum.ansible.com/t/sending-email-with-results/33812/16 "2020-09-18T06:18:35Z")

</div>

Final resolution:  
As I suspected the main problem was missing “mail” module. I am a fan of the gentoo distribution, and I compile everything from source, but this time, the package was incomplete (even with the proper use flags). Ansible is re-compiled and mails are sent now…

@Dick Visser - if you still feel like helping, may you please suggest something about your script. What output I am getting - if I have 4 files deleted, I receive an email with a body like that: “f, f, f, f”. If I have no files deleted - empty body.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 18, 2020, 6:29am UTC](https://forum.ansible.com/t/sending-email-with-results/33812/17 "2020-09-18T06:29:45Z")

</div>

ah yes, that f should be in double brackets:

{{ f }}

---

<div class="post-metadata">

**Author:** ![Sandy\_Toshev](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Sandy\_Toshev](https://forum.ansible.com/u/Sandy_Toshev)\
**Post date:** [September 18, 2020, 7:04am UTC](https://forum.ansible.com/t/sending-email-with-results/33812/18 "2020-09-18T07:04:12Z")

</div>

Great! Thanks a lot for everything.
