Skip to content

fix: ensure idempotence by writing settings in sorted order#295

Merged
richm merged 1 commit intolinux-system-roles:mainfrom
richm:fix-el7-idempotence
Feb 25, 2026
Merged

fix: ensure idempotence by writing settings in sorted order#295
richm merged 1 commit intolinux-system-roles:mainfrom
richm:fix-el7-idempotence

Conversation

@richm
Copy link
Collaborator

@richm richm commented Feb 25, 2026

Cause: On EL7, the underlying module uses an unordered dict, so the order of the settings being
written can change between role invocations.

Consequence: The role may report changed when nothing actually changed except the order.

Fix: The settings are sorted before being written so that the order is consistent.

Result: The role does not report changed when nothing has changed.

Signed-off-by: Rich Megginson rmeggins@redhat.com

Summary by Sourcery

Bug Fixes:

  • Prevent spurious changes being reported by the role by sorting kernel settings before rendering them in the template.

Cause: On EL7, the underlying module uses an unordered dict, so the order of the settings being
written can change between role invocations.

Consequence: The role may report changed when nothing actually changed except the order.

Fix: The settings are sorted before being written so that the order is consistent.

Result: The role does not report changed when nothing has changed.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 25, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensures idempotent generation of kernel settings by iterating over settings keys in sorted order before rendering, preventing spurious changes due to dictionary ordering differences on EL7.

Sequence diagram for idempotent kernel_settings template rendering

sequenceDiagram
    actor User
    participant AnsibleController
    participant TemplateEngine
    participant ManagedNode

    User->>AnsibleController: Run kernel_settings role
    AnsibleController->>TemplateEngine: Render kernel_settings.j2 with settings
    TemplateEngine->>TemplateEngine: Extract settings keys
    TemplateEngine->>TemplateEngine: Sort keys
    loop For each sorted key
        TemplateEngine->>TemplateEngine: Look up val = settings[key]
        alt val.state is absent
            TemplateEngine->>TemplateEngine: Skip writing this setting
        else val.state is not absent
            TemplateEngine->>TemplateEngine: Write line "key = val" in INI section
        end
    end
    TemplateEngine-->>AnsibleController: Rendered kernel_settings content
    AnsibleController->>ManagedNode: Compare and write file if changed
    ManagedNode-->>AnsibleController: No change if only order differed
    AnsibleController-->>User: Report idempotent result (changed only on real change)
Loading

File-Level Changes

Change Details Files
Make kernel settings template iterate over settings in a stable, sorted key order to avoid non-deterministic output.
  • Replace direct iteration over settings.items() with iteration over sorted settings.keys().
  • Lookup each value from the settings dict using the current key after sorting the keys.
  • Preserve existing logic that skips settings marked with state=absent while benefiting from deterministic ordering.
templates/kernel_settings.j2

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Instead of iterating over settings.keys() | sort and then indexing back into the dict, consider using for key, val in settings | dictsort to get a stable, sorted key order in a more idiomatic and readable way.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Instead of iterating over `settings.keys() | sort` and then indexing back into the dict, consider using `for key, val in settings | dictsort` to get a stable, sorted key order in a more idiomatic and readable way.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@richm
Copy link
Collaborator Author

richm commented Feb 25, 2026

[citest]

@richm richm merged commit 251310b into linux-system-roles:main Feb 25, 2026
38 checks passed
@richm richm deleted the fix-el7-idempotence branch February 25, 2026 17:55
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