Repository: ansible/ansible

Issue:
ios_facts exception on WS-C3850-48T
<!---
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 -->
Receiving exception in ios_facts.py running against single WS-C3850-48T running IOS-XE 03.03.05SE

##### 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_facts.py 

Looks like the issue is related to attempting to run a command that is not available on the switch. 
```
sw01#show memory statistics
                               ^
% Invalid input detected at '^' marker.
```

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
$ ansible --version
ansible 2.5.3
  config file = None
  configured module search path = [u'/Users/jvanderaa/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Library/Python/2.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.10 (default, Oct  6 2017, 22:29:07) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
```

##### 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.-->

##### 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
        - name: IOS>> Get facts
          ios_facts:
            authorize: yes
            gather_subset: all
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Was looking to get the running configuration but ran into this with the issue when trying to get all the facts.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/dq/_tjy3jfn0j98n6j590nyr24jpxb_7r/T/ansible_ad9N9O/ansible_module_ios_facts.py\", line 518, in <module>\n    main()\n  File \"/var/folders/dq/_tjy3jfn0j98n6j590nyr24jpxb_7r/T/ansible_ad9N9O/ansible_module_ios_facts.py\", line 503, in main\n    inst.populate()\n  File \"/var/folders/dq/_tjy3jfn0j98n6j590nyr24jpxb_7r/T/ansible_ad9N9O/ansible_module_ios_facts.py\", line 232, in populate\n    if 'Processor' in l].pop()\nIndexError: pop from empty list\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
```



Discussion:

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
@@ -204,11 +204,11 @@ def parse_serialnum(self, data):
             return match.group(1)
 
     def parse_stacks(self, data):
-        match = re.findall(r'^Model number\s+: (\S+)', data, re.M)
+        match = re.findall(r'^Model [Nn]umber\s+: (\S+)', data, re.M)
         if match:
             self.facts['stacked_models'] = match
 
-        match = re.findall(r'^System serial number\s+: (\S+)', data, re.M)
+        match = re.findall(r'^System [Ss]erial [Nn]umber\s+: (\S+)', data, re.M)
         if match:
             self.facts['stacked_serialnums'] = match
 
@@ -228,12 +228,15 @@ def populate(self):
 
         data = self.responses[1]
         if data:
-            processor_line = [l for l in data.splitlines()
-                              if 'Processor' in l].pop()
-            match = re.findall(r'\s(\d+)\s', processor_line)
-            if match:
-                self.facts['memtotal_mb'] = int(match[0]) / 1024
-                self.facts['memfree_mb'] = int(match[3]) / 1024
+            if 'Invalid input detected' in data:
+                warnings.append('Unable to gather memory statistics')
+            else:
+                processor_line = [l for l in data.splitlines()
+                                  if 'Processor' in l].pop()
+                match = re.findall(r'\s(\d+)\s', processor_line)
+                if match:
+                    self.facts['memtotal_mb'] = int(match[0]) / 1024
+                    self.facts['memfree_mb'] = int(match[3]) / 1024
 
     def parse_filesystems(self, data):
         return re.findall(r'^Directory of (\S+)/', data, re.M)
@@ -446,6 +449,9 @@ def parse_lldp_port(self, data):
 
 VALID_SUBSETS = frozenset(FACT_SUBSETS.keys())
 
+global warnings
+warnings = list()
+
 
 def main():
     """main entry point for module execution
@@ -508,7 +514,6 @@ def main():
         key = 'ansible_net_%s' % key
         ansible_facts[key] = value
 
-    warnings = list()
     check_args(module, warnings)
 
     module.exit_json(ansible_facts=ansible_facts, warnings=warnings)
diff --git a/lib/ansible/plugins/terminal/ios.py b/lib/ansible/plugins/terminal/ios.py
--- a/lib/ansible/plugins/terminal/ios.py
+++ b/lib/ansible/plugins/terminal/ios.py
@@ -64,7 +64,7 @@ def on_become(self, passwd=None):
         if passwd:
             # Note: python-3.5 cannot combine u"" and r"" together.  Thus make
             # an r string and use to_text to ensure it's text on both py2 and py3.
-            cmd[u'prompt'] = to_text(r"[\r\n]password: ?$", errors='surrogate_or_strict')
+            cmd[u'prompt'] = to_text(r"[\r\n](?:Local_)?[Pp]assword: ?$", errors='surrogate_or_strict')
             cmd[u'answer'] = passwd
             cmd[u'prompt_retry_check'] = True
         try:


Test patch: