diff --git a/lib/ansible/module_utils/network/common/utils.py b/lib/ansible/module_utils/network/common/utils.py index 1ac6a996af7..1e281bbb151 100644 --- a/lib/ansible/module_utils/network/common/utils.py +++ b/lib/ansible/module_utils/network/common/utils.py @@ -359,6 +359,14 @@ def validate_ip_address(address): return address.count('.') == 3 +def validate_ip_v6_address(address): + try: + socket.inet_pton(socket.AF_INET6, address) + except socket.error: + return False + return True + + def validate_prefix(prefix): if prefix and not 0 <= int(prefix) <= 32: return False diff --git a/lib/ansible/modules/net_tools/nios/nios_network.py b/lib/ansible/modules/net_tools/nios/nios_network.py index 5d73677ec88..040158a2aff 100644 --- a/lib/ansible/modules/net_tools/nios/nios_network.py +++ b/lib/ansible/modules/net_tools/nios/nios_network.py @@ -20,6 +20,7 @@ description: - Adds and/or removes instances of network objects from Infoblox NIOS servers. This module manages NIOS C(network) objects using the Infoblox WAPI interface over REST. + - Supports both IPV4 and IPV6 internet protocols requirements: - infoblox_client extends_documentation_fragment: nios @@ -87,7 +88,7 @@ options: ''' EXAMPLES = ''' -- name: configure a network +- name: configure a network ipv4 nios_network: network: 192.168.10.0/24 comment: this is a test comment @@ -98,7 +99,18 @@ EXAMPLES = ''' password: admin connection: local -- name: set dhcp options for a network +- name: configure a network ipv6 + nios_network: + network: fe80::/64 + comment: this is a test comment + state: present + provider: + host: "{{ inventory_hostname_short }}" + username: admin + password: admin + connection: local + +- name: set dhcp options for a network ipv4 nios_network: network: 192.168.10.0/24 comment: this is a test comment @@ -112,7 +124,7 @@ EXAMPLES = ''' password: admin connection: local -- name: remove a network +- name: remove a network ipv4 nios_network: network: 192.168.10.0/24 state: absent @@ -128,6 +140,7 @@ RETURN = ''' # ''' from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.six import iteritems from ansible.module_utils.net_tools.nios.api import WapiModule +from ansible.module_utils.network.common.utils import validate_ip_address, validate_ip_v6_address def options(module): @@ -159,6 +172,17 @@ def options(module): return options +def check_ip_addr_type(ip): + '''This function will check if the argument ip is type v4/v6 and return appropriate infoblox network type + ''' + check_ip = ip.split('/') + + if validate_ip_address(check_ip[0]): + return 'network' + elif validate_ip_v6_address(check_ip[0]): + return 'ipv6network' + + def main(): ''' Main entry point for module execution ''' @@ -194,8 +218,12 @@ def main(): module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) + # to get the argument ipaddr + obj_filter = dict([(k, module.params[k]) for k, v in iteritems(ib_spec) if v.get('ib_req')]) + network_type = check_ip_addr_type(obj_filter['network']) + wapi = WapiModule(module) - result = wapi.run('network', ib_spec) + result = wapi.run(network_type, ib_spec) module.exit_json(**result)