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 commands for community group and host VRF #795

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,27 @@ class Snmp_serverArgs(object): # pylint: disable=R0903
"options": {"cache_timeout": {"type": "int"}},
},
"communities": {
"type": "list",
"elements": "dict",
"type": "dict",
"options": {
"name": {"type": "str", "aliases": ["community"]},
"group": {"type": "str"},
"ro": {"type": "bool"},
"rw": {"type": "bool"},
"use_ipv4acl": {"type": "str"},
"use_ipv6acl": {"type": "str"},
"groups": {
"type": "list",
"elements": "dict",
"options": {
"name": {"type": "str", "aliases": ["community"]},
"group": {"type": "str"},
"ro": {"type": "bool"},
"rw": {"type": "bool"},
},
},
"use_acls": {
"type": "list",
"elements": "dict",
"options": {
"name": {"type": "str", "aliases": ["community"]},
"use_ipv4acl": {"type": "str"},
"use_ipv6acl": {"type": "str"},
},
},
},
},
"contact": {"type": "str"},
Expand Down Expand Up @@ -289,23 +301,35 @@ class Snmp_serverArgs(object): # pylint: disable=R0903
},
"global_enforce_priv": {"type": "bool"},
"hosts": {
"type": "list",
"elements": "dict",
"type": "dict",
"options": {
"host": {"type": "str"},
"community": {"type": "str"},
"filter_vrf": {"type": "str"},
"informs": {"type": "bool"},
"source_interface": {"type": "str"},
"traps": {"type": "bool"},
"use_vrf": {"type": "str"},
"version": {
"type": "str",
"choices": ["1", "2c", "3"],
},
"auth": {"type": "str"},
"priv": {"type": "str"},
"udp_port": {"type": "int"},
"sources": {
"type": "list",
"elements": "dict",
"options": {
"host": {"type": "str"},
"community": {"type": "str"},
"filter_vrf": {"type": "str"},
"informs": {"type": "bool"},
"source_interface": {"type": "str"},
"traps": {"type": "bool"},
"version": {
"type": "str",
"choices": ["1", "2c", "3"],
},
"auth": {"type": "str"},
"priv": {"type": "str"},
"udp_port": {"type": "int"},
},
},
"use_vrfs": {
"type": "list",
"elements": "dict",
"options": {
"host": {"type": "str"},
"use_vrf": {"type": "str"},
},
},
},
},
"location": {"type": "str"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,14 @@ def _compare_lists(self, want, have):
"""
Compare list of dictionaries
"""
for x in ["users.auth", "users.use_acls", "hosts", "communities"]:
for x in [
"users.auth",
"users.use_acls",
"hosts.sources",
"hosts.use_vrfs",
"communities.groups",
"communities.use_acls",
]:
wantx = get_from_dict(want, x) or {}
havex = get_from_dict(have, x) or {}
for wkey, wentry in iteritems(wantx):
Expand Down Expand Up @@ -230,7 +237,15 @@ def _build_key(x):

tmp = deepcopy(data)
if "communities" in tmp:
tmp["communities"] = {_build_key(entry): entry for entry in tmp["communities"]}
if "groups" in tmp["communities"]:
tmp["communities"]["groups"] = {
_build_key(entry): entry for entry in tmp["communities"]["groups"]
}
if "use_acls" in tmp["communities"]:
# tmp["communities"]["use_acls"] = {_build_key(entry): entry for entry in tmp["communities"]["use_acls"]}
tmp["communities"]["use_acls"] = {
entry["name"]: entry for entry in tmp["communities"]["use_acls"]
}
if "users" in tmp:
if "auth" in tmp["users"]:
tmp["users"]["auth"] = {_build_key(entry): entry for entry in tmp["users"]["auth"]}
Expand All @@ -239,5 +254,12 @@ def _build_key(x):
entry["user"]: entry for entry in tmp["users"]["use_acls"]
}
if "hosts" in tmp:
tmp["hosts"] = {_build_key(entry): entry for entry in tmp["hosts"]}
if "sources" in tmp["hosts"]:
tmp["hosts"]["sources"] = {
_build_key(entry): entry for entry in tmp["hosts"]["sources"]
}
if "use_vrfs" in tmp["hosts"]:
tmp["hosts"]["use_vrfs"] = {
entry["host"]: entry for entry in tmp["hosts"]["use_vrfs"]
}
return tmp
12 changes: 10 additions & 2 deletions plugins/module_utils/network/nxos/facts/snmp_server/snmp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,17 @@ def populate_facts(self, connection, ansible_facts, data=None):
# parse native config using the Snmp_server template
snmp_server_parser = Snmp_serverTemplate(lines=data.splitlines(), module=self._module)
objs = snmp_server_parser.parse()

if "communities" in objs:
objs["communities"] = sorted(objs["communities"], key=lambda k: to_text(k["name"]))
if "groups" in objs["communities"]:
objs["communities"]["groups"] = sorted(
objs["communities"]["groups"],
key=lambda k: to_text(k["name"]),
)
if "use_acls" in objs["communities"]:
objs["communities"]["use_acls"] = sorted(
objs["communities"]["use_acls"],
key=lambda k: to_text(k["name"]),
)

