# Custom filter plugin - access Ansible variables?

**URL:** <https://forum.ansible.com/t/custom-filter-plugin-access-ansible-variables/3910>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [February 16, 2024, 8:57pm UTC](https://forum.ansible.com/t/custom-filter-plugin-access-ansible-variables/3910 "2024-02-16T20:57:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jms1](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jms1/32/2255_2.png) [@jms1](https://forum.ansible.com/u/jms1)\
**Post date:** [February 16, 2024, 8:57pm UTC](https://forum.ansible.com/t/custom-filter-plugin-access-ansible-variables/3910/1 "2024-02-16T20:57:48Z")

</div>

I’m writing a custom filter plugin in Python, to perform some complicated business logic which uses several values which are already known as Ansible variables. This is going to take a filename and return a list of modified versions of that filename, in a specific order, which will then be used with the `ansible.builtin.first_found` filter.

Is there a way for the Python code to access Ansible variables directly, rather than having to pass parameters to the filter function? These will include `playbook_dir`, `role_path`, a few calculated values, and a few “facts” supplied by `/etc/ansible/facts.d/` files on the target machines.

I ask because the immediate problem I’m trying to solve needs the values of eight different variables, and I _really_ don’t want to have to pass every single one of those variables to the filter every time I call it.

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [February 17, 2024, 1:43am UTC](https://forum.ansible.com/t/custom-filter-plugin-access-ansible-variables/3910/2 "2024-02-17T01:43:06Z")

</div>

Filters aren’t really intended to access data they weren’t explicitly passed. While it might technically be possible, it’s easier to use a lookup plugin (which has a Templar instance available to it) for this purpose. A couple of toy examples can be found at [0\_test.yml · GitHub](https://gist.github.com/flowerysong/7994e39cf6518410efcd1190ef4aebcd) and [0\_test.yml · GitHub](https://gist.github.com/flowerysong/59123681c382f54a9dca51b436fd8ef8), or you can look at the usage of templating in the [`ansible.builtin.first_found`](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/lookup/first_found.py) and [`ansible.builtin.vars`](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/lookup/vars.py) lookups.

---

<div class="post-metadata">

**Author:** ![jms1](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jms1/32/2255_2.png) [@jms1](https://forum.ansible.com/u/jms1)\
**Post date:** [February 18, 2024, 6:30pm UTC](https://forum.ansible.com/t/custom-filter-plugin-access-ansible-variables/3910/3 "2024-02-18T18:30:08Z")

</div>

Can you point me to some documentation which explains what a “Templar instance” is, and how to make use of it?

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [February 18, 2024, 11:31pm UTC](https://forum.ansible.com/t/custom-filter-plugin-access-ansible-variables/3910/4 "2024-02-18T23:31:42Z")

</div>

I pointed you to several things that show you how to use it. What questions are you left with after reading through the examples that I provided?

---

<div class="post-metadata">

**Author:** ![jms1](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jms1/32/2255_2.png) [@jms1](https://forum.ansible.com/u/jms1)\
**Post date:** [February 19, 2024, 2:12pm UTC](https://forum.ansible.com/t/custom-filter-plugin-access-ansible-variables/3910/5 "2024-02-19T14:12:54Z")

</div>

The term “Templar” is not something I’ve ever seen in the context of computers, let alone Ansible.

Again, _what is_ a “Templar instance”?

Your comment above mentioned it in passing, as if I’m supposed to already know what that means. I just asked for clarification - the email notification came in over the weekend, I plan on looking over those examples later today.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [February 19, 2024, 7:40pm UTC](https://forum.ansible.com/t/custom-filter-plugin-access-ansible-variables/3910/6 "2024-02-19T19:40:07Z")

</div>

You could search for the string `templar` in the links @flowerysong provided to see it in action. Alternatively look up the definition of that class in ansible-core’s source code ([ansible/lib/ansible/template/\_\_init\_\_.py at devel · ansible/ansible · GitHub](https://github.com/ansible/ansible/blob/devel/lib/ansible/template/ __init__.py#L562)).

---

<div class="post-metadata">

**Author:** ![jms1](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jms1/32/2255_2.png) [@jms1](https://forum.ansible.com/u/jms1)\
**Post date:** [February 20, 2024, 4:59pm UTC](https://forum.ansible.com/t/custom-filter-plugin-access-ansible-variables/3910/7 "2024-02-20T16:59:57Z")

</div>

I did see it “in action”, I was asking where to find the official _documentation_ about it. The search engines keep directing me to pages about a Nicholas Cage movie, or to the old religious order mentioned in that movie. It’s really weird, like … maybe the movie industry is paying google to send clicks their way or something?

Anyway. After a good night’s sleep, and reading the documentation about “lookup plugins”, it turns out that within the `run()` function I can use `variables.get( 'xxx' , '' )` to retrieve the value of any variable that can be “seen” by the task which executes the plugin. This, plus “use a lookup plugin instead of a filter plugin”, are all I was looking for in the first place.

The only other thing I ran into is that `environment` happens to be an undocumented “magic” variable that you can’t use … but that’s not on anybody here, unless you’re part of the ansible documentation team.

Thanks for nudging me in the right direction, much appreciated.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [February 20, 2024, 6:02pm UTC](https://forum.ansible.com/t/custom-filter-plugin-access-ansible-variables/3910/8 "2024-02-20T18:02:30Z")

</div>

> [@jms1](#):
>
> I did see it “in action”, I was asking where to find the official _documentation_ about it. The search engines keep directing me to pages about a Nicholas Cage movie, or to the old religious order mentioned in that movie. It’s really weird, like … maybe the movie industry is paying google to send clicks their way or something?

The only official documentation is the ansible-core source code.

> [@jms1](#):
>
> but that’s not on anybody here, unless you’re part of the ansible documentation team.

The “ansible documentation team” is basically the community. So it includes both you and me 🙂
