I need to use Ansible to copy from the remote server the log files, rename them and send by emails as attachments.
I created this playbook:
---
- hosts: WebAccessServers
vars:
date: "{{ lookup('pipe', 'date +%Y%m%d') }}"
tasks:
- name: copy web access log file from Web servers to Ansible Server and rename it
fetch:
src: /var/www/html/mywebapp/logs/access.log
dest: /tmp/{{ date }}_{{ inventory_hostname }}_access.log
flat: yes
ignore_errors: true
delegate_to: localhost
- hosts: localhost
vars:
date: "{{ lookup('pipe', 'date +%Y%m%d') }}"
tasks:
- name: create variable for the attachments
shell: "ls /tmp/{{ date }}_*.log"
register: command_output
- debug: msg="{{ command_output.stdout_lines }}"
- name: Send Emails to a bunch of users, with Playbook Report as an attachment.
mail:
host: mysmtp.mydomain.com
port: 25
subject: Ansible Playbook Report
body: This is an Email generated using Ansible after execution of task.
from: [email protected] (Ansible Automates)
to:
- John Brown <[email protected]>
attach:
- "{{ command_output.stdout_lines }}"
headers:
- [email protected]
- X-Special="Write something special about this Email"
charset: us-ascii
If I run it I receive this output:
PLAY [WebAccess]
TASK [Gathering Facts] ************************************************************************************************************************************ ok: [Server-1] ok: [Server-2] ok: [Server-3]
TASK [copy custom web access log file from Web servers] ********************************************************************************************* ok: [Server-1] ok: [Server-2] ok: [Server-3]
PLAY [localhost]
TASK [Gathering Facts] ************************************************************************************************************************************ ok: [localhost]
TASK [create variable for the attachments] **************************************************************************************************************** changed: [localhost]
TASK [debug] ********************************************************************************************************************************************** ok: [localhost] => { "msg": [ "/tmp/20200923_Server-1_access.log", "/tmp/20200923_Server-2_access.log", "/tmp/20200923_Server-3_access.log" ] } TASK [Send Emails to a bunch of users, with Playbook Report as an attachment.] **************************************************************************** An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: expected str, bytes or os.PathLike object, not list fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to send mail: can't attach file ['/tmp/20200923_Server-1_access.log', '/tmp/20200923_Server-2_access.log', '/tmp/20200923_Server-3_access.log']: expected str, bytes or os.PathLike object, not list", "rc": 1} PLAY RECAP ************************************************************************************************************************************************ Server-1 : ok= changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 Server-2 : ok=2 changed=0 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0 Server-3 : ok=2
changed=0 unreachable=0 failed=0 skipped=0 rescued=0
ignored=0 localhost : ok=3 changed=1 unreachable=0 failed=1
skipped=0 rescued=0 ignored=0
Where is the best way to assign dynamically and recursively the attach in the email? Thanks for the support. Marco