Repository: ansible/ansible

Issue:
ios_vlan fails to correctly identify vlans starting with number 9
<!---
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
The Fix "ios_vlan to correctly identify unmodified config when having long interface names" #40145 introduces one minor bug. The parser in parse_to_logical_rows(out) ignores vlans starting with number 9.

The line 221:
if '0' < l[0] < '9':
Perhaps should read:
if '0' < l[0] <= '9':

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
ios_vlan

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/jagudo/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /home/jagudo/dev/venvs/ansible-2.6/local/lib/python2.7/site-packages/ansible
  executable location = /home/jagudo/dev/venvs/ansible-2.6/bin/ansible
  python version = 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0]
```

##### 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.-->
Running ansible on Ubuntu 18.04
Tested against Cisco IOS 12.2 15.2

##### 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. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- hosts: ios_devices
  gather_facts: no
  connection: local

  vars:
    vlans:
      - vlan_id: 1
        name: default
      - vlan_id: 10
        name: Servers
      - vlan_id: 920
        name: Test
      - vlan_id: 1002
        name: fddi-default
      - vlan_id: 1003
        name: token-ring-default
      - vlan_id: 1004
        name: fddinet-default
      - vlan_id: 1005
        name: trnet-default

  tasks:
  - name: Config Vlans
    ios_vlan:
      aggregate: "{{ vlans }}"
      provider: "{{ provider }}"
      purge: yes
    when: vlans is defined
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Config Vlans defined (1,10,920,1002,1003,1004,1005)

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
The vlans starting with 9 are configured each time the playbook is run because the parser ignores the output starting with 9. Also are not purged / removed if state dictates it.
<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:
Files identified in the description:
* [lib/ansible/modules/network/ios/ios_vlan.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/ios/ios_vlan.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 @privateip @rcarrillocruz @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@pierremahot and I introduced this bug :(

I would probably even do `if l[0].isdigit(): ` test now.
@HD650 could you please fix this. Patch is trivial but requires more regression tests.
@webknjaz No problem.

Resolving patch:
diff --git a/lib/ansible/modules/network/ios/ios_vlan.py b/lib/ansible/modules/network/ios/ios_vlan.py
--- a/lib/ansible/modules/network/ios/ios_vlan.py
+++ b/lib/ansible/modules/network/ios/ios_vlan.py
@@ -218,7 +218,7 @@ def parse_to_logical_rows(out):
         if not l:
             """Skip empty lines."""
             continue
-        if '0' < l[0] < '9':
+        if '0' < l[0] <= '9':
             """Line starting with a number."""
             if started_yielding:
                 yield cur_row


Test patch:
diff --git a/test/units/modules/network/ios/fixtures/ios_vlan_config.cfg b/test/units/modules/network/ios/fixtures/ios_vlan_config.cfg
--- a/test/units/modules/network/ios/fixtures/ios_vlan_config.cfg
+++ b/test/units/modules/network/ios/fixtures/ios_vlan_config.cfg
@@ -4,5 +4,6 @@ VLAN Name                             Status    Ports
                                                 Gi1/0/52
                                                 Gi1/0/54
 2    vlan2                            active    Gi1/0/6, Gi1/0/7
+9    vlan9                            active    Gi1/0/6
 1002 fddi-default                     act/unsup 
-1003 fddo-default                     act/unsup
\ No newline at end of file
+1003 fddo-default                     act/unsup
diff --git a/test/units/modules/network/ios/test_ios_vlan.py b/test/units/modules/network/ios/test_ios_vlan.py
--- a/test/units/modules/network/ios/test_ios_vlan.py
+++ b/test/units/modules/network/ios/test_ios_vlan.py
@@ -59,6 +59,12 @@ def test_ios_vlan_create(self):
         ]
         self.assertEqual(result['commands'], expected_commands)
 
+    def test_ios_vlan_id_startwith_9(self):
+        set_module_args({'vlan_id': '9', 'name': 'vlan9', 'state': 'present'})
+        result = self.execute_module(changed=False)
+        expected_commands = []
+        self.assertEqual(result['commands'], expected_commands)
+
     def test_ios_vlan_rename(self):
         set_module_args({'vlan_id': '2', 'name': 'test', 'state': 'present'})
         result = self.execute_module(changed=True)
@@ -121,6 +127,14 @@ def test_parse_vlan_brief(self):
                 'state': 'active',
                 'vlan_id': '2',
             },
+            {
+                'name': 'vlan9',
+                'interfaces': [
+                    'GigabitEthernet1/0/6',
+                ],
+                'state': 'active',
+                'vlan_id': '9',
+            },
             {
                 'name': 'fddi-default',
                 'interfaces': [],