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

fix: ensure role is idempotent and supports check mode #41

Merged
merged 1 commit into from
Feb 20, 2024

Conversation

richm
Copy link
Contributor

@richm richm commented Feb 20, 2024

Cause: Role was not idempotent, and did not support check mode.

Consequence: Users could not use role to ensure idempotence or
check state.

Fix: The role has been fixed to return "changed": true if the
role changed the state of the system, and false otherwise. This
means that some operations that used to return an error, such as
trying to snapshot a volume that already had a snapshot, or trying
to remove a snapshot that does not exist, will no longer return
an error, but will instead just report "changed": false. Users
should use the list operation to examine state of the system.

Result: The role is idempotent and supports check mode.

Signed-off-by: Rich Megginson [email protected]

@richm richm requested a review from spetrosi as a code owner February 20, 2024 16:22
tasks/files/snapshot.py Fixed Show fixed Hide fixed
tasks/files/snapshot.py Fixed Show fixed Hide fixed
tasks/files/snapshot.py Fixed Show fixed Hide fixed
tasks/files/snapshot.py Fixed Show fixed Hide fixed
tasks/files/snapshot.py Fixed Show fixed Hide fixed
tasks/files/snapshot.py Fixed Show fixed Hide fixed
tasks/files/snapshot.py Fixed Show fixed Hide fixed
tasks/files/snapshot.py Fixed Show fixed Hide fixed
tasks/files/snapshot.py Fixed Show fixed Hide fixed
tasks/files/snapshot.py Fixed Show fixed Hide fixed
tasks/files/snapshot.py Fixed Show fixed Hide fixed
tasks/files/snapshot.py Fixed Show fixed Hide fixed
@richm
Copy link
Contributor Author

richm commented Feb 20, 2024

[citest]

Copy link
Contributor

@spetrosi spetrosi left a comment

Choose a reason for hiding this comment

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

Massive work, Rich!


- name: Print out response
debug:
var: snapshot_cmd.stdout
var: snapshot_cmd_raw
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var: snapshot_cmd_raw
var: snapshot_cmd_raw.stdout

Should it print only stdout as it was before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No - would prefer to see all of the fields rather than just stdout

tasks/main.yml Outdated Show resolved Hide resolved
tasks/main.yml Outdated Show resolved Hide resolved

- name: Assert no changes for umount
assert:
that: not snapshot_cmd["changed"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool way to test idempotency!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do this in a few roles - have the module (or script in this case) return "changed" as a variable that can be used elsewhere - unfortunately Ansible doesn't expose the internal "changed" counter or state

snapshot_cmd.stderr_lines is search(__snapshot_failed_regex)
- snapshot_cmd_raw is failed
- snapshot_cmd["return_code"] != 0
- snapshot_cmd["errors"] is search(__snapshot_failed_regex)
msg: "{{ __snapshot_failed_msg }}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
msg: "{{ __snapshot_failed_msg }}"
var: __snapshot_failed_msg

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like var is not a valid parameter for assert

    _VALID_ARGS = frozenset(('fail_msg', 'msg', 'quiet', 'success_msg', 'that'))

msg is an alias for fail_msg

Cause: Role was not idempotent, and did not support check mode.

Consequence: Users could not use role to ensure idempotence or
check state.

Fix: The role has been fixed to return `"changed": true` if the
role changed the state of the system, and `false` otherwise.  This
means that some operations that used to return an error, such as
trying to snapshot a volume that already had a snapshot, or trying
to remove a snapshot that does not exist, will no longer return
an error, but will instead just report `"changed": false`.  Users
should use the `list` operation to examine state of the system.

Result: The role is idempotent and supports check mode.

Signed-off-by: Rich Megginson <[email protected]>
@richm richm merged commit 437b4db into linux-system-roles:main Feb 20, 2024
17 checks passed
@richm richm deleted the idempotence branch February 20, 2024 20:25
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