Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ansible-lint support #54

Open
wants to merge 63 commits into
base: main
Choose a base branch
from
Open

Conversation

lilatomic
Copy link
Contributor

@lilatomic lilatomic commented Apr 6, 2022

I think this is pretty decent for ansible-lint support. I dropped the generator stuff from main, I'll reintroduce it later. Currently it does something similar but much more jank by fulling in a slightly tailored **/*

Features:

  • ansible-lint support
  • support for other Ansible artifacts (Playbook&context, Role, Collection)

Tests:

  • some end2end tests
  • capture the helloansible directory to test resources

Deferred:

  • Use source generators instead of fancy globs

sureshjoshi and others added 30 commits March 30, 2022 12:24
- Created a check goal for an ansible playbook (--syntax-check)
ansible-lint will helpfully also pull in `ansible` as a module
if there is an 'ansible' in the same venv.
However, this isn't a general solution for running other binaries from the same venv.
 I would rather force using more robust features of PEX or the toolchain
so that it's easier to use this as a base for other plugins
or rules within this plugin
This option isn't plumbed with the VenvPexRequest,
so I just use the PexRequest with additional args which do the same.
VenvPexRequest mostly just adds the `--venv` argument to the PEX creation,
so using the PexRequest in this way is mostly equivalent anyhow.

`--venv prepend` prepends the binaries to the PATH, so they're discoverable
- new way to get sources
- we no longer need to strip the top-level directory
required adding wrapping type to keep graph separate
the playbook itself is already included as a source
they're wrong, but at least they'll be easier to fix
@sureshjoshi
Copy link
Owner

Wow, that's a big PR :)

rules and target_types merged too messily
using the envvar ANSIBLE_COLLECTIONS_PATHS,
we can use the installed collections
regardless of the location of the playbook
and without relying on the local collections path.
This also means that we don't have to relocate playbooks.
Keeping playbooks in their positions in the repo
makes relative references cleaner.
For example, importing from /envs/prod/vars.yml
@lilatomic lilatomic marked this pull request as ready for review April 7, 2022 00:49
@lilatomic
Copy link
Contributor Author

Wow, that's a big PR :)

yeah it, uh, got away a bit. Some of it is ansible test code.

@lilatomic
Copy link
Contributor Author

Heya, I've merged HEAD into this branch, a rebase was getting messy fast.
There are some open mypy problems, most of them seems like Type Tetris related (some pants things aren't exported; I'm having trouble with some of the config settings like StrOption). LMK if you want me to work harder on those.

the *Option types are only necessary on the Subsystem class (as classvars),
not dataclasses created from their values
@sureshjoshi
Copy link
Owner

Okay, so - in the process of reviewing. Is there a mechanism to NOT take and entire subfolder and have it owned by Ansible? Like, can the list of sources be trimmed or filtered? I'm just trying to get my head around some of the Pants ownership semantics

@sureshjoshi
Copy link
Owner

Also, I might split this PR up into a couple of them, as there's a lot of intermingled items - so it's a bit tricky to grok all the changes.

@lilatomic
Copy link
Contributor Author

Okay, so - in the process of reviewing. Is there a mechanism to NOT take and entire subfolder and have it owned by Ansible? Like, can the list of sources be trimmed or filtered? I'm just trying to get my head around some of the Pants ownership semantics

do you mean on the pants side, or the ansible side? I'm pretty sure there isn't a way to affect the ansible file discovery, and all files are technically fair game for ansible. That said, there's nothing stopping us from restricting pants to only pull in a constrained subset. We could also add tailor support, so it's not so kludgy to request that people add eg role_defaults() to a role's defaults/BUILD file; that would really help constrain things, and make it more reasonable to not pull in every file ansible might.

I haven't really read up on implementing tailor or on using target generation or any of that. I'm sure they'd provide benefits over the globs I have here, but also would make this MR even bigger.

Speaking of size, LMK if there's a way you'd like this broken up and I can help with that. I'm a history packrat so I didn't see a way to disentangle this, but if you'd like me to try again but more segmented this time I can give it a shot.

@sureshjoshi
Copy link
Owner

do you mean on the pants side, or the ansible side? I'm pretty sure there isn't a way to affect the ansible file discovery, and all files are technically fair game for ansible. That said, there's nothing stopping us from restricting pants to only pull in a constrained subset. We could also add tailor support, so it's not so kludgy to request that people add eg role_defaults() to a role's defaults/BUILD file; that would really help constrain things, and make it more reasonable to not pull in every file ansible might.

I meant on the Pants side with a sources filter. I used a glob previously for simplicity, but it allows the end-user to add/remove whatever they want and constrain as needed.

I haven't really read up on implementing tailor or on using target generation or any of that. I'm sure they'd provide benefits over the globs I have here, but also would make this MR even bigger.

Can be added as a different PR. I implemented it in a couple language targets, and if we can constrain the obvious ansible files (which is hard, as it's more free-form), it's surprisingly trivial to add tailor support!

Speaking of size, LMK if there's a way you'd like this broken up and I can help with that. I'm a history packrat so I didn't see a way to disentangle this, but if you'd like me to try again but more segmented this time I can give it a shot.

I've started this split by at least moving the most logical, isolated block (the ansible-lint tool itself) into a separate PR. We can always add to it when targets get changed and all of that. If the test coverage is in place, there won't be an accidental breakage.

I was trying to add you as a reviewer to this - but couldn't last night. I gave up and went to bed instead.
#64

Incomplete PR, as I haven't finished pulling in your testing. I've also tried to line it up with some of the other formatters/linters I've added - so there is consistency (and it should also be consistent with Pants mainline linters too).

There's currently a bug somewhere I think, as I'm only seeing a subset of lint bugs, but I haven't looked into it much.

Once that PR is merged, we can pull it in, and then have a PR for each Ansible "type" (role, collection, galaxy support + testing). That way each PR is pretty constrained in scope and easy to review, and test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants