Repository: ansible/ansible

Issue:
The ios_l2_interface vlan_range_to_list function may not cast a null variable in integer
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->
In our infrastructure we can have devices configured with a list of vlans whose string ends with a comma.

When splitting the string, the last element is then equal to None which can not be cast to integer
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
ios_l2_interface
##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
(zion_ansible)[ta-admin-ng5898b@rac group_vars]$ ansible --version
ansible 2.6.2
 config file = /etc/ansible/ansible.cfg
 configured module search path = [u'/usr/share/ansible']
 ansible python module location = /dns/development/ctrebuchet_sandbox/zion_ansible/lib/python2.7/site-packages/ansible
 executable location = /dns/development/ctrebuchet_sandbox/zion_ansible/bin/ansible
 python version = 2.7.8 (default, Oct  9 2014, 10:48:46) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
Red Hat Enterprise Linux Server release 6.9 (Santiago)
##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->
having in the configuration a string of vlan like : `"228-230,370,378,379,381,385,500,501,580-584,600,646,"`
<!--- Paste example playbooks or commands between quotes below -->
```python
>>> def vlan_range_to_list(vlans):
...     result = []
...     if vlans:
...         for part in vlans.split(','):
...             if part.lower() == 'none':
...                 break
...             if '-' in part:
...                 start, stop = (int(i) for i in part.split('-'))
...                 result.extend(range(start, stop + 1))
...             else:
...                 result.append(int(part))
...     return sorted(result)
... 
>>> vlans = "228-230,370,378,379,381,385,500,501,580-584,600,646,"
>>> vlan_range_to_list(vlans)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 11, in vlan_range_to_list
ValueError: invalid literal for int() with base 10: ''
>>> vlans = "228-230,370,378,379,381,385,500,501,580-584,600,646"
>>> vlan_range_to_list(vlans)
[228, 229, 230, 370, 378, 379, 381, 385, 500, 501, 580, 581, 582, 583, 584, 600, 646]
>>> 
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
The vlan string is parsed correctly and the function handle all the cases.
```python
>>> def vlan_range_to_list(vlans):
...     result = []
...     if vlans:
...         for part in vlans.split(','):
...             if part.lower() == 'none':
...                 break
...             if part:
...                 if '-' in part:
...                     start, stop = (int(i) for i in part.split('-'))
...                     result.extend(range(start, stop + 1))
...                 else:
...                     result.append(int(part))
...     return sorted(result)
... 
>>> vlans = "228-230,370,378,379,381,385,500,501,580-584,600,646,"
>>> vlan_range_to_list(vlans)
[228, 229, 230, 370, 378, 379, 381, 385, 500, 501, 580, 581, 582, 583, 584, 600, 646]
```
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 11, in vlan_range_to_list
ValueError: invalid literal for int() with base 10: ''
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/network/ios/ios_l2_interface.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/ios/ios_l2_interface.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @qalthos @rcarrillocruz
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Hi @clementtrebuchet,

Thank you for the issue, just so you are aware we have a dedicated Working Group for network.
You can find other people interested in this in #ansible-network on Freenode IRC
For more information about communities, meetings and agendas see https://github.com/ansible/community

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: community_workgroups --->

Resolving patch:
diff --git a/lib/ansible/modules/network/ios/ios_l2_interface.py b/lib/ansible/modules/network/ios/ios_l2_interface.py
--- a/lib/ansible/modules/network/ios/ios_l2_interface.py
+++ b/lib/ansible/modules/network/ios/ios_l2_interface.py
@@ -303,11 +303,12 @@ def vlan_range_to_list(vlans):
         for part in vlans.split(','):
             if part.lower() == 'none':
                 break
-            if '-' in part:
-                start, stop = (int(i) for i in part.split('-'))
-                result.extend(range(start, stop + 1))
-            else:
-                result.append(int(part))
+            if part:
+                if '-' in part:
+                    start, stop = (int(i) for i in part.split('-'))
+                    result.extend(range(start, stop + 1))
+                else:
+                    result.append(int(part))
     return sorted(result)
 
 


Test patch: