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

107 views
Skip to first unread message

Dmitriy Panteleyev

unread,
Oct 18, 2018, 1:54:12 PM10/18/18
to Ansible Project
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
.

Kai Stian Olstad

unread,
Oct 18, 2018, 2:39:29 PM10/18/18
to ansible...@googlegroups.com
On Thursday, 18 October 2018 19.54.12 CEST Dmitriy Panteleyev wrote:
> 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?

Yes, by design, but you can avoid it by using set_fact.

- set_fact:
myvar: '{{ lookup(...) }}'

--
Kai Stian Olstad


Dmitriy Panteleyev

unread,
Oct 18, 2018, 3:20:45 PM10/18/18
to Ansible Project
Thank you for clearing it up!
Reply all
Reply to author
Forward
0 new messages