ansible-lint icon indicating copy to clipboard operation
ansible-lint copied to clipboard

use-loop: Disallow the use of `with_xxx` as a looping mechanism

Open cidrblock opened this issue 3 years ago • 4 comments

The use of with_ is "discouraged" and "loop" is preferred.

Although with_ is not deprecated, this would be a good optional rule for people wanting to future proof their playbooks.

cidrblock avatar Jun 09 '22 15:06 cidrblock

This is not entirely true. There are still places where with_ is recommended over using loop.

with_XXX should not be converted to a loop if you just have to call lookup to get the same behavior.

loop: '{{ lookup("XXX", "yyy") }}'` 

That is more complicated than just with_XXX: yyy

We really should not being discouraging use of any non-deprecated functionality that ansible-core provides.

It does make more sense to use something like the |dict2items filter instead of with_dict. Lookups should lookup data, not just manipulate data for the purpose of looping. That is the job of filter plugins.

sivel avatar Jun 09 '22 20:06 sivel

@sivel Can you please provide some real-world example where there no loop alternative for existing with or where the loop alternative is worse? We need a clear and comprehensive list of examples in order to figure-out how this rule should be implemented.

The reality is that full qualified notation adoption made the with_* look horrible. As we are already toward requiring them in other places we endup with stuff like with_ansible.builtin.items or with_community.general.flattened.

We need to build a list of convert, maybe and keep for at least core and community.general filters.

Unless we have clear cases where conversion is not possible or too inconvenient, I am supporting ditching most with_. Lets see the examples, what I seen in documentation are very few.

ssbarnea avatar Jun 10 '22 09:06 ssbarnea

loop is just less readable sometimes. I'm not sure how you'd get a comprehensive list of examples (if the general one given is not it) since with_ works with any arbitrary lookup. Core was considering deprecating with_ at one point so you can probably find some real-world examples in ansible/ansible issues/meetting logs that were worse with loop.

Here's one, https://github.com/ansible/ansible/issues/51153. Even if that example used with_ansible.builtin.nested instead of with_nested, it's still easier to read than the loop alternative.

s-hertel avatar Jun 10 '22 15:06 s-hertel

I really dislike this one because it's controversial and a matter of taste and style. It didn't make it into the Ansible CoP good practices for a reason. I dislike it even more being in the production profile and being on by default. Yes, I know they can switch it off, but I've worked for companies that wouldn't allow me to change the vendor recommended settings. You want to include it in lint? Fine, but make it optional. Keep it out of any built-in profile.

tima avatar Aug 02 '22 18:08 tima