# Accessing variable data from a filter

**URL:** <https://forum.ansible.com/t/accessing-variable-data-from-a-filter/17170>\
**Category:** Ansible Project\
**Created:** [January 1, 2015, 7:59pm UTC](https://forum.ansible.com/t/accessing-variable-data-from-a-filter/17170 "2015-01-01T19:59:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akos\_Vandra](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@Akos\_Vandra](https://forum.ansible.com/u/Akos_Vandra)\
**Post date:** [January 1, 2015, 7:59pm UTC](https://forum.ansible.com/t/accessing-variable-data-from-a-filter/17170/1 "2015-01-01T19:59:19Z")

</div>

Hello!

I might be going around the problem in the wrong way, but I seem to need to access the variable cache from a filter plugin.

I am trying to create a map filter, that can be used like so:

- name: Test Play  
hosts: 127.0.0.1  
connection: local  
vars:  
input:
- foo
- bar  
data:  
foo: 4  
bar: 5  
baz: 6  
tasks:
- debug: msg=“{{ input | map(”{{data[**in**]}}“) }}”

I am expecting it to print [4,5].

It seems that the way to solve it was to include a jinja expression as the parameter to the filter which will be evaluated for each element of the list, by substituting their value in place of the **in** symbol, which I took the liberty to hard-code.

Unfortunately for this to work I need to be able to access the variable cache within the filter:

from ansible.utils import template

def \_map(arg, expr):  
vars = {}  
return map(lambda x: template.template\_from\_string(None, expr, dict(vars, **in** = x), True), list(arg))

class FilterModule(object):

‘’’ Ansible extra jinja2 filters ‘’’

def filters(self):  
return {  
‘map’: \_map,  
}

This results in an error:

fatal: [127.0.0.1] =\> One or more undefined variables: ‘data’ is undefined

I have been browsing through the code for 3 hours, but could not find a way to get a copy of all variables, similar to task.py: [https://github.com/ansible/ansible/blob/devel/lib/ansible/playbook/task.py#L225,L230](https://github.com/ansible/ansible/blob/devel/lib/ansible/playbook/task.py#L225,L230)

Can anyone lend a hand?

Thanks,  
Akos Vandra

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 2, 2015, 6:41pm UTC](https://forum.ansible.com/t/accessing-variable-data-from-a-filter/17170/2 "2015-01-02T18:41:11Z")

</div>

first of all, 'map' is an existing jinja2 filter, also currently  
filters have no way to access the variable cache, they only deal with  
data passed to them.

---

<div class="post-metadata">

**Author:** ![Akos\_Vandra](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@Akos\_Vandra](https://forum.ansible.com/u/Akos_Vandra)\
**Post date:** [January 3, 2015, 3:37pm UTC](https://forum.ansible.com/t/accessing-variable-data-from-a-filter/17170/3 "2015-01-03T15:37:50Z")

</div>

I know map is an existing jinja2 filter, but as far as I see they are capable of calling “methods” only on the elements themselves, and they are not able to be used as I would intend to.

But, please, correct me if i’m wrong.

Akos