if "users" in objs:
if "auth" in objs["users"]:
Expand Down
102 changes: 73 additions & 29 deletions plugins/module_utils/network/nxos/rm_templates/snmp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ def _template_hosts(data):
cmd += " traps"
if data.get("informs"):
cmd += " informs"
if data.get("use_vrf"):
cmd += " use-vrf {0}".format(data["use_vrf"])
if data.get("filter_vrf"):
cmd += " filter-vrf {0}".format(data["filter_vrf"])
if data.get("source_interface"):
Expand Down Expand Up @@ -98,32 +96,54 @@ def __init__(self, lines=None, module=None):
},
},
{
"name": "communities",
"name": "communities.groups",
"getval": re.compile(
r"""
^snmp-server
\scommunity\s(?P<community>\S+)
(\sgroup\s(?P<group>\S+))?
$""", re.VERBOSE,
),
"setval": "snmp-server community "
"{{ name }}"
"{{ (' group ' + group) if group is defined else '' }}",
"result": {
"communities": {
"groups": [
{
"name": "{{ community }}",
"group": "{{ group }}",
},
],
},
},
},
{
"name": "communities.use_acls",
"getval": re.compile(
r"""
^snmp-server
\scommunity\s(?P<community>\S+)
(\suse-ipv4acl\s(?P<use_ipv4acl>\S+))?
(\suse-ipv6acl\s(?P<use_ipv6acl>\S+))?
$""", re.VERBOSE,
),
"setval": "snmp-server community "
"{{ name }}"
"{{ (' group ' + group) if group is defined else '' }}"
"{{ (' use-ipv4acl ' + use_ipv4acl) if use_ipv4acl is defined else '' }}"
"{{ (' use-ipv6acl ' + use_ipv6acl) if use_ipv6acl is defined else '' }}"
"{{ ' ro' if ro|d(False) else ''}}"
"{{ ' rw' if rw|d(False) else ''}}",
"result": {
"communities": [
{
"name": "{{ community }}",
"group": "{{ group }}",
"use_ipv4acl": "{{ use_ipv4acl }}",
"use_ipv6acl": "{{ use_ipv6acl }}",
},
],
"communities": {
"use_acls": [
{
"name": "{{ community }}",
"use_ipv4acl": "{{ use_ipv4acl }}",
"use_ipv6acl": "{{ use_ipv6acl }}",
},
],
},
},
},
{
Expand Down Expand Up @@ -1319,34 +1339,58 @@ def __init__(self, lines=None, module=None):
},
},
{
"name": "hosts",
"name": "hosts.sources",
"getval": re.compile(
r"""
^snmp-server
\shost\s(?P<host>\S+)
(\s((?P<traps>traps)|(?P<informs>informs)|(use-vrf\s(?P<use_vrf>\S+)|(filter-vrf\s(?P<filter_vrf>\S+))|(source-interface\s(?P<source_interface>\S+)))))
(\s((?P<traps>traps)|(?P<informs>informs)|(filter-vrf\s(?P<filter_vrf>\S+)|(source-interface\s(?P<source_interface>\S+)))))
(\sversion\s(?P<version>\S+))?
(\s((auth\s(?P<auth>\S+))|(priv\s(?P<priv>\S+))|((?P<community>\S+))))?
(\sudp-port\s(?P<udp_port>\S+))?
$""", re.VERBOSE,
),
"setval": _template_hosts,
"result": {
"hosts": [
{
"host": "{{ host }}",
"community": "{{ community }}",
"filter_vrf": "{{ filter_vrf }}",
"informs": "{{ not not informs }}",
"source_interface": "{{ source_interface }}",
"traps": "{{ not not traps }}",
"use_vrf": "{{ use_vrf }}",
"version": "{{ version }}",
"udp_port": "{{ udp_port }}",
"auth": "{{ auth }}",
"priv": "{{ priv }}",
},
],
"hosts": {
"sources": [
{
"host": "{{ host }}",
"community": "{{ community }}",
"filter_vrf": "{{ filter_vrf }}",
"informs": "{{ not not informs }}",
"source_interface": "{{ source_interface }}",
"traps": "{{ not not traps }}",
"version": "{{ version }}",
"udp_port": "{{ udp_port }}",
"auth": "{{ auth }}",
"priv": "{{ priv }}",
},
],
},
},
},
{
"name": "hosts.use_vrfs",
"getval": re.compile(
r"""
^snmp-server
\shost\s(?P<host>\S+)
(\suse-vrf\s(?P<use_vrf>\S+))?
$""", re.VERBOSE,
),
"setval": "snmp-server host "
"{{ host }}"
"{{ (' use-vrf ' + use_vrf) if use_vrf is defined else '' }}",
"result": {
"hosts": {
"use_vrfs": [
{
"host": "{{ host }}",
"use_vrf": "{{ use_vrf }}",
},
],
},
},
},
{
Expand Down
Loading