17
17
from ansible .module_utils .ios .argspec .l3_interfaces .l3_interfaces import L3_InterfacesArgs
18
18
from ansible .module_utils .ios .config .base import ConfigBase
19
19
from ansible .module_utils .ios .facts .facts import Facts
20
-
20
+ import q
21
21
22
22
class L3_Interfaces (ConfigBase , L3_InterfacesArgs ):
23
23
"""
@@ -132,6 +132,8 @@ def _state_replaced(**kwargs):
132
132
commands .extend (L3_Interfaces .clear_interface (** kwargs ))
133
133
kwargs = {'want' : interface , 'have' : each , 'commands' : commands , 'module' : module }
134
134
commands .extend (L3_Interfaces .set_interface (** kwargs ))
135
+ # Remove the duplicate interface call
136
+ commands = L3_Interfaces ._remove_duplicate_interface (commands )
135
137
136
138
return commands
137
139
@@ -162,6 +164,8 @@ def _state_overridden(**kwargs):
162
164
commands .extend (L3_Interfaces .clear_interface (** kwargs ))
163
165
kwargs = {'want' : interface , 'have' : each , 'commands' : commands , 'module' : module }
164
166
commands .extend (L3_Interfaces .set_interface (** kwargs ))
167
+ # Remove the duplicate interface call
168
+ commands = L3_Interfaces ._remove_duplicate_interface (commands )
165
169
166
170
return commands
167
171
@@ -246,16 +250,38 @@ def validate_ipv6(value, module):
246
250
if not 0 <= int (address [1 ]) <= 128 :
247
251
module .fail_json (msg = 'invalid value for mask: {}, mask should be in range 0-128' .format (address [1 ]))
248
252
253
+ @staticmethod
254
+ def validate_n_expand_ipv4 (module , want ):
255
+ # Check if input IPV4 is valid IP and expand IPV4 with its subnet mask
256
+ ip_addr_want = want .get ('address' )
257
+ L3_Interfaces .validate_ipv4 (ip_addr_want , module )
258
+ ip = ip_addr_want .split ('/' )
259
+ if len (ip ) == 2 :
260
+ ip_addr_want = '{0} {1}' .format (ip [0 ], to_netmask (ip [1 ]))
261
+
262
+ return ip_addr_want
263
+
264
+ @staticmethod
265
+ def _remove_duplicate_interface (commands ):
266
+ # Remove duplicate interface from commands
267
+ set_cmd = []
268
+ for each in commands :
269
+ if 'interface' in each :
270
+ interface = each
271
+ if interface not in set_cmd :
272
+ set_cmd .append (each )
273
+ else :
274
+ set_cmd .append (each )
275
+
276
+ return set_cmd
277
+
249
278
@staticmethod
250
279
def set_interface (** kwargs ):
251
280
# Set the interface config based on the want and have config
252
281
commands = []
253
282
want = kwargs ['want' ]
254
283
have = kwargs ['have' ]
255
284
module = kwargs ['module' ]
256
- clear_cmds = []
257
- if kwargs .get ('commands' ):
258
- clear_cmds = kwargs ['commands' ]
259
285
interface = 'interface ' + want ['name' ]
260
286
261
287
# To handle Sub-Interface if encapsulation is not already configured
@@ -264,67 +290,59 @@ def set_interface(**kwargs):
264
290
module .fail_json (msg = 'IP routing on a LAN Sub-Interface is only allowed if Encapsulation'
265
291
' is configured over respective Sub-Interface' .format (want ['name' ]))
266
292
# To handle L3 IPV4 configuration
267
- ipv4 = want .get ('ipv4' )
268
- if ipv4 :
269
- for each_ip in ipv4 :
270
- if each_ip .get ('address' ) == 'dhcp' :
271
- if each_ip .get ('dhcp_client' ) and (each_ip .get ('dhcp_client' ) != have .get ('dhcp_client' )
272
- or 'no ip address' in clear_cmds ):
273
- if each_ip .get ('dhcp_hostname' ) and each_ip .get ('dhcp_hostname' ) == have .get ('dhcp_hostname' ):
274
- cmd = 'ip address dhcp client-id GigabitEthernet0/{} hostname {}' .\
275
- format (each_ip .get ('dhcp_client' ), each_ip .get ('dhcp_hostname' ))
276
- L3_Interfaces ._add_command_to_interface (interface , cmd , commands )
277
- else :
278
- cmd = 'ip address dhcp client-id GigabitEthernet0/{}' .format (each_ip .get ('dhcp_client' ))
279
- L3_Interfaces ._add_command_to_interface (interface , cmd , commands )
280
- if each_ip .get ('dhcp_hostname' ) and (each_ip .get ('dhcp_hostname' ) != have .get ('dhcp_hostname' )
281
- or 'no ip address' in clear_cmds ):
282
- cmd = 'ip address dhcp hostname {}' .format (each_ip .get ('dhcp_hostname' ))
283
- L3_Interfaces ._add_command_to_interface (interface , cmd , commands )
284
- else :
285
- ip_addr = each_ip .get ('address' )
286
- L3_Interfaces .validate_ipv4 (ip_addr , module )
287
- ip = ip_addr .split ('/' )
288
- if len (ip ) == 2 :
289
- ip_addr = '{0} {1}' .format (ip [0 ], to_netmask (ip [1 ]))
290
- if each_ip .get ('secondary' ):
291
- if ip_addr != have .get ('secondary_ipv4' ) or 'no ip address' in clear_cmds :
292
- cmd = 'ip address {} secondary' .format (ip_addr )
293
- L3_Interfaces ._add_command_to_interface (interface , cmd , commands )
294
- elif have .get ('ipv4' ) != ip_addr or 'no ip address' in clear_cmds :
295
- cmd = 'ip address {}' .format (ip_addr )
296
- L3_Interfaces ._add_command_to_interface (interface , cmd , commands )
293
+ if want .get ("ipv4" ):
294
+ for each in want .get ("ipv4" ):
295
+ if each .get ('address' ) != 'dhcp' :
296
+ ip_addr_want = L3_Interfaces .validate_n_expand_ipv4 (module , each )
297
+ each ['address' ] = ip_addr_want
298
+
299
+ want_ipv4 = set (tuple ({k :v for k ,v in iteritems (address ) if v is not None }.items ()) for address in want .get ("ipv4" ) or [])
300
+ have_ipv4 = set (tuple (address .items ()) for address in have .get ("ipv4" ) or [])
301
+ diff = want_ipv4 - have_ipv4
302
+ for address in diff :
303
+ address = dict (address )
304
+ if address .get ('address' ) != 'dhcp' :
305
+ cmd = "ip address {}" .format (address ["address" ])
306
+ if address .get ("secondary" ):
307
+ cmd += " secondary"
308
+ elif address .get ('address' ) == 'dhcp' :
309
+ if address .get ('dhcp_client' ) and address .get ('dhcp_hostname' ):
310
+ cmd = "ip address dhcp client-id GigabitEthernet 0/{} hostname {}" .format \
311
+ (address .get ('dhcp_client' ),address .get ('dhcp_hostname' ))
312
+ elif address .get ('dhcp_client' ) and not address .get ('dhcp_hostname' ):
313
+ cmd = "ip address dhcp client-id GigabitEthernet 0/{}" .format (address .get ('dhcp_client' ))
314
+ elif not address .get ('dhcp_client' ) and address .get ('dhcp_hostname' ):
315
+ cmd = "ip address dhcp hostname {}" .format (address .get ('dhcp_client' ))
316
+
317
+ L3_Interfaces ._add_command_to_interface (interface , cmd , commands )
297
318
298
319
# To handle L3 IPV6 configuration
299
- ipv6 = want .get ('ipv6' )
300
- if ipv6 :
301
- for each_ip in ipv6 :
302
- if each_ip .get ('dhcp' ) and (have ('dhcp' ) or 'no ipv6 address' in clear_cmds ):
303
- cmd = 'ipv6 address dhcp'
304
- L3_Interfaces ._add_command_to_interface (interface , cmd , commands )
305
- elif each_ip .get ('autoconfig' ) and (have ('autoconfig' ) or 'no ipv6 address' in clear_cmds ):
306
- cmd = 'ipv6 address autoconfig'
307
- L3_Interfaces ._add_command_to_interface (interface , cmd , commands )
308
- else :
309
- ipv6_addr = each_ip .get ('address' )
310
- L3_Interfaces .validate_ipv6 (ipv6_addr , module )
311
- if have .get ('ipv6' ) != ipv6_addr .upper () or 'no ipv6 address' in clear_cmds :
312
- cmd = 'ipv6 address {}' .format (ipv6_addr )
313
- L3_Interfaces ._add_command_to_interface (interface , cmd , commands )
320
+ want_ipv6 = set (tuple ({k :v for k ,v in iteritems (address ) if v is not None }.items ()) for address in want .get ("ipv6" ) or [])
321
+ have_ipv6 = set (tuple (address .items ()) for address in have .get ("ipv6" ) or [])
322
+ diff = want_ipv6 - have_ipv6
323
+ for address in diff :
324
+ address = dict (address )
325
+ L3_Interfaces .validate_ipv6 (address .get ('address' ), module )
326
+ cmd = "ipv6 address {}" .format (address .get ('address' ))
327
+ L3_Interfaces ._add_command_to_interface (interface , cmd , commands )
314
328
315
329
return commands
316
330
317
331
@staticmethod
318
332
def clear_interface (** kwargs ):
319
333
# Delete the interface config based on the want and have config
334
+ count = 0
320
335
commands = []
321
336
want = kwargs ['want' ]
322
337
have = kwargs ['have' ]
323
338
interface = 'interface ' + want ['name' ]
324
339
325
- if have .get ('secondary' ) and not want .get ('secondary' ):
326
- cmd = 'ip address {} secondary' .format (have .get ('secondary_ipv4' ))
327
- L3_Interfaces ._remove_command_from_interface (interface , cmd , commands )
340
+ if have .get ('ipv4' ) and want .get ('ipv4' ):
341
+ for each in have .get ('ipv4' ):
342
+ if each .get ('secondary' ) and not (want .get ('ipv4' )[count ].get ('secondary' )):
343
+ cmd = 'ipv4 address {} secondary' .format (each .get ('address' ))
344
+ L3_Interfaces ._remove_command_from_interface (interface , cmd , commands )
345
+ count += 1
328
346
if have .get ('ipv4' ) and not want .get ('ipv4' ):
329
347
L3_Interfaces ._remove_command_from_interface (interface , 'ip address' , commands )
330
348
if have .get ('ipv6' ) and not want .get ('ipv6' ):
0 commit comments