-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add diff support to ufw module #10666
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
base: main
Are you sure you want to change the base?
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
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.
Thanks for your contribution! Please add a changelog fragment. Thanks.
I've added some first comments below. Please note that right now, the tests are failing.
params = module.params | ||
|
||
commands = {key: params[key] for key in command_keys if params[key]} | ||
commands = dict((key, params[key]) for key in command_keys if params[key]) |
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.
Please keep the dictionary literal syntax.
cmd.append([params['comment'], "comment '%s'" % params['comment']]) | ||
|
||
rules_dry = execute(cmd) | ||
cmd = [[ufw_bin], ["--dry-run route allow in on foo out on bar from 1.1.1.2 port 7000 to 8.8.8.8 port 7001 proto tcp"]] |
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.
These hard-coded values look wrong. What is this supposed to do?
if not module.check_mode: | ||
post_state = execute([[ufw_bin], ['status'], ['verbose']]) | ||
post_rules = get_current_rules() | ||
if (not changed): |
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.
if (not changed): | |
if not changed: |
if (diff is not None) and (diff["before"] == diff["after"]): | ||
diff = None | ||
|
||
if (changed) and (diff is None): | ||
raise Exception("ufw module reported a change, but no differences detected") | ||
elif (changed is False) and (diff is not None): |
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.
It's unusual to use these many brackets in Python:
if (diff is not None) and (diff["before"] == diff["after"]): | |
diff = None | |
if (changed) and (diff is None): | |
raise Exception("ufw module reported a change, but no differences detected") | |
elif (changed is False) and (diff is not None): | |
if diff is not None and diff["before"] == diff["after"]: | |
diff = None | |
if changed and diff is None: | |
raise Exception("ufw module reported a change, but no differences detected") | |
elif changed is False and diff is not None: |
Hi Felix,
Thank you for your email, and your suggestions.
I will take them all on board and fix them, as well as fixing the tests. I
need to dedicate some time to this, I hope to do so this coming weekend.
Regarding the change log fragment, where should I put that exactly?
The hard coded rule is there to get ufw to print out the current ruleset,
even if ufw is disabled. I'll probably change this to a 'delete rule'.
Please bear with me.
Jort
…On Sat, 16 Aug 2025, 08:38 Felix Fontein, ***@***.***> wrote:
***@***.**** commented on this pull request.
Thanks for your contribution! Please add a changelog fragment
<https://docs.ansible.com/ansible/devel/community/collection_development_process.html#creating-a-changelog-fragment>.
Thanks.
I've added some first comments below. Please note that right now, the
tests are failing.
------------------------------
In plugins/modules/ufw.py
<#10666 (comment)>
:
> params = module.params
- commands = {key: params[key] for key in command_keys if params[key]}
+ commands = dict((key, params[key]) for key in command_keys if params[key])
Please keep the dictionary literal syntax.
------------------------------
In plugins/modules/ufw.py
<#10666 (comment)>
:
> @@ -564,6 +599,15 @@ def ufw_version():
cmd.append([params['comment'], "comment '%s'" % params['comment']])
rules_dry = execute(cmd)
+ cmd = [[ufw_bin], ["--dry-run route allow in on foo out on bar from 1.1.1.2 port 7000 to 8.8.8.8 port 7001 proto tcp"]]
These hard-coded values look wrong. What is this supposed to do?
------------------------------
In plugins/modules/ufw.py
<#10666 (comment)>
:
> @@ -581,16 +625,32 @@ def ufw_version():
changed = True
elif pre_rules != rules_dry:
changed = True
+ else:
+ diff = None
+
+ if not module.check_mode:
+ post_state = execute([[ufw_bin], ['status'], ['verbose']])
+ post_rules = get_current_rules()
+ if (not changed):
⬇️ Suggested change
- if (not changed):
+ if not changed:
------------------------------
In plugins/modules/ufw.py
<#10666 (comment)>
:
> + if (diff is not None) and (diff["before"] == diff["after"]):
+ diff = None
+
+ if (changed) and (diff is None):
+ raise Exception("ufw module reported a change, but no differences detected")
+ elif (changed is False) and (diff is not None):
It's unusual to use these many brackets in Python:
⬇️ Suggested change
- if (diff is not None) and (diff["before"] == diff["after"]):
- diff = None
-
- if (changed) and (diff is None):
- raise Exception("ufw module reported a change, but no differences detected")
- elif (changed is False) and (diff is not None):
+ if diff is not None and diff["before"] == diff["after"]:
+ diff = None
+
+ if changed and diff is None:
+ raise Exception("ufw module reported a change, but no differences detected")
+ elif changed is False and diff is not None:
—
Reply to this email directly, view it on GitHub
<#10666 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BV7NOU67276R3JGB57FIHDT3NZALZAVCNFSM6AAAAACD3E2BOOVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZTCMRVGA3TINBXGE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***
.com>
|
SUMMARY
minor_changes:
ISSUE TYPE
COMPONENT NAME
ufw
ADDITIONAL INFORMATION
before this code change:
after this code change: