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.?
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.
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?
- 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.
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:
mail:
body: |
Deleted some files - {% for f in files_to_delete.files %}f, {% endfor %}
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.
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.
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_user_name@gmail.com
password: my_pass
body: |
sender: my_user_name@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.
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.
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.