diff --git a/lib/ansible/module_utils/ce.py b/lib/ansible/module_utils/ce.py index e93439ede45..e7c40dc1421 100644 --- a/lib/ansible/module_utils/ce.py +++ b/lib/ansible/module_utils/ce.py @@ -29,6 +29,8 @@ # import re +import socket +import sys from ansible.module_utils.basic import env_fallback from ansible.module_utils.network_common import to_list, ComplexList @@ -443,3 +445,21 @@ def execute_nc_cli(module, xml_str): conn = get_nc_connection(module) return conn.execute_cli(xml_str) + + +def check_ip_addr(ipaddr): + """ check ip address, Supports IPv4 and IPv6 """ + + if not ipaddr or '\x00' in ipaddr: + return False + + try: + res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC, + socket.SOCK_STREAM, + 0, socket.AI_NUMERICHOST) + return bool(res) + except socket.gaierror: + err = sys.exc_info()[1] + if err.args[0] == socket.EAI_NONAME: + return False + raise diff --git a/lib/ansible/modules/network/cloudengine/ce_aaa_server_host.py b/lib/ansible/modules/network/cloudengine/ce_aaa_server_host.py index a798f863a83..16f5b3ed49f 100644 --- a/lib/ansible/modules/network/cloudengine/ce_aaa_server_host.py +++ b/lib/ansible/modules/network/cloudengine/ce_aaa_server_host.py @@ -282,12 +282,9 @@ updates: "hwtacacs server authorization 10.135.182.157 vpn-instance test_vpn public-net"] ''' -import sys -import socket from xml.etree import ElementTree from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec - +from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr SUCCESS = """success""" FAILED = """failed""" @@ -2193,25 +2190,6 @@ class AaaServerHost(object): return cmds -def check_ip_addr(ipaddr): - """ check_ip_addr, Supports IPv4 and IPv6 """ - - if not ipaddr or '\x00' in ipaddr: - return False - - try: - res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC, - socket.SOCK_STREAM, - 0, socket.AI_NUMERICHOST) - return bool(res) - except socket.gaierror: - err = sys.exc_info()[1] - if err.args[0] == socket.EAI_NONAME: - return False - raise - return True - - def check_name(**kwargs): """ Check invalid name """ diff --git a/lib/ansible/modules/network/cloudengine/ce_acl.py b/lib/ansible/modules/network/cloudengine/ce_acl.py index 1f9846cf0aa..82df6c2676e 100644 --- a/lib/ansible/modules/network/cloudengine/ce_acl.py +++ b/lib/ansible/modules/network/cloudengine/ce_acl.py @@ -210,12 +210,9 @@ updates: sample: ["undo acl name test"] ''' -import socket -import sys from xml.etree import ElementTree from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec - +from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr # get acl CE_GET_ACL_HEADER = """ @@ -319,25 +316,6 @@ CE_DELETE_ACL_BASE_RULE_TAIL = """ """ -def check_ip_addr(ipaddr): - """ check_ip_addr, Supports IPv4 and IPv6 """ - - if not ipaddr or '\x00' in ipaddr: - return False - - try: - res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC, - socket.SOCK_STREAM, - 0, socket.AI_NUMERICHOST) - return bool(res) - except socket.gaierror: - err = sys.exc_info()[1] - if err.args[0] == socket.EAI_NONAME: - return False - raise - return True - - class BaseAcl(object): """ Manages base acl configuration """ diff --git a/lib/ansible/modules/network/cloudengine/ce_acl_advance.py b/lib/ansible/modules/network/cloudengine/ce_acl_advance.py index e8cbbc9186f..af18be8cc4b 100644 --- a/lib/ansible/modules/network/cloudengine/ce_acl_advance.py +++ b/lib/ansible/modules/network/cloudengine/ce_acl_advance.py @@ -356,11 +356,9 @@ updates: sample: ["undo acl name test"] ''' -import socket -import sys from xml.etree import ElementTree from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec +from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr # get acl @@ -486,25 +484,6 @@ IGMP_TYPE_NUM = {"host-query": "17", "v3host-report": "34"} -def check_ip_addr(ipaddr): - """ check ip address, Supports IPv4 and IPv6 """ - - if not ipaddr or '\x00' in ipaddr: - return False - - try: - res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC, - socket.SOCK_STREAM, - 0, socket.AI_NUMERICHOST) - return bool(res) - except socket.gaierror: - err = sys.exc_info()[1] - if err.args[0] == socket.EAI_NONAME: - return False - raise - return True - - def get_wildcard_mask(mask): """ convert mask length to ip address wildcard mask, i.e. 24 to 0.0.0.255 """ diff --git a/lib/ansible/modules/network/cloudengine/ce_bgp_af.py b/lib/ansible/modules/network/cloudengine/ce_bgp_af.py index d303e708e23..c92e8f86c83 100644 --- a/lib/ansible/modules/network/cloudengine/ce_bgp_af.py +++ b/lib/ansible/modules/network/cloudengine/ce_bgp_af.py @@ -497,11 +497,8 @@ updates: ''' import re -import sys -import socket from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec - +from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr # get bgp address family CE_GET_BGP_ADDRESS_FAMILY_HEADER = """ @@ -882,27 +879,6 @@ CE_BGP_DELETE_NETWORK_UNIT = """ """ -def check_ip_addr(**kwargs): - """ check_ip_addr, Supports IPv4 and IPv6""" - - ipaddr = kwargs["ipaddr"] - - if not ipaddr or '\x00' in ipaddr: - return False - - try: - res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC, - socket.SOCK_STREAM, - 0, socket.AI_NUMERICHOST) - return bool(res) - except socket.gaierror: - err = sys.exc_info()[1] - if err.args[0] == socket.EAI_NONAME: - return False - raise - return True - - class BgpAf(object): """ Manages BGP Address-family configuration """ diff --git a/lib/ansible/modules/network/cloudengine/ce_bgp_neighbor.py b/lib/ansible/modules/network/cloudengine/ce_bgp_neighbor.py index ccf0505e59c..8aafd492ee3 100644 --- a/lib/ansible/modules/network/cloudengine/ce_bgp_neighbor.py +++ b/lib/ansible/modules/network/cloudengine/ce_bgp_neighbor.py @@ -293,10 +293,8 @@ updates: ''' import re -import sys -import socket from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec +from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr # get bgp peer @@ -461,27 +459,6 @@ CE_DELETE_PEER_BFD_TAIL = """ """ -def check_ip_addr(**kwargs): - """ check_ip_addr, Supports IPv4 and IPv6 """ - - ipaddr = kwargs["ipaddr"] - - if not ipaddr or '\x00' in ipaddr: - return False - - try: - res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC, - socket.SOCK_STREAM, - 0, socket.AI_NUMERICHOST) - return bool(res) - except socket.gaierror: - err = sys.exc_info()[1] - if err.args[0] == socket.EAI_NONAME: - return False - raise - return True - - class BgpNeighbor(object): """ Manages BGP peer configuration """ diff --git a/lib/ansible/modules/network/cloudengine/ce_bgp_neighbor_af.py b/lib/ansible/modules/network/cloudengine/ce_bgp_neighbor_af.py index 6c2568cd392..2948a4c4bb9 100644 --- a/lib/ansible/modules/network/cloudengine/ce_bgp_neighbor_af.py +++ b/lib/ansible/modules/network/cloudengine/ce_bgp_neighbor_af.py @@ -460,11 +460,8 @@ updates: ''' import re -import sys -import socket from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec - +from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr # get bgp peer af CE_GET_BGP_PEER_AF_HEADER = """ @@ -571,27 +568,6 @@ CE_DELETE_BGP_PEER_AF = """ """ -def check_ip_addr(**kwargs): - """ check_ip_addr, Supports IPv4 and IPv6 """ - - ipaddr = kwargs["ipaddr"] - - if not ipaddr or '\x00' in ipaddr: - return False - - try: - res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC, - socket.SOCK_STREAM, - 0, socket.AI_NUMERICHOST) - return bool(res) - except socket.gaierror: - err = sys.exc_info()[1] - if err.args[0] == socket.EAI_NONAME: - return False - raise - return True - - class BgpNeighborAf(object): """ Manages BGP neighbor Address-family configuration """ diff --git a/lib/ansible/modules/network/cloudengine/ce_info_center_global.py b/lib/ansible/modules/network/cloudengine/ce_info_center_global.py index af1162750b3..0fcc63cc854 100644 --- a/lib/ansible/modules/network/cloudengine/ce_info_center_global.py +++ b/lib/ansible/modules/network/cloudengine/ce_info_center_global.py @@ -326,11 +326,10 @@ changed: type: boolean sample: true ''' -import socket -import sys + from xml.etree import ElementTree from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.ce import ce_argument_spec, get_nc_config, set_nc_config +from ansible.module_utils.ce import ce_argument_spec, get_nc_config, set_nc_config, check_ip_addr CE_NC_GET_CENTER_GLOBAL_INFO_HEADER = """ @@ -571,25 +570,6 @@ CE_NC_DELETE_SERVER_DNS_INFO_TAIL = """ """ -def is_valid_address(address): - """ check ip address, Supports IPv4 and IPv6""" - - if not address or '\x00' in address: - return False - - try: - res = socket.getaddrinfo(address, 0, socket.AF_UNSPEC, - socket.SOCK_STREAM, - 0, socket.AI_NUMERICHOST) - return bool(res) - except socket.gaierror: - err = sys.exc_info()[1] - if err.args[0] == socket.EAI_NONAME: - return False - raise - return True - - def get_out_direct_default(out_direct): """get default out direct""" @@ -1433,12 +1413,12 @@ class InfoCenterGlobal(object): # server_ip check if self.server_ip: - if not is_valid_address(self.server_ip): + if not check_ip_addr(self.server_ip): self.module.fail_json( msg='Error: The %s is not a valid ip address' % self.server_ip) # source_ip check if self.source_ip: - if not is_valid_address(self.source_ip): + if not check_ip_addr(self.source_ip): self.module.fail_json( msg='Error: The %s is not a valid ip address' % self.source_ip) diff --git a/lib/ansible/modules/network/cloudengine/ce_sflow.py b/lib/ansible/modules/network/cloudengine/ce_sflow.py index d018c4a0dae..329e9957bd7 100644 --- a/lib/ansible/modules/network/cloudengine/ce_sflow.py +++ b/lib/ansible/modules/network/cloudengine/ce_sflow.py @@ -243,12 +243,10 @@ changed: sample: true ''' -import sys import re -import socket from xml.etree import ElementTree from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec +from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr from ansible.module_utils.ce import get_config, load_config CE_NC_GET_SFLOW = """ @@ -331,26 +329,6 @@ def is_valid_ip_vpn(vpname): return True -def check_ip_addr(ipaddr): - """check ip address, Supports IPv4 and IPv6""" - - if not ipaddr or '\x00' in ipaddr: - return False - - try: - res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC, - socket.SOCK_STREAM, - 0, socket.AI_NUMERICHOST) - return bool(res) - except socket.gaierror: - err = sys.exc_info()[1] - if err.args[0] == socket.EAI_NONAME: - return False - raise - - return True - - def get_ip_version(address): """get ip version fast""" diff --git a/lib/ansible/modules/network/cloudengine/ce_snmp_target_host.py b/lib/ansible/modules/network/cloudengine/ce_snmp_target_host.py index 9b10cc1e919..1823723102e 100644 --- a/lib/ansible/modules/network/cloudengine/ce_snmp_target_host.py +++ b/lib/ansible/modules/network/cloudengine/ce_snmp_target_host.py @@ -170,13 +170,10 @@ updates: sample: ["snmp-agent target-host host-name test2 trap address udp-domain 10.135.182.158 vpn-instance js params securityname wdz v3 authentication"] ''' -import sys -import socket from xml.etree import ElementTree from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.ce import get_nc_config, set_nc_config, \ - ce_argument_spec, get_config, load_config - + ce_argument_spec, get_config, load_config, check_ip_addr # get snmp version CE_GET_SNMP_VERSION = """ @@ -263,25 +260,6 @@ INTERFACE_TYPE = ['ethernet', 'eth-trunk', 'tunnel', 'null', 'loopback', 'vlanif', '100ge', '40ge', 'mtunnel', '10ge', 'ge', 'meth', 'vbdif', 'nve'] -def check_ip_addr(ipaddr): - """ check_ip_addr, Supports IPv4 and IPv6 """ - - if not ipaddr or '\x00' in ipaddr: - return False - - try: - res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC, - socket.SOCK_STREAM, - 0, socket.AI_NUMERICHOST) - return bool(res) - except socket.gaierror: - err = sys.exc_info()[1] - if err.args[0] == socket.EAI_NONAME: - return False - raise - return True - - class SnmpTargetHost(object): """ Manages SNMP target host configuration """