# Ansible mail module: how to attach a file with a relative path?

**URL:** <https://forum.ansible.com/t/ansible-mail-module-how-to-attach-a-file-with-a-relative-path/28589>\
**Category:** Ansible Project\
**Created:** [September 17, 2018, 10:13am UTC](https://forum.ansible.com/t/ansible-mail-module-how-to-attach-a-file-with-a-relative-path/28589 "2018-09-17T10:13:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sabrina\_Lautier](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Sabrina\_Lautier](https://forum.ansible.com/u/Sabrina_Lautier)\
**Post date:** [September 17, 2018, 10:13am UTC](https://forum.ansible.com/t/ansible-mail-module-how-to-attach-a-file-with-a-relative-path/28589/1 "2018-09-17T10:13:48Z")

</div>

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’] }}”

---

<div class="post-metadata">

**Author:** ![Sabrina\_Lautier](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Sabrina\_Lautier](https://forum.ansible.com/u/Sabrina_Lautier)\
**Post date:** [September 17, 2018, 2:38pm UTC](https://forum.ansible.com/t/ansible-mail-module-how-to-attach-a-file-with-a-relative-path/28589/2 "2018-09-17T14:38:20Z")

</div>

[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’] }}”

---

<div class="post-metadata">

**Author:** ![Sabrina\_Lautier](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@Sabrina\_Lautier](https://forum.ansible.com/u/Sabrina_Lautier)\
**Post date:** [September 27, 2018, 11:43am UTC](https://forum.ansible.com/t/ansible-mail-module-how-to-attach-a-file-with-a-relative-path/28589/3 "2018-09-27T11:43:55Z")

</div>

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.
