# long nasty filter chain

**URL:** <https://forum.ansible.com/t/long-nasty-filter-chain/29615>\
**Category:** Ansible Project\
**Created:** [February 5, 2019, 2:39am UTC](https://forum.ansible.com/t/long-nasty-filter-chain/29615 "2019-02-05T02:39:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luca\_Cazzaniga](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@Luca\_Cazzaniga](https://forum.ansible.com/u/Luca_Cazzaniga)\
**Post date:** [February 5, 2019, 2:39am UTC](https://forum.ansible.com/t/long-nasty-filter-chain/29615/1 "2019-02-05T02:39:21Z")

</div>

Hi all I’ve some variables scattered in my roles dirs:

fstype: [‘xfs’, ‘ext4’, ‘ext3’, ‘btrfs’, ‘ufs’] #enable check on all partition of the listed types  
default\_limit: { limit: 90, unit: ‘%’ }  
mountpoints:

- { mount: “/”, limit: 80, unit: “%” }
- { mount: “/usr/local” } #use default limit  
#mntpoints: “{{ mountpoint|default(ansible\_mounts|selectattr(‘fstype’, ‘in’, fstype))|map(‘combine’, default\_limit)|list }}”  
mntpoints: “{{ mountpoints|selectattr(‘limit’,‘defined’)|list + mountpoints|selectattr(‘limit’,‘undefined’)|map(‘combine’,default\_limit)|list if mountpoints|default()|length \> 0 else ansible\_mounts|selectattr(‘fstype’, ‘in’, fstype)|map(‘combine’,default\_limit)|list }}”

my aim is to get a list of mount points with a space limit defined or otherwise set to a default value, if specific mounts aren’t chosen the filesystems of the specified types are yielded out…  
And I got it the snippet works… but it’s really nasty, ahah I make python gigs nasty… the revenge of the dirty perl guy… anyway-

I’ve a lot of questions…

in the commented assignment the issue was that combine overwrote existent limits…  
so I had to opt for the long and repetitive filter chain…

Is possible to split the long jinja2 assignment on more lines to improve readability? how?

Is there a more brilliant way to write my filter without creating a new combine filter?  
In order to improve the readability I’d like to shorten the ansible\_mounts hash

Is there a way to make projection (a sort of map over more fields)? Yes, I know it’s not useful, anyway I like to know if it’s possible…

Would it be better placing the logic in the template file?

thanks

Luca
