# When are file lookups performed? How to store lookup result in a variable?

**URL:** <https://forum.ansible.com/t/when-are-file-lookups-performed-how-to-store-lookup-result-in-a-variable/28823>\
**Category:** Ansible Project\
**Created:** [October 18, 2018, 5:54pm UTC](https://forum.ansible.com/t/when-are-file-lookups-performed-how-to-store-lookup-result-in-a-variable/28823 "2018-10-18T17:54:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dmitriy\_Panteleyev](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@Dmitriy\_Panteleyev](https://forum.ansible.com/u/Dmitriy_Panteleyev)\
**Post date:** [October 18, 2018, 5:54pm UTC](https://forum.ansible.com/t/when-are-file-lookups-performed-how-to-store-lookup-result-in-a-variable/28823/1 "2018-10-18T17:54:12Z")

</div>

I was under the impression that if I define a variable with a `lookup` function, it would store the results of the lookup. And then when I used that variable, it would spit out the results of the lookup.

However, from running a bunch of different test cases that Ansible performs the lookup every time I try to use that variable. Is that by design? Is there a way to avoid repeated lookups?

So… expectation:

`  
myvar: lookup(…)

> > > look up stuff and store result of lookup in ‘myvar’

- name: ‘{{ myvar }}’

> > > spit out contents of ‘myvar’

- debug:  
var: myvar

> > > spit out contents of ‘myvar’

etc.

`

What seems to be happening is…

`  
myvar: lookup(…)

> > > look up stuff

- name: ‘{{ myvar }}’

> > > look at ‘myvar’ and perform the lookup defined by it, then spit out result

- debug:  
var: myvar

> > > look at ‘myvar’ and perform the lookup defined by it, then spit out result

etc.  
`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 18, 2018, 6:39pm UTC](https://forum.ansible.com/t/when-are-file-lookups-performed-how-to-store-lookup-result-in-a-variable/28823/2 "2018-10-18T18:39:18Z")

</div>

Yes, by design, but you can avoid it by using set\_fact.

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;myvar: '{{ lookup(...) }}'

---

<div class="post-metadata">

**Author:** ![Dmitriy\_Panteleyev](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@Dmitriy\_Panteleyev](https://forum.ansible.com/u/Dmitriy_Panteleyev)\
**Post date:** [October 18, 2018, 7:20pm UTC](https://forum.ansible.com/t/when-are-file-lookups-performed-how-to-store-lookup-result-in-a-variable/28823/3 "2018-10-18T19:20:44Z")

</div>

Thank you for clearing it up!
