Understanding lookup task context

28 views
Skip to first unread message

Lihu Ber

unread,
Apr 6, 2018, 10:17:24 AM4/6/18
to Ansible Project
I noticed odd behavior with Ansible lookups a while back and, thinking it was a bug, reported it here. Essentially, when I defined a lookup in host_vars, and referenced it in different ways from the same role, the lookup was executing with a different 'relative search path'. Brian told me this was by design, since my tasks different contexts. I'm not sure I understand the distinction though. Here is the role in question:

---
# Does lookup with respect to role
- name: direct reference
 
debug:
   
msg: "{{ users }}"
   
# Does lookup with respect to playbook
- name: reference through hostvars
 
debug:
   
msg: "{{ hostvars['SERVER_NAME']['users'] }}"

What's different about the context for these two tasks? How can I know a priori how a lookup is going to execute, without doing guess-and-check?

Thanks so much for your help!

Brian Coca

unread,
Apr 6, 2018, 11:40:56 AM4/6/18
to Ansible Project
I forgot to add to the response, that hostvars is not 'task aware'
while direct variable access IS task aware.



--
----------
Brian Coca

Lihu Ber

unread,
Apr 6, 2018, 1:14:02 PM4/6/18
to Ansible Project
Hi Brian, thanks so much for being so responsive!

Why the difference in 'task awareness' between the two access methods? Also, is there some documentation on which operations are task-aware, and which ones aren't?

Brian Coca

unread,
Apr 6, 2018, 3:29:41 PM4/6/18
to Ansible Project
No docs really, this is not something people normally hit as it is
rare to try to do indirect lookups via hostvars. It has more to do on
how hostvars works than anything else, it is a lazy object that is
lazy loaded vars, pre 2.5 we woudl load all vars per host and keep
that info (w/o it having any access to task info), in 2.5 now we don't
preload per host, but it still has no 'task info'.

When using vars directly you do have the task info always available,
that is the main difference, i'm looking at the possibility of adding
task info to hostvars, but precedence and how variable management
works in general makes this difficult.

----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages