# Retrieve facts from a lookup plugin

**URL:** <https://forum.ansible.com/t/retrieve-facts-from-a-lookup-plugin/35662>\
**Category:** Ansible Developer\
**Tags:** aws\
**Created:** [August 23, 2021, 2:51pm UTC](https://forum.ansible.com/t/retrieve-facts-from-a-lookup-plugin/35662 "2021-08-23T14:51:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Day](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@John\_Day](https://forum.ansible.com/u/John_Day)\
**Post date:** [August 23, 2021, 2:51pm UTC](https://forum.ansible.com/t/retrieve-facts-from-a-lookup-plugin/35662/1 "2021-08-23T14:51:16Z")

</div>

I am building a lookup plugin to help with identifying AWS resource ids from other relevant resource information, e.g. the Name tag.

My issue is that I need the region, access key id, and secret access key to invoke the boto3 functions and I don’t really want to pass these as arguments to the lookup function on each use.

I have these set as both variables and facts in my plays. Is there something in ansible utilities that I can call to retrieve the variable value or fact variable from within the lookup plugin?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![John\_Day](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@John\_Day](https://forum.ansible.com/u/John_Day)\
**Post date:** [August 23, 2021, 2:56pm UTC](https://forum.ansible.com/t/retrieve-facts-from-a-lookup-plugin/35662/2 "2021-08-23T14:56:23Z")

</div>

I should add that I tried setting environment variables using “environment:” in the play, but those do not appear to be set yet in the process that is running the jinja2 templating.

---

<div class="post-metadata">

**Author:** ![John\_Day](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@John\_Day](https://forum.ansible.com/u/John_Day)\
**Post date:** [August 23, 2021, 9:23pm UTC](https://forum.ansible.com/t/retrieve-facts-from-a-lookup-plugin/35662/3 "2021-08-23T21:23:08Z")

</div>

I partially answered my own question, from what should have been obvious. The variable stack is passed to the lookup plugin.  
self.set\_options(var\_options=variables, direct=kwargs)  
x = variables[‘vars’][‘\<variable\_name\>’]

But I am still stuck with a problem. That returns the literal string value that was assigned to the variable, it does not evaluate that value.  
For example, if I have the following in a vars file  
x: ‘a value’  
y: “{{ x }}”

variables[‘vars’][‘y’] will return “{{ x }}”, not the evaluated result “a value”.

Is there some other utility function I need to call to force that evaluation?

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [August 23, 2021, 10:18pm UTC](https://forum.ansible.com/t/retrieve-facts-from-a-lookup-plugin/35662/4 "2021-08-23T22:18:08Z")

</div>

Make sure the vars are documented, since the documentation doubles as the arg spec:

DOCUMENTATION = “”"  
…  
options:  
option\_name:  
description: …  
vars:

- name: my\_var  
“”"  
And use self.get\_option(‘option\_name’) to retrieve the value after setting options with self.set\_options(var\_options=variables, direct=kwargs).
