-
Notifications
You must be signed in to change notification settings - Fork 8
fix: Avoid append() in sudoers file template #62
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
Conversation
Cause: Ansible 2.19 does not allow calling `.append()` on a list any more (ansible/ansible#85202). You also can't reassign variables in a loop, so list addition does not work either. Consequence: The role failed on Ansible 2.19. Fix: Rewrite the template to using filters instead of explicit dict iteration.
Reviewer's GuideRewritten the sudoers.j2 template to remove deprecated list.append calls and manual accumulation in macros and loops, replacing them with direct Jinja2 filter-based joins and inline conditionals to restore compatibility with Ansible 2.19. Class diagram for updated Jinja2 template macros in sudoers.j2classDiagram
class render_aliases {
+alias_desc
+alias_str
+alias_dict
+alias_subdict_name
+render_aliases(item)
}
class render_override_spec {
+spec
+spec_type
+spec_dict
+sign
+render_override_spec(spec)
}
render_aliases --|> render_override_spec : uses
Flow diagram for sudoers.j2 template rendering logic after refactorflowchart TD
A[Start template rendering] --> B{item.aliases defined?}
B -- Yes --> C[Render aliases using join filter]
B -- No --> D[Skip alias rendering]
C --> E{item.defaults defined?}
D --> E
E -- Yes --> F[Render defaults using join filter]
E -- No --> G[Skip defaults]
F --> H{item.user_specifications defined?}
G --> H
H -- Yes --> I[Render user specifications with inline joins and conditionals]
H -- No --> J[Skip user specifications]
I --> K[Render default_overrides if defined]
J --> K
K --> L[End template rendering]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
I've sat down another hour, also with Claude, GPT, etc. I have a template version which eliminates all macros and hence is very very verbose, and that (almost) works. But this is too frustrating right now. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #62 +/- ##
=======================================
Coverage ? 52.31%
=======================================
Files ? 1
Lines ? 346
Branches ? 0
=======================================
Hits ? 181
Misses ? 165
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@richm so I think the change here is actually valid -- it's required but not sufficient. It additionally needs some toad/crab treatment |
|
[citest] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @martinpitt - I've reviewed your changes and they look great!
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Looks like it - seeing things like |
|
@martinpitt with the two suggested changes, all tests pass with 2.19 on my local system |
Co-authored-by: Richard Megginson <richm@stanfordalumni.org>
|
@richm nice, thanks! Doing a quick CI run, if green I'll squash and update the commit description. |
|
[citest] |
1 similar comment
|
[citest] |
|
Nice! Will squash on merge then. |
|
err, wait, many CI tests are red, but for infra reasons. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @martinpitt - I've reviewed your changes - here's some feedback:
- Ensure the combined if-check in the user_spec macro (for users, hosts, commands) doesn’t accidentally skip specs when optional fields (like operators or tags) are empty—consider handling each optional field separately so you don’t drop entire lines.
- The explicit else blocks that emit an empty string are redundant; you can remove them and rely on Jinja’s whitespace control to avoid unwanted blank lines.
- Review the use of {%- -%} whitespace control to confirm the rendered sudoers file still has the correct line breaks and alignment required by sudoers syntax.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Ensure the combined if-check in the user_spec macro (for users, hosts, commands) doesn’t accidentally skip specs when optional fields (like operators or tags) are empty—consider handling each optional field separately so you don’t drop entire lines.
- The explicit else blocks that emit an empty string are redundant; you can remove them and rely on Jinja’s whitespace control to avoid unwanted blank lines.
- Review the use of {%- -%} whitespace control to confirm the rendered sudoers file still has the correct line breaks and alignment required by sudoers syntax.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
[citest_bad] |
|
That's a case of successful run, but failed overall status. I see the same in linux-system-roles/nbde_server#188 , so presumably an infra outage? The workflow says "gateway timeout" |
Cause: Ansible 2.19 does not allow calling
.append()on a list any more (ansible/ansible#85202). You also can't reassign variables in a loop, so list addition does not work either.Consequence: The role failed on Ansible 2.19.
Fix: Rewrite the template to using filters instead of explicit dict iteration.
This is supposed to fix the Ansible 2.19 failure. It took me hours to get the spacing exactly right, but I developed this against 2.17 (to make sure I don't regress). Turns out in 2.19 it is still making a mess, and I don't know how. Parking my current state as draft PR, today I don't have the energy to work on this further.
Summary by Sourcery
Refactor the sudoers Jinja2 template to eliminate unsupported list .append() calls and rely on built-in filters for list rendering, restoring compatibility with Ansible 2.19 and streamlining template logic.
Bug Fixes:
Enhancements: