# Support for functions for complex expressions

**URL:** <https://forum.ansible.com/t/support-for-functions-for-complex-expressions/36837>\
**Category:** Ansible Developer\
**Created:** [July 12, 2022, 7:59pm UTC](https://forum.ansible.com/t/support-for-functions-for-complex-expressions/36837 "2022-07-12T19:59:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roy\_Lenferink](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Roy\_Lenferink](https://forum.ansible.com/u/Roy_Lenferink)\
**Post date:** [July 12, 2022, 7:59pm UTC](https://forum.ansible.com/t/support-for-functions-for-complex-expressions/36837/1 "2022-07-12T19:59:16Z")

</div>

Hi all,

Ansible has a lot of options, where combining and chaining filters is of great usage.  
In certain cases the final result might become a bit hard to maintain if it is re-used in multiple places. E.g. take the following examples:

_merged\_list: “{{ (lookup(‘vars’, \*query(‘varnames’, ‘^.+\_\_merge\_list$’)) + [testlist\_initial\_value]) | flatten(levels=1) }}”_

_merged\_dict: “{{ lookup(‘vars’, \*query(‘varnames’, ‘^.+\_\_merge\_dict$’)) | combine(testdict\_initial\_value, recursive=True, list\_merge=‘append’) }}”_

I couldn’t find anything about Ansible having this functionality, but is it an idea for adding functions/aliases to Ansible where often re-used expressions can be stored as a ‘function’ or ‘alias’ so the user can call it with the appropriate input arguments?

In case of the above e.g. defining the expressions somewhere and calling it as e.g.  
_{{ merge\_dict(‘\_\_merge\_dict’, testdict\_initial\_value) }}_

The user then only has to define the complex expression once, after which it can re-use the function in multiple places throughout the project.

Happy to hear any thoughts on this idea (or whether this is already possible).

Regards,  
Roy

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 13, 2022, 3:37pm UTC](https://forum.ansible.com/t/support-for-functions-for-complex-expressions/36837/2 "2022-07-13T15:37:30Z")

</div>

Jinja2 has this already, macros  
[https://jinja.palletsprojects.com/en/2.10.x/templates/#macros](https://jinja.palletsprojects.com/en/2.10.x/templates/#macros)

---

<div class="post-metadata">

**Author:** ![Roy\_Lenferink](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@Roy\_Lenferink](https://forum.ansible.com/u/Roy_Lenferink)\
**Post date:** [July 13, 2022, 5:37pm UTC](https://forum.ansible.com/t/support-for-functions-for-complex-expressions/36837/3 "2022-07-13T17:37:00Z")

</div>

Thanks for the link Brian!

I experimented a bit with macros but couldn’t find a working solution.  
These are the lines I would like to have available in a macro: [https://github.com/rlenferink/ansible-function-test/blob/9c83a40a29eb616a59b4da713771545a2aa44f32/playbook.yml#L15-L16](https://github.com/rlenferink/ansible-function-test/blob/9c83a40a29eb616a59b4da713771545a2aa44f32/playbook.yml#L15-L16)

Knowing that this is called a macro, I found the following feature request: [https://github.com/ansible/ansible/issues/36130](https://github.com/ansible/ansible/issues/36130)  
It is mentioned in the issue that the decision was made not to add such a feature.

I tried using the described workaround but wasn’t successful so far:

[https://github.com/rlenferink/ansible-function-test/tree/macro-try](https://github.com/rlenferink/ansible-function-test/tree/macro-try)

Honestly using a separate file for defining this macro and then importing it for every usage also feels a bit like a workaround (since this would not something I’d do once, more like in the 100+ occurrences area).

Do you know if there is an alternative to define and use macros within a playbook?  
Is it maybe worth reevaluating the feature request for adding support for this?

Roy

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 14, 2022, 2:20pm UTC](https://forum.ansible.com/t/support-for-functions-for-complex-expressions/36837/4 "2022-07-14T14:20:25Z")

</div>

Mostly i was thinking for the use of in templates, the only other  
thing i can think of is using 'intermediate vars' to keep reusable  
expressions and then creating their input when needed:

- play:  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;merged\_list: "{{ (lookup('vars', \*query('varnames',  
merge\_pattern$')) + [testlist\_initial\_value]) |  
flatten(levels=merge\_flatten\_levels|default(1)) }}"  
....

- name: also works with 'vars' in other scopes, using set\_fact to  
get 'static value'  
&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mylist: '{{merged\_list}}'  
&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;testlist\_initial\_value: [10,11]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;merge\_pattern: '^.+\_merge\_list'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a\_merge\_list: [1,2,3]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b\_merge\_list: [4,5,6]
