update snmp modules to fix bugs for cloudengine (#57025)
* update snmp modules to fix bugs for cloudengine * update snmp modules to fix bugs for cloudengine * update "ce_snmp_contact", list may be out range
This commit is contained in:
parent
17c475b101
commit
5ac3661176
6 changed files with 336 additions and 211 deletions
|
@ -236,7 +236,7 @@ CE_MERGE_SNMP_V3_GROUP_TAIL = """
|
|||
</snmpv3Group>
|
||||
</snmpv3Groups>
|
||||
</snmp>
|
||||
</filter>
|
||||
</config>
|
||||
"""
|
||||
# create snmp v3 group
|
||||
CE_CREATE_SNMP_V3_GROUP_HEADER = """
|
||||
|
@ -251,7 +251,7 @@ CE_CREATE_SNMP_V3_GROUP_TAIL = """
|
|||
</snmpv3Group>
|
||||
</snmpv3Groups>
|
||||
</snmp>
|
||||
</filter>
|
||||
</config>
|
||||
"""
|
||||
# delete snmp v3 group
|
||||
CE_DELETE_SNMP_V3_GROUP_HEADER = """
|
||||
|
@ -266,7 +266,7 @@ CE_DELETE_SNMP_V3_GROUP_TAIL = """
|
|||
</snmpv3Group>
|
||||
</snmpv3Groups>
|
||||
</snmp>
|
||||
</filter>
|
||||
</config>
|
||||
"""
|
||||
|
||||
|
||||
|
@ -344,7 +344,7 @@ class SnmpCommunity(object):
|
|||
replace('xmlns="http://www.huawei.com/netconf/vrp"', "")
|
||||
|
||||
root = ElementTree.fromstring(xml_str)
|
||||
community_info = root.findall("data/snmp/communitys/community")
|
||||
community_info = root.findall("snmp/communitys/community")
|
||||
if community_info:
|
||||
for tmp in community_info:
|
||||
tmp_dict = dict()
|
||||
|
@ -355,35 +355,63 @@ class SnmpCommunity(object):
|
|||
result["community_info"].append(tmp_dict)
|
||||
|
||||
if result["community_info"]:
|
||||
community_name_list = list()
|
||||
for tmp in result["community_info"]:
|
||||
if "communityName" in tmp.keys():
|
||||
need_cfg = True
|
||||
community_name_list.append(tmp["communityName"])
|
||||
|
||||
if "accessRight" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["accessRight"] != access_right:
|
||||
need_cfg = True
|
||||
if community_name not in community_name_list:
|
||||
need_cfg = True
|
||||
else:
|
||||
need_cfg_bool = True
|
||||
|
||||
for tmp in result["community_info"]:
|
||||
if tmp["communityName"] == community_name:
|
||||
|
||||
cfg_bool_list = list()
|
||||
|
||||
if access_right:
|
||||
if "accessRight" in tmp.keys():
|
||||
need_cfg_access = False
|
||||
if tmp["accessRight"] != access_right:
|
||||
need_cfg_access = True
|
||||
else:
|
||||
need_cfg_access = True
|
||||
|
||||
cfg_bool_list.append(need_cfg_access)
|
||||
|
||||
if acl_number:
|
||||
if "aclNumber" in tmp.keys():
|
||||
need_cfg_acl = False
|
||||
if tmp["aclNumber"] != acl_number:
|
||||
need_cfg_acl = True
|
||||
else:
|
||||
need_cfg_acl = True
|
||||
|
||||
cfg_bool_list.append(need_cfg_acl)
|
||||
|
||||
if community_mib_view:
|
||||
if "mibViewName" in tmp.keys():
|
||||
need_cfg_mib = False
|
||||
if tmp["mibViewName"] != community_mib_view:
|
||||
need_cfg_mib = True
|
||||
else:
|
||||
need_cfg_mib = True
|
||||
cfg_bool_list.append(need_cfg_mib)
|
||||
|
||||
if True not in cfg_bool_list:
|
||||
need_cfg_bool = False
|
||||
|
||||
if state == "present":
|
||||
if not need_cfg_bool:
|
||||
need_cfg = False
|
||||
else:
|
||||
if tmp["accessRight"] == access_right:
|
||||
need_cfg = True
|
||||
|
||||
if acl_number:
|
||||
if "aclNumber" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["aclNumber"] != acl_number:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["aclNumber"] == acl_number:
|
||||
need_cfg = True
|
||||
|
||||
if community_mib_view:
|
||||
if "mibViewName" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["mibViewName"] != community_mib_view:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["mibViewName"] == community_mib_view:
|
||||
need_cfg = True
|
||||
need_cfg = True
|
||||
else:
|
||||
if not need_cfg_bool:
|
||||
need_cfg = True
|
||||
else:
|
||||
need_cfg = False
|
||||
|
||||
result["need_cfg"] = need_cfg
|
||||
return result
|
||||
|
@ -464,7 +492,7 @@ class SnmpCommunity(object):
|
|||
replace('xmlns="http://www.huawei.com/netconf/vrp"', "")
|
||||
|
||||
root = ElementTree.fromstring(xml_str)
|
||||
group_info = root.findall("data/snmp/snmpv3Groups/snmpv3Group")
|
||||
group_info = root.findall("snmp/snmpv3Groups/snmpv3Group")
|
||||
if group_info:
|
||||
for tmp in group_info:
|
||||
tmp_dict = dict()
|
||||
|
@ -476,58 +504,83 @@ class SnmpCommunity(object):
|
|||
result["group_info"].append(tmp_dict)
|
||||
|
||||
if result["group_info"]:
|
||||
group_name_list = list()
|
||||
|
||||
for tmp in result["group_info"]:
|
||||
if "groupName" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["groupName"] != group_name:
|
||||
need_cfg = True
|
||||
group_name_list.append(tmp["groupName"])
|
||||
if group_name not in group_name_list:
|
||||
if state == "present":
|
||||
need_cfg = True
|
||||
else:
|
||||
need_cfg = False
|
||||
else:
|
||||
need_cfg_bool = True
|
||||
for tmp in result["group_info"]:
|
||||
if tmp["groupName"] == group_name:
|
||||
|
||||
cfg_bool_list = list()
|
||||
|
||||
if security_level:
|
||||
if "securityLevel" in tmp.keys():
|
||||
need_cfg_group = False
|
||||
if tmp["securityLevel"] != security_level:
|
||||
need_cfg_group = True
|
||||
else:
|
||||
need_cfg_group = True
|
||||
|
||||
cfg_bool_list.append(need_cfg_group)
|
||||
|
||||
if acl_number:
|
||||
if "aclNumber" in tmp.keys():
|
||||
need_cfg_acl = False
|
||||
if tmp["aclNumber"] != acl_number:
|
||||
need_cfg_acl = True
|
||||
else:
|
||||
need_cfg_acl = True
|
||||
|
||||
cfg_bool_list.append(need_cfg_acl)
|
||||
|
||||
if read_view:
|
||||
if "readViewName" in tmp.keys():
|
||||
need_cfg_read = False
|
||||
if tmp["readViewName"] != read_view:
|
||||
need_cfg_read = True
|
||||
else:
|
||||
need_cfg_read = True
|
||||
cfg_bool_list.append(need_cfg_read)
|
||||
|
||||
if write_view:
|
||||
if "writeViewName" in tmp.keys():
|
||||
need_cfg_write = False
|
||||
if tmp["writeViewName"] != write_view:
|
||||
need_cfg_write = True
|
||||
else:
|
||||
need_cfg_write = True
|
||||
cfg_bool_list.append(need_cfg_write)
|
||||
|
||||
if notify_view:
|
||||
if "notifyViewName" in tmp.keys():
|
||||
need_cfg_notify = False
|
||||
if tmp["notifyViewName"] != notify_view:
|
||||
need_cfg_notify = True
|
||||
else:
|
||||
need_cfg_notify = True
|
||||
cfg_bool_list.append(need_cfg_notify)
|
||||
|
||||
if True not in cfg_bool_list:
|
||||
need_cfg_bool = False
|
||||
|
||||
if state == "present":
|
||||
if not need_cfg_bool:
|
||||
need_cfg = False
|
||||
else:
|
||||
if tmp["groupName"] == group_name:
|
||||
need_cfg = True
|
||||
|
||||
if "securityLevel" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["securityLevel"] != security_level:
|
||||
need_cfg = True
|
||||
need_cfg = True
|
||||
else:
|
||||
if not need_cfg_bool:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["securityLevel"] == security_level:
|
||||
need_cfg = True
|
||||
|
||||
if acl_number:
|
||||
if "aclNumber" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["aclNumber"] != acl_number:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["aclNumber"] == acl_number:
|
||||
need_cfg = True
|
||||
|
||||
if read_view:
|
||||
if "readViewName" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["readViewName"] != read_view:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["readViewName"] == read_view:
|
||||
need_cfg = True
|
||||
|
||||
if write_view:
|
||||
if "writeViewName" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["writeViewName"] != write_view:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["writeViewName"] == write_view:
|
||||
need_cfg = True
|
||||
|
||||
if notify_view:
|
||||
if "notifyViewName" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["notifyViewName"] != notify_view:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["notifyViewName"] == notify_view:
|
||||
need_cfg = True
|
||||
need_cfg = False
|
||||
|
||||
result["need_cfg"] = need_cfg
|
||||
return result
|
||||
|
@ -892,16 +945,17 @@ def main():
|
|||
end_tmp = dict()
|
||||
for item in snmp_community_rst:
|
||||
if item != "need_cfg":
|
||||
exist_tmp[item] = snmp_community_rst[item]
|
||||
end_tmp[item] = snmp_community_rst[item]
|
||||
end_tmp[item] = snmp_community_rst[item]
|
||||
if end_tmp:
|
||||
end_state["snmp community"] = end_tmp
|
||||
# state exist snmp v3 group config
|
||||
# state end snmp v3 group config
|
||||
snmp_v3_group_rst = snmp_community_obj.check_snmp_v3_group_args(
|
||||
module=module)
|
||||
end_tmp = dict()
|
||||
for item in snmp_v3_group_rst:
|
||||
if item != "need_cfg":
|
||||
exist_tmp[item] = snmp_v3_group_rst[item]
|
||||
end_tmp[item] = snmp_v3_group_rst[item]
|
||||
if end_tmp:
|
||||
end_state["snmp v3 group"] = end_tmp
|
||||
|
||||
|
|
|
@ -100,7 +100,7 @@ updates:
|
|||
'''
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.network.cloudengine.ce import get_config, load_config, ce_argument_spec
|
||||
from ansible.module_utils.network.cloudengine.ce import exec_command, load_config, ce_argument_spec
|
||||
|
||||
|
||||
class SnmpContact(object):
|
||||
|
@ -140,6 +140,22 @@ class SnmpContact(object):
|
|||
self.module.fail_json(
|
||||
msg='Error: The len of contact is 0.')
|
||||
|
||||
def get_config(self, flags=None):
|
||||
"""Retrieves the current config from the device or cache
|
||||
"""
|
||||
flags = [] if flags is None else flags
|
||||
|
||||
cmd = 'display current-configuration '
|
||||
cmd += ' '.join(flags)
|
||||
cmd = cmd.strip()
|
||||
|
||||
rc, out, err = exec_command(self.module, cmd)
|
||||
if rc != 0:
|
||||
self.module.fail_json(msg=err)
|
||||
cfg = str(out).strip()
|
||||
|
||||
return cfg
|
||||
|
||||
def get_proposed(self):
|
||||
""" Get proposed state """
|
||||
|
||||
|
@ -154,8 +170,9 @@ class SnmpContact(object):
|
|||
tmp_cfg = self.cli_get_config()
|
||||
if tmp_cfg:
|
||||
temp_data = tmp_cfg.split(r"contact ")
|
||||
self.cur_cfg["contact"] = temp_data[1]
|
||||
self.existing["contact"] = temp_data[1]
|
||||
if len(temp_data) > 1:
|
||||
self.cur_cfg["contact"] = temp_data[1]
|
||||
self.existing["contact"] = temp_data[1]
|
||||
|
||||
def get_end_state(self):
|
||||
""" Get end state """
|
||||
|
@ -163,7 +180,8 @@ class SnmpContact(object):
|
|||
tmp_cfg = self.cli_get_config()
|
||||
if tmp_cfg:
|
||||
temp_data = tmp_cfg.split(r"contact ")
|
||||
self.end_state["contact"] = temp_data[1]
|
||||
if len(temp_data) > 1:
|
||||
self.end_state["contact"] = temp_data[1]
|
||||
|
||||
def cli_load_config(self, commands):
|
||||
""" Load configure by cli """
|
||||
|
@ -177,7 +195,7 @@ class SnmpContact(object):
|
|||
regular = "| include snmp | include contact"
|
||||
flags = list()
|
||||
flags.append(regular)
|
||||
tmp_cfg = get_config(self.module, flags)
|
||||
tmp_cfg = self.get_config(flags)
|
||||
|
||||
return tmp_cfg
|
||||
|
||||
|
|
|
@ -101,7 +101,7 @@ updates:
|
|||
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.network.cloudengine.ce import get_config, load_config, ce_argument_spec
|
||||
from ansible.module_utils.network.cloudengine.ce import exec_command, load_config, ce_argument_spec
|
||||
|
||||
|
||||
class SnmpLocation(object):
|
||||
|
@ -141,6 +141,22 @@ class SnmpLocation(object):
|
|||
self.module.fail_json(
|
||||
msg='Error: The len of location is 0.')
|
||||
|
||||
def get_config(self, flags=None):
|
||||
"""Retrieves the current config from the device or cache
|
||||
"""
|
||||
flags = [] if flags is None else flags
|
||||
|
||||
cmd = 'display current-configuration '
|
||||
cmd += ' '.join(flags)
|
||||
cmd = cmd.strip()
|
||||
|
||||
rc, out, err = exec_command(self.module, cmd)
|
||||
if rc != 0:
|
||||
self.module.fail_json(msg=err)
|
||||
cfg = str(out).strip()
|
||||
|
||||
return cfg
|
||||
|
||||
def get_proposed(self):
|
||||
""" Get proposed state """
|
||||
|
||||
|
@ -178,7 +194,7 @@ class SnmpLocation(object):
|
|||
regular = "| include snmp | include location"
|
||||
flags = list()
|
||||
flags.append(regular)
|
||||
tmp_cfg = get_config(self.module, flags)
|
||||
tmp_cfg = self.get_config(flags)
|
||||
|
||||
return tmp_cfg
|
||||
|
||||
|
|
|
@ -148,7 +148,7 @@ updates:
|
|||
from xml.etree import ElementTree
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.network.cloudengine.ce import get_nc_config, set_nc_config, \
|
||||
ce_argument_spec, get_config, load_config, check_ip_addr
|
||||
ce_argument_spec, load_config, check_ip_addr
|
||||
|
||||
# get snmp version
|
||||
CE_GET_SNMP_VERSION = """
|
||||
|
@ -231,6 +231,29 @@ CE_DELETE_SNMP_TARGET_HOST_TAIL = """
|
|||
</config>
|
||||
"""
|
||||
|
||||
# get snmp listen port
|
||||
CE_GET_SNMP_PORT = """
|
||||
<filter type="subtree">
|
||||
<snmp xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
|
||||
<systemCfg>
|
||||
<snmpListenPort></snmpListenPort>
|
||||
</systemCfg>
|
||||
</snmp>
|
||||
</filter>
|
||||
"""
|
||||
|
||||
# merge snmp listen port
|
||||
CE_MERGE_SNMP_PORT = """
|
||||
<config>
|
||||
<snmp xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
|
||||
<systemCfg operation="merge">
|
||||
<snmpListenPort>%s</snmpListenPort>
|
||||
</systemCfg>
|
||||
</snmp>
|
||||
</config>
|
||||
"""
|
||||
|
||||
|
||||
INTERFACE_TYPE = ['ethernet', 'eth-trunk', 'tunnel', 'null', 'loopback',
|
||||
'vlanif', '100ge', '40ge', 'mtunnel', '10ge', 'ge', 'meth', 'vbdif', 'nve']
|
||||
|
||||
|
@ -406,7 +429,7 @@ class SnmpTargetHost(object):
|
|||
|
||||
root = ElementTree.fromstring(xml_str)
|
||||
target_host_info = root.findall(
|
||||
"data/snmp/targetHosts/targetHost")
|
||||
"snmp/targetHosts/targetHost")
|
||||
if target_host_info:
|
||||
for tmp in target_host_info:
|
||||
tmp_dict = dict()
|
||||
|
@ -470,7 +493,7 @@ class SnmpTargetHost(object):
|
|||
same_flag = False
|
||||
|
||||
if "interface-name" in tmp.keys():
|
||||
if tmp["interface-name"] != self.interface_name:
|
||||
if tmp["interface-name"].lower() != self.interface_name.lower():
|
||||
same_flag = False
|
||||
|
||||
if same_flag:
|
||||
|
@ -509,7 +532,7 @@ class SnmpTargetHost(object):
|
|||
replace('xmlns="http://www.huawei.com/netconf/vrp"', "")
|
||||
|
||||
root = ElementTree.fromstring(xml_str)
|
||||
version_info = root.find("data/snmp/engine")
|
||||
version_info = root.find("snmp/engine")
|
||||
if version_info:
|
||||
for site in version_info:
|
||||
if site.tag in ["version"]:
|
||||
|
@ -517,15 +540,24 @@ class SnmpTargetHost(object):
|
|||
|
||||
return version
|
||||
|
||||
def cli_get_connect_port(self):
|
||||
""" Get connect port by cli """
|
||||
def xml_get_connect_port(self):
|
||||
""" Get connect port by xml """
|
||||
tmp_cfg = None
|
||||
conf_str = CE_GET_SNMP_PORT
|
||||
recv_xml = self.netconf_get_config(conf_str=conf_str)
|
||||
if "<data/>" in recv_xml:
|
||||
pass
|
||||
else:
|
||||
xml_str = recv_xml.replace('\r', '').replace('\n', '').\
|
||||
replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\
|
||||
replace('xmlns="http://www.huawei.com/netconf/vrp"', "")
|
||||
|
||||
regular = "| include snmp | include snmp-agent udp-port"
|
||||
flags = list()
|
||||
flags.append(regular)
|
||||
tmp_cfg = get_config(self.module, flags)
|
||||
root = ElementTree.fromstring(xml_str)
|
||||
snmp_port_info = root.findall("snmp/systemCfg/snmpListenPort")
|
||||
|
||||
return tmp_cfg
|
||||
if snmp_port_info:
|
||||
tmp_cfg = snmp_port_info[0].text
|
||||
return tmp_cfg
|
||||
|
||||
def get_proposed(self):
|
||||
""" Get proposed state """
|
||||
|
@ -569,11 +601,10 @@ class SnmpTargetHost(object):
|
|||
self.existing["version"] = version
|
||||
|
||||
if self.connect_port:
|
||||
tmp_cfg = self.cli_get_connect_port()
|
||||
tmp_cfg = self.xml_get_connect_port()
|
||||
if tmp_cfg:
|
||||
temp_data = tmp_cfg.split(r"udp-port ")
|
||||
self.cur_cli_cfg["connect port"] = temp_data[1]
|
||||
self.existing["connect port"] = temp_data[1]
|
||||
self.cur_cli_cfg["connect port"] = tmp_cfg
|
||||
self.existing["connect port"] = tmp_cfg
|
||||
|
||||
if self.host_name:
|
||||
self.existing["target host info"] = self.cur_netconf_cfg[
|
||||
|
@ -588,10 +619,9 @@ class SnmpTargetHost(object):
|
|||
self.end_state["version"] = version
|
||||
|
||||
if self.connect_port:
|
||||
tmp_cfg = self.cli_get_connect_port()
|
||||
tmp_cfg = self.xml_get_connect_port()
|
||||
if tmp_cfg:
|
||||
temp_data = tmp_cfg.split(r"udp-port ")
|
||||
self.end_state["connect port"] = temp_data[1]
|
||||
self.end_state["connect port"] = tmp_cfg
|
||||
|
||||
if self.host_name:
|
||||
self.end_state["target host info"] = self.end_netconf_cfg[
|
||||
|
@ -640,8 +670,8 @@ class SnmpTargetHost(object):
|
|||
self.cli_load_config(cmds)
|
||||
self.changed = True
|
||||
|
||||
def config_connect_port_cli(self):
|
||||
""" Config connect port by cli """
|
||||
def config_connect_port_xml(self):
|
||||
""" Config connect port by xml """
|
||||
|
||||
if "connect port" in self.cur_cli_cfg.keys():
|
||||
if self.cur_cli_cfg["connect port"] == self.connect_port:
|
||||
|
@ -653,7 +683,8 @@ class SnmpTargetHost(object):
|
|||
cmds.append(cmd)
|
||||
|
||||
self.updates_cmd.append(cmd)
|
||||
self.cli_load_config(cmds)
|
||||
conf_str = CE_MERGE_SNMP_PORT % self.connect_port
|
||||
self.netconf_set_config(conf_str=conf_str)
|
||||
self.changed = True
|
||||
else:
|
||||
cmd = "snmp-agent udp-port %s" % self.connect_port
|
||||
|
@ -662,7 +693,8 @@ class SnmpTargetHost(object):
|
|||
cmds.append(cmd)
|
||||
|
||||
self.updates_cmd.append(cmd)
|
||||
self.cli_load_config(cmds)
|
||||
conf_str = CE_MERGE_SNMP_PORT % self.connect_port
|
||||
self.netconf_set_config(conf_str=conf_str)
|
||||
self.changed = True
|
||||
|
||||
def undo_config_connect_port_cli(self):
|
||||
|
@ -678,7 +710,9 @@ class SnmpTargetHost(object):
|
|||
cmds.append(cmd)
|
||||
|
||||
self.updates_cmd.append(cmd)
|
||||
self.cli_load_config(cmds)
|
||||
connect_port = "161"
|
||||
conf_str = CE_MERGE_SNMP_PORT % connect_port
|
||||
self.netconf_set_config(conf_str=conf_str)
|
||||
self.changed = True
|
||||
|
||||
def merge_snmp_target_host(self):
|
||||
|
@ -843,7 +877,7 @@ class SnmpTargetHost(object):
|
|||
if self.version != self.cur_cli_cfg["version"]:
|
||||
self.merge_snmp_version()
|
||||
if self.connect_port:
|
||||
self.config_connect_port_cli()
|
||||
self.config_connect_port_xml()
|
||||
if self.cur_netconf_cfg["need_cfg"]:
|
||||
self.merge_snmp_target_host()
|
||||
|
||||
|
|
|
@ -128,6 +128,7 @@ updates:
|
|||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.network.cloudengine.ce import get_config, load_config, ce_argument_spec, run_commands
|
||||
from ansible.module_utils.connection import exec_command
|
||||
|
||||
|
||||
class SnmpTraps(object):
|
||||
|
@ -175,6 +176,22 @@ class SnmpTraps(object):
|
|||
commands.append(cmd1)
|
||||
self.interface = run_commands(self.module, commands)
|
||||
|
||||
def get_config(self, flags=None):
|
||||
"""Retrieves the current config from the device or cache
|
||||
"""
|
||||
flags = [] if flags is None else flags
|
||||
|
||||
cmd = 'display current-configuration '
|
||||
cmd += ' '.join(flags)
|
||||
cmd = cmd.strip()
|
||||
|
||||
rc, out, err = exec_command(self.module, cmd)
|
||||
if rc != 0:
|
||||
self.module.fail_json(msg=err)
|
||||
cfg = str(out).strip()
|
||||
|
||||
return cfg
|
||||
|
||||
def check_args(self):
|
||||
""" Check invalid args """
|
||||
|
||||
|
@ -304,7 +321,7 @@ class SnmpTraps(object):
|
|||
regular = "| include snmp | include trap"
|
||||
flags = list()
|
||||
flags.append(regular)
|
||||
tmp_cfg = get_config(self.module, flags)
|
||||
tmp_cfg = self.get_config(flags)
|
||||
|
||||
return tmp_cfg
|
||||
|
||||
|
|
|
@ -151,8 +151,8 @@ updates:
|
|||
|
||||
from xml.etree import ElementTree
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.network.cloudengine.ce import get_nc_config, set_nc_config, ce_argument_spec, get_config
|
||||
|
||||
from ansible.module_utils.network.cloudengine.ce import get_nc_config, set_nc_config
|
||||
from ansible.module_utils.network.cloudengine.ce import ce_argument_spec
|
||||
|
||||
# get snmp v3 USM user
|
||||
CE_GET_SNMP_V3_USM_USER_HEADER = """
|
||||
|
@ -283,6 +283,16 @@ CE_DELETE_SNMP_V3_LOCAL_USER = """
|
|||
</snmp>
|
||||
</config>
|
||||
"""
|
||||
# display info
|
||||
GET_SNMP_LOCAL_ENGINE = """
|
||||
<filter type="subtree">
|
||||
<snmp xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
|
||||
<engine>
|
||||
<engineID></engineID>
|
||||
</engine>
|
||||
</snmp>
|
||||
</filter>
|
||||
"""
|
||||
|
||||
|
||||
class SnmpUser(object):
|
||||
|
@ -313,7 +323,6 @@ class SnmpUser(object):
|
|||
|
||||
module = kwargs["module"]
|
||||
result = dict()
|
||||
result["usm_user_info"] = []
|
||||
need_cfg = False
|
||||
state = module.params['state']
|
||||
usm_user_name = module.params['usm_user_name']
|
||||
|
@ -381,6 +390,8 @@ class SnmpUser(object):
|
|||
msg='Error: The length of priv_key %s is out of [1 - 255].' % priv_key)
|
||||
conf_str += "<privKey></privKey>"
|
||||
|
||||
result["usm_user_info"] = []
|
||||
|
||||
conf_str += CE_GET_SNMP_V3_USM_USER_TAIL
|
||||
recv_xml = self.netconf_get_config(module=module, conf_str=conf_str)
|
||||
|
||||
|
@ -394,10 +405,11 @@ class SnmpUser(object):
|
|||
replace('xmlns="http://www.huawei.com/netconf/vrp"', "")
|
||||
|
||||
root = ElementTree.fromstring(xml_str)
|
||||
usm_user_info = root.findall("data/snmp/usmUsers/usmUser")
|
||||
usm_user_info = root.findall("snmp/usmUsers/usmUser")
|
||||
if usm_user_info:
|
||||
for tmp in usm_user_info:
|
||||
tmp_dict = dict()
|
||||
tmp_dict["remoteEngineID"] = None
|
||||
for site in tmp:
|
||||
if site.tag in ["userName", "remoteEngineID", "engineID", "groupName", "authProtocol",
|
||||
"authKey", "privProtocol", "privKey", "aclNumber"]:
|
||||
|
@ -405,83 +417,51 @@ class SnmpUser(object):
|
|||
|
||||
result["usm_user_info"].append(tmp_dict)
|
||||
|
||||
if result["usm_user_info"]:
|
||||
for tmp in result["usm_user_info"]:
|
||||
if "userName" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["userName"] != usm_user_name:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["userName"] == usm_user_name:
|
||||
need_cfg = True
|
||||
if "remoteEngineID" in tmp.keys():
|
||||
if remote_engine_id:
|
||||
enable = "true"
|
||||
else:
|
||||
enable = "false"
|
||||
cur_cfg = dict()
|
||||
if usm_user_name:
|
||||
cur_cfg["userName"] = usm_user_name
|
||||
if user_group:
|
||||
cur_cfg["groupName"] = user_group
|
||||
if auth_protocol:
|
||||
cur_cfg["authProtocol"] = auth_protocol
|
||||
if auth_key:
|
||||
cur_cfg["authKey"] = auth_key
|
||||
if priv_protocol:
|
||||
cur_cfg["privProtocol"] = priv_protocol
|
||||
if priv_key:
|
||||
cur_cfg["privKey"] = priv_key
|
||||
if acl_number:
|
||||
cur_cfg["aclNumber"] = acl_number
|
||||
|
||||
if state == "present":
|
||||
if tmp["remoteEngineID"] != enable:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["remoteEngineID"] == enable:
|
||||
need_cfg = True
|
||||
if remote_engine_id:
|
||||
if "engineID" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["engineID"] != remote_engine_id:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["engineID"] == remote_engine_id:
|
||||
need_cfg = True
|
||||
if user_group:
|
||||
if "groupName" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["groupName"] != user_group:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["groupName"] == user_group:
|
||||
need_cfg = True
|
||||
if auth_protocol:
|
||||
if "authProtocol" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["authProtocol"] != auth_protocol:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["authProtocol"] == auth_protocol:
|
||||
need_cfg = True
|
||||
if auth_key:
|
||||
if "authKey" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["authKey"] != auth_key:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["authKey"] == auth_key:
|
||||
need_cfg = True
|
||||
if priv_protocol:
|
||||
if "privProtocol" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["privProtocol"] != priv_protocol:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["privProtocol"] == priv_protocol:
|
||||
need_cfg = True
|
||||
if priv_key:
|
||||
if "privKey" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["privKey"] != priv_key:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["privKey"] == priv_key:
|
||||
need_cfg = True
|
||||
if acl_number:
|
||||
if "aclNumber" in tmp.keys():
|
||||
if state == "present":
|
||||
if tmp["aclNumber"] != acl_number:
|
||||
need_cfg = True
|
||||
else:
|
||||
if tmp["aclNumber"] == acl_number:
|
||||
need_cfg = True
|
||||
if remote_engine_id:
|
||||
cur_cfg["engineID"] = remote_engine_id
|
||||
cur_cfg["remoteEngineID"] = "true"
|
||||
else:
|
||||
cur_cfg["engineID"] = self.local_engine_id
|
||||
cur_cfg["remoteEngineID"] = "false"
|
||||
|
||||
if result["usm_user_info"]:
|
||||
num = 0
|
||||
for tmp in result["usm_user_info"]:
|
||||
if cur_cfg == tmp:
|
||||
num += 1
|
||||
|
||||
if num == 0:
|
||||
if state == "present":
|
||||
need_cfg = True
|
||||
else:
|
||||
need_cfg = False
|
||||
else:
|
||||
if state == "present":
|
||||
need_cfg = False
|
||||
else:
|
||||
need_cfg = True
|
||||
|
||||
else:
|
||||
if state == "present":
|
||||
need_cfg = True
|
||||
else:
|
||||
need_cfg = False
|
||||
|
||||
result["need_cfg"] = need_cfg
|
||||
return result
|
||||
|
@ -491,7 +471,7 @@ class SnmpUser(object):
|
|||
|
||||
module = kwargs["module"]
|
||||
result = dict()
|
||||
result["local_user_info"] = []
|
||||
|
||||
need_cfg = False
|
||||
state = module.params['state']
|
||||
local_user_name = module.params['aaa_local_user']
|
||||
|
@ -524,6 +504,8 @@ class SnmpUser(object):
|
|||
module.fail_json(
|
||||
msg='Error: The length of priv_key %s is out of [1 - 255].' % priv_key)
|
||||
|
||||
result["local_user_info"] = []
|
||||
|
||||
conf_str = CE_GET_SNMP_V3_LOCAL_USER
|
||||
recv_xml = self.netconf_get_config(module=module, conf_str=conf_str)
|
||||
|
||||
|
@ -538,7 +520,7 @@ class SnmpUser(object):
|
|||
|
||||
root = ElementTree.fromstring(xml_str)
|
||||
local_user_info = root.findall(
|
||||
"data/snmp/localUsers/localUser")
|
||||
"snmp/localUsers/localUser")
|
||||
if local_user_info:
|
||||
for tmp in local_user_info:
|
||||
tmp_dict = dict()
|
||||
|
@ -649,8 +631,8 @@ class SnmpUser(object):
|
|||
|
||||
if auth_protocol != "noAuth":
|
||||
cmd += " cipher %s" % "******"
|
||||
|
||||
cmds.append(cmd)
|
||||
if auth_protocol or auth_key:
|
||||
cmds.append(cmd)
|
||||
|
||||
if remote_engine_id:
|
||||
cmd = "snmp-agent remote-engineid %s usm-user v3 %s" % (
|
||||
|
@ -669,8 +651,8 @@ class SnmpUser(object):
|
|||
|
||||
if auth_protocol != "noAuth" and priv_protocol != "noPriv":
|
||||
cmd += " cipher %s" % "******"
|
||||
|
||||
cmds.append(cmd)
|
||||
if priv_key or priv_protocol:
|
||||
cmds.append(cmd)
|
||||
|
||||
conf_str += CE_MERGE_SNMP_V3_USM_USER_TAIL
|
||||
recv_xml = self.netconf_set_config(module=module, conf_str=conf_str)
|
||||
|
@ -716,7 +698,6 @@ class SnmpUser(object):
|
|||
if acl_number:
|
||||
conf_str += "<aclNumber>%s</aclNumber>" % acl_number
|
||||
cmd += " acl %s" % acl_number
|
||||
|
||||
cmds.append(cmd)
|
||||
|
||||
if remote_engine_id:
|
||||
|
@ -737,7 +718,8 @@ class SnmpUser(object):
|
|||
if auth_protocol != "noAuth":
|
||||
cmd += " cipher %s" % "******"
|
||||
|
||||
cmds.append(cmd)
|
||||
if auth_key or auth_protocol:
|
||||
cmds.append(cmd)
|
||||
|
||||
if remote_engine_id:
|
||||
cmd = "snmp-agent remote-engineid %s usm-user v3 %s" % (
|
||||
|
@ -757,9 +739,11 @@ class SnmpUser(object):
|
|||
if auth_protocol != "noAuth" and priv_protocol != "noPriv":
|
||||
cmd += " cipher %s" % "******"
|
||||
|
||||
cmds.append(cmd)
|
||||
if priv_protocol or priv_key:
|
||||
cmds.append(cmd)
|
||||
|
||||
conf_str += CE_CREATE_SNMP_V3_USM_USER_TAIL
|
||||
|
||||
recv_xml = self.netconf_set_config(module=module, conf_str=conf_str)
|
||||
|
||||
if "<ok/>" not in recv_xml:
|
||||
|
@ -890,14 +874,17 @@ class SnmpUser(object):
|
|||
|
||||
module = kwargs["module"]
|
||||
|
||||
regular = "| include snmp | include local-engineid"
|
||||
flags = list()
|
||||
flags.append(regular)
|
||||
tmp_cfg = get_config(module, flags)
|
||||
conf_str = GET_SNMP_LOCAL_ENGINE
|
||||
recv_xml = self.netconf_get_config(module=module, conf_str=conf_str)
|
||||
if "</data>" in recv_xml:
|
||||
xml_str = recv_xml.replace('\r', '').replace('\n', '').\
|
||||
replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\
|
||||
replace('xmlns="http://www.huawei.com/netconf/vrp"', "")
|
||||
|
||||
if tmp_cfg:
|
||||
tmp_data = tmp_cfg.split(r"snmp-agent local-engineid ")
|
||||
self.local_engine_id = tmp_data[1]
|
||||
root = ElementTree.fromstring(xml_str)
|
||||
local_engine_info = root.findall("snmp/engine/engineID")
|
||||
if local_engine_info:
|
||||
self.local_engine_id = local_engine_info[0].text
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -968,13 +955,12 @@ def main():
|
|||
if aaa_local_user:
|
||||
proposed["aaa_local_user"] = aaa_local_user
|
||||
|
||||
snmp_user_obj.get_snmp_local_engine(module=module)
|
||||
snmp_v3_usm_user_rst = snmp_user_obj.check_snmp_v3_usm_user_args(
|
||||
module=module)
|
||||
snmp_v3_local_user_rst = snmp_user_obj.check_snmp_v3_local_user_args(
|
||||
module=module)
|
||||
|
||||
snmp_user_obj.get_snmp_local_engine(module=module)
|
||||
|
||||
# state exist snmp v3 user config
|
||||
exist_tmp = dict()
|
||||
for item in snmp_v3_usm_user_rst:
|
||||
|
|
Loading…
Reference in a new issue