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.
On Tuesday, September 15, 2020 at 5:30:38 PM UTC+3 Sandy Toshev wrote: > 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. > > On Tuesday, September 15, 2020 at 5:18:33 PM UTC+3 [email protected] > wrote: > >> 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: [email protected] >> password: my_pass >> subject: Deleted files overview >> to: Sandy toshev <my_user_name@my_domain> >> body: | >> sender: [email protected] >> 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 >> >> On Tue, Sep 15, 2020 at 4:14 PM Sandy Toshev <[email protected]> wrote: >> > >> > >> > 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: [email protected] >> > password: my_pass >> > subject: Deleted files overview >> > to: Sandy toshev <my_user_name@my_domain> >> > body: | >> > sender: [email protected] >> > 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?) >> > On Tuesday, September 15, 2020 at 4:46:06 PM UTC+3 [email protected] >> wrote: >> >> >> >> 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: [email protected] >> >> > 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 >> >> >> >> > >> >> > 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 >> >> > >> >> > On Tuesday, September 15, 2020 at 3:15:02 PM UTC+3 >> [email protected] wrote: >> >> > >> >> > 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 >> >> > >> >> > On Tue, 15 Sep 2020 at 13:55, Sandy Toshev <[email protected]> >> wrote: >> >> > > >> >> > > 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 }}" >> >> > > >> >> > > -- >> >> > > 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 [email protected]. >> >> > > 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 >> >> > >> >> > -- >> >> > 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 >> >> > [email protected] <mailto: >> [email protected]>. >> >> > 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. >> >> >> > -- >> > 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 [email protected]. >> > To view this discussion on the web visit >> https://groups.google.com/d/msgid/ansible-project/6d1091c0-d2eb-47d1-a4f0-caa2ba1875cen%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 , < >> [email protected]> >> > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f546df4d-af5d-4580-9101-5993d4edbc6en%40googlegroups.com.
