Repository: ansible/ansible

Issue:
ios_facts: 'show interfaces' gives different output from 'show ipv6 interfaces'
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
ios_facts

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.1.0
  config file = /opt/isv/netflow/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.13 (default, Dec 24 2016, 13:29:06) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
<!---
Mention 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.
-->
- RHEL 7
- Cisco IOS Software, C2951 Software (C2951-UNIVERSALK9-M), Version 15.4(3)M3, RELEASE SOFTWARE (fc2)
##### SUMMARY
<!--- Explain the problem briefly -->
ios_facts fails on a VoIP interface when doing a fact-gather of all subsets.

The problem appears to be because `parse_interfaces` doesn't present the Voip-related interfaces, so when `populate_ipv6_interfaces` is run, it finds the new Voip interfaces, but can't tie them back to any existing key and so fails.

https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/ios/ios_facts.py#L268
##### 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
$ cat cisco-router.yaml 
---
- hosts: cisco_routers
  gather_facts: no
  connection: local 


  tasks:
  - name: GET CREDENTIALS
    include_vars: secrets.yaml

  - name: DEFINE CONNECTION
    set_fact:
       connection:
          authorize: yes
          host: "{{ inventory_hostname }}"
          username: "{{ creds['username'] }}"
          password: "{{ creds['password'] }}"
          auth_pass: "{{ creds['auth_pass'] }}"

  - name: Get Cisco Facts
    ios_facts:
      gather_subset: all
      provider: "{{ connection }}"
    register: thefacts

  - debug: var=thefacts.stdout_lines
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Router facts are returned

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
TASK [Get Cisco Facts] ***************************************************************************************************************************
task path: /opt/ansible/cisco-router.yaml:20
<cisco_rtr1> using connection plugin network_cli
<cisco_rtr1> socket_path: /home/usertest/.ansible/pc/23176f0a55
open_shell() returned 0 ok 
Using module file /opt/virtualenvs/ansiblerun/lib/python2.7/site-packages/ansible/modules/network/ios/ios_facts.py
<cisco_rtr1> ESTABLISH LOCAL CONNECTION FOR USER: usertest
<cisco_rtr1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<cisco_rtr1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/usertest/.ansible/tmp/ansible-tmp-1500908715.18-68393613750600 `" && echo ansible-tmp-1500908715.18-68393613750600="` echo /home/usertest/.ansible/tmp/ansible-tmp-1500908715.18-68393613750600 `" ) && sleep 0'
<cisco_rtr1> PUT /tmp/tmpRQRKJf TO /home/usertest/.ansible/tmp/ansible-tmp-1500908715.18-68393613750600/ios_facts.py
<cisco_rtr1> EXEC /bin/sh -c 'chmod u+x /home/usertest/.ansible/tmp/ansible-tmp-1500908715.18-68393613750600/ /home/usertest/.ansible/tmp/ansible-tmp-1500908715.18-68393613750600/ios_facts.py && sleep 0'
<cisco_rtr1> EXEC /bin/sh -c '/usr/bin/python /home/usertest/.ansible/tmp/ansible-tmp-1500908715.18-68393613750600/ios_facts.py; rm -rf "/home/usertest/.ansible/tmp/ansible-tmp-1500908715.18-68393613750600/" > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_POLuRN/ansible_module_ios_facts.py", line 489, in <module>
    main()
  File "/tmp/ansible_POLuRN/ansible_module_ios_facts.py", line 474, in main
    inst.populate()
  File "/tmp/ansible_POLuRN/ansible_module_ios_facts.py", line 269, in populate
    self.populate_ipv6_interfaces(data)
  File "/tmp/ansible_POLuRN/ansible_module_ios_facts.py", line 302, in populate_ipv6_interfaces
    self.facts['interfaces'][key]['ipv6'] = list()
KeyError: 'VoIP-Null0'

fatal: [cisco_rtr1]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_POLuRN/ansible_module_ios_facts.py\", line 489, in <module>\n    main()\n  File \"/tmp/ansible_POLuRN/ansible_module_ios_facts.py\", line 474, in main\n    inst.populate()\n  File \"/tmp/ansible_POLuRN/ansible_module_ios_facts.py\", line 269, in populate\n    self.populate_ipv6_interfaces(data)\n  File \"/tmp/ansible_POLuRN/ansible_module_ios_facts.py\", line 302, in populate_ipv6_interfaces\n    self.facts['interfaces'][key]['ipv6'] = list()\nKeyError: 'VoIP-Null0'\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE", 
    "rc": 0
}
        to retry, use: --limit @/opt/ansible/cisco-router.retry
```
Here's the truncated output of `show ipv6 interface`
```
#sh ipv6 interface | i Null
VoIP-Null0 is up, line protocol is up
VoipNull0.1 is up, line protocol is down
VoipNull0.2 is up, line protocol is down
VoipNull0.3 is up, line protocol is down
VoipNull0.4 is up, line protocol is down
VoipNull0.5 is up, line protocol is down
VoipNull0.6 is up, line protocol is down
```


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

Resolving patch:
diff --git a/lib/ansible/modules/network/ios/ios_facts.py b/lib/ansible/modules/network/ios/ios_facts.py
--- a/lib/ansible/modules/network/ios/ios_facts.py
+++ b/lib/ansible/modules/network/ios/ios_facts.py
@@ -299,7 +299,11 @@ def populate_interfaces(self, interfaces):
 
     def populate_ipv6_interfaces(self, data):
         for key, value in iteritems(data):
-            self.facts['interfaces'][key]['ipv6'] = list()
+            try:
+                self.facts['interfaces'][key]['ipv6'] = list()
+            except KeyError:
+                self.facts['interfaces'][key] = dict()
+                self.facts['interfaces'][key]['ipv6'] = list()
             addresses = re.findall(r'\s+(.+), subnet', value, re.M)
             subnets = re.findall(r', subnet is (.+)$', value, re.M)
             for addr, subnet in zip(addresses, subnets):


Test patch: