Hi everyone,
Is it possible to provide a relative path to a file as attach parameter of the ansible mail module?
Directory structure
`
./roles/notify/
- files: contains the file to send
- tasks: send_email.yml
`
Task “Send email”:
`
name: Send email
mail:
host: “{{ hostvars[groups[‘smtp’][0]][‘ansible_host’] }}”
[I forgot to mention that the attached file path can change from one execution to another, that’s why I cannot provide an absolute path]
It works fine if I copy the attached file to a non-changing directory (e.g. /tmp), but the task fails if no attached file is provided.
`
- name: Copy attached file to /tmp
copy:
src: “./files/{{ attached_file }}”
dest: “/tmp”
name: Send email
mail:
host: “{{ hostvars[groups[‘smtp’][0]][‘ansible_host’] }}”
When there’s not attachment, setting “” works fine.
name: Send email
mail:
host: “{{ smtp_server }}”
from: “{{ from_address }}”
to: “{{ to_recipients }}”
cc: “{{ cc_recipients
}}”
attach**: “”**
subject: “{{ subject }}”
body: “{{ body }}”
subtype: “html”..
So setting a variable to either “” if there’s not attachment or the file to attach if there’s one prevents the duplication of the block.