Sending email with results

179 views
Skip to first unread message

Sandy Toshev

unread,
Sep 15, 2020, 7:54:48 AM9/15/20
to Ansible Project
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.

---
- hosts: lxc
  tasks:
  - name: Finding older than 1 week files in the distfiles folder
    find:
      paths: /var/cache/distfiles
      age: 1w
      recurse: no
    register: files_to_delete

  - name: Removing the founded files
    file:
      path: "{{ item.path }}"
      state: absent
    with_items: "{{ files_to_delete.files }}"

Dick Visser

unread,
Sep 15, 2020, 8:15:02 AM9/15/20
to ansible...@googlegroups.com
Use 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
mail:
body: |
Deleted some files - {% for f in files_to_delete.files %}f, {% endfor %}
subject: Deleted files overview

etc etc
> --
> 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/1fd1fcbe-4150-49b8-93c0-9a6aa5879da9n%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Sandy Toshev

unread,
Sep 15, 2020, 8:36:30 AM9/15/20
to Ansible Project

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_use...@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 ':'

The error appears to be in '/root/ansible_playbooks/remove_distfiles.yml': line 21, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

      Deleted some files - {% for f in files_to_delete.files %}f, {% endfor %}
      subject: Deleted files overview
      ^ here

timothy pember

unread,
Sep 15, 2020, 8:41:33 AM9/15/20
to ansible...@googlegroups.com
You could generate the app specific password for gmail and use it to try again.

Thanks 

Sandy Toshev

unread,
Sep 15, 2020, 8:45:39 AM9/15/20
to Ansible Project

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

timothy pember

unread,
Sep 15, 2020, 8:58:39 AM9/15/20
to ansible...@googlegroups.com
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 


Sandy Toshev

unread,
Sep 15, 2020, 9:32:51 AM9/15/20
to Ansible Project
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?

timothy pember

unread,
Sep 15, 2020, 9:36:26 AM9/15/20
to ansible...@googlegroups.com
Can you configure the credentials with a regular mail client such as thunderbird to see if your gmail SMTP works correctly?

Thanks 

Luca 'remix_tj' Lorenzetto

unread,
Sep 15, 2020, 9:42:18 AM9/15/20
to ansible...@googlegroups.com
Hello,

you're missing an indentation on the line after

body: |

Luca
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e8a857a0-0709-4577-807b-da88fe1a489bn%40googlegroups.com.



--
"E' assurdo impiegare gli uomini di intelligenza eccellente per fare
calcoli che potrebbero essere affidati a chiunque se si usassero delle
macchine"
Gottfried Wilhelm von Leibnitz, Filosofo e Matematico (1646-1716)

"Internet è la più grande biblioteca del mondo.
Ma il problema è che i libri sono tutti sparsi sul pavimento"
John Allen Paulos, Matematico (1945-vivente)

Luca 'remix_tj' Lorenzetto, http://www.remixtj.net , <lorenze...@gmail.com>

Sandy Toshev

unread,
Sep 15, 2020, 9:43:15 AM9/15/20
to Ansible Project
:) - 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.

Stefan Hornburg (Racke)

unread,
Sep 15, 2020, 9:46:06 AM9/15/20
to ansible...@googlegroups.com
On 9/15/20 2:36 PM, Sandy Toshev wrote:
>
> 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_use...@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:

mail:
body: |
Deleted some files - {% for f in files_to_delete.files %}f, {% endfor %}
subject: Deleted files overview

Regards
Racke
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/e8a857a0-0709-4577-807b-da88fe1a489bn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/e8a857a0-0709-4577-807b-da88fe1a489bn%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Sandy Toshev

unread,
Sep 15, 2020, 10:13:50 AM9/15/20
to Ansible Project

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.

---
- hosts: lxc
  tasks:
  - name: Finding older than 1 week files in the distfiles folder
    find:
      paths: /var/cache/distfiles
      age: 1w
      recurse: no
    register: files_to_delete

  - name: Removing the founded files
    file:
      path: "{{ item.path }}"
      state: absent
    with_items: "{{ files_to_delete.files }}"

  - name: Sending mail
    mail:
    host: smtp.gmail.com
    port: 587
    username: my_use...@gmail.com
    password: my_pass
    subject: Deleted files overview
    to: Sandy toshev <my_user_name@my_domain>
    body: |
    sender: my_use...@gmail.com
    Deleted some files - {% for f in files_to_delete.files %}f, {% endfor %}

===

Error:
[root@ansible#~/ansible_playbooks]#ansible-playbook remove_distfiles_email.yml
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 ':'

The error appears to be in '/root/ansible_playbooks/remove_distfiles_email.yml': line 28, column 1, but may
be elsewhere in the file depending on the exact syntax problem.

(specified line no longer in file, maybe it changed?)

Dick Visser

unread,
Sep 15, 2020, 10:16:58 AM9/15/20
to ansible...@googlegroups.com
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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6d1091c0-d2eb-47d1-a4f0-caa2ba1875cen%40googlegroups.com.

Luca 'remix_tj' Lorenzetto

unread,
Sep 15, 2020, 10:18:33 AM9/15/20
to ansible...@googlegroups.com
Hello,

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

- name: Sending mail
mail:
host: smtp.gmail.com
port: 587
username: my_use...@gmail.com
password: my_pass
subject: Deleted files overview
to: Sandy toshev <my_user_name@my_domain>
body: |
sender: my_use...@gmail.com
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
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6d1091c0-d2eb-47d1-a4f0-caa2ba1875cen%40googlegroups.com.

Sandy Toshev

unread,
Sep 15, 2020, 10:30:38 AM9/15/20
to Ansible Project
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.

Sandy Toshev

unread,
Sep 18, 2020, 2:18:35 AM9/18/20
to Ansible Project
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.

Dick Visser

unread,
Sep 18, 2020, 2:30:18 AM9/18/20
to ansible...@googlegroups.com
ah yes, that f should be in double brackets:

{{ f }}
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f546df4d-af5d-4580-9101-5993d4edbc6en%40googlegroups.com.

Sandy Toshev

unread,
Sep 18, 2020, 3:04:12 AM9/18/20
to Ansible Project
Great! Thanks a lot for everything.
Reply all
Reply to author
Forward
0 new messages