From 7321ba49903de02e36f8a911f16077392d0fe193 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Mon, 24 Nov 2025 01:17:11 +1300 Subject: [PATCH] snmp_facts: improvements (#11148) * snmp_facts: improvements * require level if vesion=v3 --- .../fragments/11148-snmp-facts-improve.yml | 2 ++ plugins/modules/snmp_facts.py | 21 +++++++------------ 2 files changed, 9 insertions(+), 14 deletions(-) create mode 100644 changelogs/fragments/11148-snmp-facts-improve.yml diff --git a/changelogs/fragments/11148-snmp-facts-improve.yml b/changelogs/fragments/11148-snmp-facts-improve.yml new file mode 100644 index 0000000000..ee26070751 --- /dev/null +++ b/changelogs/fragments/11148-snmp-facts-improve.yml @@ -0,0 +1,2 @@ +minor_changes: + - snmp_facts - simplify and improve code using standard Ansible validations (https://github.com/ansible-collections/community.general/pull/11148). diff --git a/plugins/modules/snmp_facts.py b/plugins/modules/snmp_facts.py index 38da04214f..6a516cb9bd 100644 --- a/plugins/modules/snmp_facts.py +++ b/plugins/modules/snmp_facts.py @@ -249,9 +249,7 @@ def decode_mac(hexstring): def lookup_adminstatus(int_adminstatus): adminstatus_options = {1: "up", 2: "down", 3: "testing"} - if int_adminstatus in adminstatus_options: - return adminstatus_options[int_adminstatus] - return "" + return adminstatus_options.get(int_adminstatus, "") def lookup_operstatus(int_operstatus): @@ -264,9 +262,7 @@ def lookup_operstatus(int_operstatus): 6: "notPresent", 7: "lowerLayerDown", } - if int_operstatus in operstatus_options: - return operstatus_options[int_operstatus] - return "" + return operstatus_options.get(int_operstatus, "") def main(): @@ -288,6 +284,11 @@ def main(): ["username", "level", "integrity", "authkey"], ["privacy", "privkey"], ), + required_if=[ + ("version", "v2", ["community"]), + ("version", "v2c", ["community"]), + ("version", "v3", ["username", "authkey", "level"]), + ], supports_check_mode=True, ) @@ -298,17 +299,9 @@ def main(): cmdGen = cmdgen.CommandGenerator() transport_opts = {k: m_args[k] for k in ("timeout", "retries") if m_args[k] is not None} - # Verify that we receive a community when using snmp v2 - if m_args["version"] in ("v2", "v2c"): - if m_args["community"] is None: - module.fail_json(msg="Community not set when using snmp version 2") - integrity_proto = None privacy_proto = None if m_args["version"] == "v3": - if m_args["username"] is None: - module.fail_json(msg="Username not set when using snmp version 3") - if m_args["level"] == "authPriv" and m_args["privacy"] is None: module.fail_json(msg="Privacy algorithm not set when using authPriv")