# lookup("template") but only if template exists

**URL:** <https://forum.ansible.com/t/lookup-template-but-only-if-template-exists/17819>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [March 23, 2015, 8:12pm UTC](https://forum.ansible.com/t/lookup-template-but-only-if-template-exists/17819 "2015-03-23T20:12:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bret\_Martin](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@Bret\_Martin](https://forum.ansible.com/u/Bret_Martin)\
**Post date:** [March 23, 2015, 8:12pm UTC](https://forum.ansible.com/t/lookup-template-but-only-if-template-exists/17819/1 "2015-03-23T20:12:33Z")

</div>

I used to be able to do this in order to construct EC2 instance user data from a template, but only if the template file existed – otherwise, leave the user data empty.

- name: create {{ description }} instance  
ec2:

[…]  
user\_data: '{{ lookup(“template”,

“…/templates/user\_data/” + instance + “.j2”)

> default(None) }}’

I think I was actually taking advantage of the fact that the lookup plugin used to silently eat template errors (is that right?), but no longer does – now I get

TASK: [aws-ec2 | create Data Importer instance] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [aws-vpc-hbi16058] =\> unable to read /Users/bam/dev/h3-ansible/roles/aws-ec2/templates/user\_data/data-importer.j2

Is there another way I can accomplish this sort of lookup-template-but-only-if-it-exists?
