# Ansible mail module: how to include several files in 'body' option

**URL:** <https://forum.ansible.com/t/ansible-mail-module-how-to-include-several-files-in-body-option/27853>\
**Category:** Ansible Project\
**Created:** [June 22, 2018, 5:19pm UTC](https://forum.ansible.com/t/ansible-mail-module-how-to-include-several-files-in-body-option/27853 "2018-06-22T17:19:01Z")\
**Posts on this page:** 4\
**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:** [June 22, 2018, 5:19pm UTC](https://forum.ansible.com/t/ansible-mail-module-how-to-include-several-files-in-body-option/27853/1 "2018-06-22T17:19:01Z")

</div>

Hi,  
I managed to include a j2 file as ‘body’ option of the ansible mail module:

> mail: body: “{{ lookup(‘template’, j2file) }}”

, but need now to include header and footer html files (header.html and footer.html) respectively before and after the j2 file. Is it possible to do it without including these html file in the j2 file? I can’t figure that out.

Note: the j2 file contains ansible variables whereas the html files don’t.

Best,  
Sabrina

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 22, 2018, 5:52pm UTC](https://forum.ansible.com/t/ansible-mail-module-how-to-include-several-files-in-body-option/27853/2 "2018-06-22T17:52:24Z")

</div>

I would just do the include inside the template, this is what jinja2's  
original purpose.

If you don't want to touch the template, you could just concatenate later:

mail: body: "{{ lookup('file', 'header.html') + lookup('template',  
j2file) + lookup('file', 'footer.hml'}} "

---

<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:** [June 25, 2018, 10:09am UTC](https://forum.ansible.com/t/ansible-mail-module-how-to-include-several-files-in-body-option/27853/3 "2018-06-25T10:09:14Z")

</div>

Thanks Brian, this wroks like a charm :):

> mail: body: "{{ lookup(‘file’, ‘header.html’) + lookup(‘template’,  
> j2file) + lookup(‘file’, ‘footer.hml’}} "

I was doing this:

> mail: body: "lookup(‘file’, ‘header.html’) + {{ lookup(‘template’,  
> j2file) }} + lookup(‘file’, ‘footer.hml’) "

And this wasn’t working as it printed this:

> lookup(‘files’, header.html) + "

> 

> " + lookup(‘files’, footer.html)

I thought that {{}} were only required to replace the variables in the j2 file.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 25, 2018, 8:11pm UTC](https://forum.ansible.com/t/ansible-mail-module-how-to-include-several-files-in-body-option/27853/4 "2018-06-25T20:11:34Z")

</div>

lookup() \<= this is a jinja2 construct in Ansible, so it won't  
evaluate outside of a jinja2 context (what {{ }} is).
