# Ansible attach in mail module doesnt work

**URL:** <https://forum.ansible.com/t/ansible-attach-in-mail-module-doesnt-work/31204>\
**Category:** Ansible Project\
**Created:** [September 5, 2019, 10:11am UTC](https://forum.ansible.com/t/ansible-attach-in-mail-module-doesnt-work/31204 "2019-09-05T10:11:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [September 5, 2019, 10:11am UTC](https://forum.ansible.com/t/ansible-attach-in-mail-module-doesnt-work/31204/1 "2019-09-05T10:11:33Z")

</div>

I have a code as below and i have my html files present in /tmp/sanity/ .  
but i am getting error as “Failed to send mail: can’t attach file” i get file not found

- name: list html files  
run\_once: true  
local\_action: shell find /tmp/sanity/ -type f -regex “.\*.html” | tr -s ‘\n’ ’ ’ | xargs | sort  
register: list\_html\_files

- debug: msg=“{{ list\_html\_files }}”

- name: generate final email body  
run\_once: true  
local\_action: shell for i in `find /tmp/sanity/ -type f -regex ".*email.*.txt" | sort`; do cat $i ;done  
register: email\_body

- debug: msg=“{{email\_body}}”

- name: email results  
become: true  
ignore\_errors: true  
run\_once: true  
mail:  
host: “{{smtp\_host}}”  
port: 25  
body: ‘{{ email\_body.stdout }}’  
attach:

- ‘{{ list\_html\_files.stdout\_lines[0] }}’  
charset: utf8  
delegate\_to: localhost

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 5, 2019, 11:44am UTC](https://forum.ansible.com/t/ansible-attach-in-mail-module-doesnt-work/31204/2 "2019-09-05T11:44:10Z")

</div>

> I have a code as below and i have my html files present in /tmp/sanity/ .  
> but i am getting error as "Failed to send mail: can't attach file" i get file not found

Well that's pretty clear: the file is not found.  
Debug what is in list\_html\_files.stdout\_lines[0] and adjust that.  
Without any further information it's hard to tell.

On a related note, instead of fragile shell find/tr/xargs/sort , I  
would strongly suggest to use the 'find' module.  
That will at least return a standard data structure (which is easier  
to offer help with).

Dick

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [September 5, 2019, 12:32pm UTC](https://forum.ansible.com/t/ansible-attach-in-mail-module-doesnt-work/31204/3 "2019-09-05T12:32:51Z")

</div>

Additionally, you can use `fileglob` - [https://docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html](https://docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html) for getting desired files from given directory structure.

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [September 5, 2019, 1:21pm UTC](https://forum.ansible.com/t/ansible-attach-in-mail-module-doesnt-work/31204/4 "2019-09-05T13:21:00Z")

</div>

Hi Dick/Abhijit,

The problem is not searching the file in the location. the problem is “attach” inside mail module doesn’t recognize the variable reference(it takes hard coded path).I dont want to hard code path inside attach. I want to use the variable reference inside attach module.

In the debug of “list\_html\_files” below it clearly shows that the html files are present. i am using ansible.2.7.5

Pls suggest how can i achieve this.

ok: [10.253.173.111] =\> {  
“msg”: {  
“changed”: true,  
“cmd”: “find /tmp/sanity\_RIC1/ -type f -regex ".\*.html" | tr -s ‘\n’ ’ ’ | xargs | sort”,  
“delta”: “0:00:00.006235”,  
“end”: “2019-09-05 04:03:01.195198”,  
“failed”: false,  
“rc”: 0,  
“start”: “2019-09-05 04:03:01.188963”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “/tmp/sanity\_RIC1/sanity\_RIC1IC01\_20190905-040234.html /tmp/sanity\_RIC1/sanity\_RIC1IC02\_20190905-040247.html”,  
“stdout\_lines”: [  
“/tmp/sanity\_RIC1/sanity\_RIC1IC01\_20190905-040234.html /tmp/sanity\_RIC1/sanity\_RIC1IC02\_20190905-040247.html”  
]  
}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 5, 2019, 11:53pm UTC](https://forum.ansible.com/t/ansible-attach-in-mail-module-doesnt-work/31204/5 "2019-09-05T23:53:12Z")

</div>

Hi

You deliberately replaced newlines with spaces, so now your creative  
find/tr/xargs/sort command yields only one (1) line.  
Hence the attach parameter will see one line that isn't a file, and fail.  
I will repeat my previous advice: drop the shell and use the find module.  
The documentation is here:  
[https://docs.ansible.com/ansible/latest/modules/find\_module.html](https://docs.ansible.com/ansible/latest/modules/find_module.html)

That will give you exactly what you want.

Dick

---

<div class="post-metadata">

**Author:** ![Rakesh\_Parida](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@Rakesh\_Parida](https://forum.ansible.com/u/Rakesh_Parida)\
**Post date:** [September 6, 2019, 6:56am UTC](https://forum.ansible.com/t/ansible-attach-in-mail-module-doesnt-work/31204/6 "2019-09-06T06:56:32Z")

</div>

Hi Dick,

Thanks for the Advice. It works like charm.  
I modified the code as below and it works as desired. Again Many thanks for your valuable Suggestion

- name: list html files  
delegate\_to: localhost  
find:  
paths: /tmp/sanity  
patterns: ‘\*.html’  
run\_once: true  
register: list\_html\_files

- debug: msg=“{{list\_html\_files}}”

- name: email results  
ignore\_errors: true  
run\_once: true  
mail:  
host: “{{smtp\_host}}”  
port: 25  
body: Test  
attach: “{{ item.path }}”  
charset: utf8  
delegate\_to: localhost  
with\_items: “{{ list\_html\_files.files }}”
