ansible-collections / community.general

Ansible Community General Collection
https://galaxy.ansible.com/ui/repo/published/community/general/
GNU General Public License v3.0
836 stars 1.53k forks source link

Add an 'accumulate' filter #9129

Open VannTen opened 1 week ago

VannTen commented 1 week ago

Summary

I am trying to convert systemd slice names to the corresponding cgroup trees using ansible filters, which bogs down to: my-slice-foo.slice -> /my.slice/my-slice.slice/my-slice-foo.slice

(slice are always in a hierarchy and include the parent slice in their own name, and this is reflected in the hierarchy).

I had no luck finding a way to achieve this using only variable (and I'd rather not use fact and loops for that) but accumulate ( which would be a pass-through to the matching python function in itertools would make that easy.

I do believe that while my usecase in rather specialized, the underlying transformation is a general tool which would be useful for a variety of purpose.

(I implement the PR for that myself btw).

If there is a already available way to this without a new filter / lookup, I'm happy to hear about it :)

Issue Type

Feature Idea

Component Name

plugins/filter/

Additional Information

# implementation of the use case mentionned below using hypothetical accumulate:

my_slice: my-slice-foo.slice

slice_parts: "{{ (my_slice | splitext)[0].split('-') | map ('split') }}" -> list of singleton lists containing each one part of the string
cgroups_path_parts: "{{ (slice_parts | accumulate | map('join', '-') | join('.slice/')) + '.slice/' }}"

Code of Conduct

ansibullbot commented 1 week ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 1 week ago

cc @Ajpantuso @cfiehe @ericzolf @felixfontein @giner @jouir @keilr @kellyjonbrazil @resmo @sscheib @vbotka click here for bot help

VannTen commented 1 week ago

!component =plugins/filter/

ansibullbot commented 1 week ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help