Repository: ansible/ansible

Issue:
ios_facts: gather hardware facts fails on IOS-XE

 - Bug Report


##### COMPONENT NAME
ios_facts

##### ANSIBLE VERSION
```
ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

```

##### CONFIGURATION


##### OS / ENVIRONMENT
CentOS Linux 7 managing Cisco IOS-XE devices

##### SUMMARY
The module ios_facts does not work on Cisco IOS-XE devices. The command "show memory statistics | include Processor" crashes. There is no “statistics” option behind “show memory”:

sh memory ?
 debug     Memory debugging commands
 detailed  Detailed memory information
 switch    Switch  number
 |         Output modifiers

##### STEPS TO REPRODUCE
Comment out the following two following lines in ios_facts.py:
   200         add_command(self.runner, 'show memory statistics | include Processor')
   206         data = self.runner.get_command('show memory statistics | include Processor')


Example Playbook:
```yaml
---
- hosts: c_ios
  connection: local
  vars:
  tasks:
    - name: "network device facts on ios devices: hardware"
      ios_facts:
        gather_subset: hardware
        provider: "{{ cli }}"
      register: iosfacts_hardware

```


##### EXPECTED RESULTS
```
[ansible@localhost V1]$ ansible-playbook iosfacts_wo_config_c_ios.yml -i hosts.ini --ask-vault-pass -v
Using /etc/ansible/ansible.cfg as config file
Vault password:

PLAY [c_ios] *******************************************************************

TASK [setup] *******************************************************************
ok: [10.82.1.1]

TASK [network device facts on ios devices: hardware] ***************************
ok: [10.82.1.1] => {"ansible_facts": {"ansible_net_filesystems": ["bootflash:"], "ansible_net_gather_subset": ["hardware", "default"], "ansible_net_hostname": "sy-eng-ch-grn-s-001", "ansible_net_image": "bootflash:cat4500e-universalk9.SPA.03.08.02.E.152-4.E2.bin", "ansible_net_model": null, "ansible_net_serialnum": "JAE171508YH", "ansible_net_version": null}, "changed": false}

PLAY RECAP *********************************************************************
10.82.1.1                  : ok=2    changed=0    unreachable=0    failed=0

```

##### ACTUAL RESULTS
```
[[ansible@localhost V1]$ ansible-playbook iosfacts_wo_config_c_ios.yml -i hosts.ini --ask-vault-pass -vvv
Using /etc/ansible/ansible.cfg as config file
Vault password:

PLAYBOOK: iosfacts_wo_config_c_ios.yml *****************************************
1 plays in iosfacts_wo_config_c_ios.yml

PLAY [c_ios] *******************************************************************

TASK [setup] *******************************************************************
Using module file /usr/lib/python2.7/site-packages/ansible/modules/core/system/setup.py
<10.82.1.1> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<10.82.1.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1484815415.2-154319293295953 `" && echo ansible-tmp-1484815415.2-154319293295953="` echo $HOME/.ansible/tmp/ansible-tmp-1484815415.2-154319293295953 `" ) && sleep 0'
<10.82.1.1> PUT /tmp/tmpNjPI97 TO /home/ansible/.ansible/tmp/ansible-tmp-1484815415.2-154319293295953/setup.py
<10.82.1.1> EXEC /bin/sh -c 'chmod u+x /home/ansible/.ansible/tmp/ansible-tmp-1484815415.2-154319293295953/ /home/ansible/.ansible/tmp/ansible-tmp-1484815415.2-154319293295953/setup.py && sleep 0'
<10.82.1.1> EXEC /bin/sh -c '/usr/bin/python /home/ansible/.ansible/tmp/ansible-tmp-1484815415.2-154319293295953/setup.py; rm -rf "/home/ansible/.ansible/tmp/ansible-tmp-1484815415.2-154319293295953/" > /dev/null 2>&1 && sleep 0'
ok: [10.82.1.1]

TASK [network device facts on ios devices: hardware] ***************************
task path: /home/ansible/AnsibleProjects/BNAS_Course/EasyWins/V1/iosfacts_wo_config_c_ios.yml:6
Using module file /usr/lib/python2.7/site-packages/ansible/modules/core/network/ios/ios_facts.py
<10.82.1.1> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<10.82.1.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1484815415.66-184587982314609 `" && echo ansible-tmp-1484815415.66-184587982314609="` echo $HOME/.ansible/tmp/ansible-tmp-1484815415.66-184587982314609 `" ) && sleep 0'
<10.82.1.1> PUT /tmp/tmpzBnhjn TO /home/ansible/.ansible/tmp/ansible-tmp-1484815415.66-184587982314609/ios_facts.py
<10.82.1.1> EXEC /bin/sh -c 'chmod u+x /home/ansible/.ansible/tmp/ansible-tmp-1484815415.66-184587982314609/ /home/ansible/.ansible/tmp/ansible-tmp-1484815415.66-184587982314609/ios_facts.py && sleep 0'
<10.82.1.1> EXEC /bin/sh -c '/usr/bin/python /home/ansible/.ansible/tmp/ansible-tmp-1484815415.66-184587982314609/ios_facts.py; rm -rf "/home/ansible/.ansible/tmp/ansible-tmp-1484815415.66-184587982314609/" > /dev/null 2>&1 && sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_vPS19f/ansible_module_ios_facts.py", line 461, in <module>
    main()
  File "/tmp/ansible_vPS19f/ansible_module_ios_facts.py", line 443, in main
    runner.run()
  File "/tmp/ansible_vPS19f/ansible_modlib.zip/ansible/module_utils/netcli.py", line 170, in run
  File "/tmp/ansible_vPS19f/ansible_modlib.zip/ansible/module_utils/netcli.py", line 98, in run_commands
  File "/tmp/ansible_vPS19f/ansible_modlib.zip/ansible/module_utils/shell.py", line 253, in run_commands
  File "/tmp/ansible_vPS19f/ansible_modlib.zip/ansible/module_utils/shell.py", line 250, in execute
ansible.module_utils.network.NetworkError: matched error in response: show memory statistics | include Processor
                                ^
% Invalid input detected at '^' marker.

sy-eng-ch-grn-s-001#

fatal: [10.82.1.1]: FAILED! => {
    "changed": false,
    "failed": true,
    "invocation": {
        "module_name": "ios_facts"
    },
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_vPS19f/ansible_module_ios_facts.py\", line 461, in <module>\n    main()\n  File \"/tmp/ansible_vPS19f/ansible_module_ios_facts.py\", line 443, in main\n    runner.run()\n  File \"/tmp/ansible_vPS19f/ansible_modlib.zip/ansible/module_utils/netcli.py\", line 170, in run\n  File \"/tmp/ansible_vPS19f/ansible_modlib.zip/ansible/module_utils/netcli.py\", line 98, in run_commands\n  File \"/tmp/ansible_vPS19f/ansible_modlib.zip/ansible/module_utils/shell.py\", line 253, in run_commands\n  File \"/tmp/ansible_vPS19f/ansible_modlib.zip/ansible/module_utils/shell.py\", line 250, in execute\nansible.module_utils.network.NetworkError: matched error in response: show memory statistics | include Processor\r\n                                ^\r\n% Invalid input detected at '^' marker.\r\n\r\nsy-eng-ch-grn-s-001#\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE"
}
        to retry, use: --limit @/home/ansible/AnsibleProjects/BNAS_Course/EasyWins/V1/iosfacts_wo_config_c_ios.retry

PLAY RECAP *********************************************************************
10.82.1.1                  : ok=1    changed=0    unreachable=0    failed=1

```



Discussion:
cc @privateip @gundalow @Qalthos
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@bittsi 

Could you please provide a 'show ver' command output?
I don't have a Cisco XE device to play with in our lab, I'm thinking about detecting the OS version via 'show ver' and branch the code in ios_facts depending if it's IOS or IOS-XE .

Thanks
Hi all

Please find attached a "sh version” output for IOS-XE and IOS.

Because with ios_facts I didn’t get what I would have expected, I also did the differentiation for IOS and IOS-XE with show version in my project, :

ios:
---
- ios_command:
      provider: "{{ cli }}"
      commands: "show version"
  register: result
- set_fact:
    sw_version: "{{result.stdout[0] | regex_search('Version\\s+(?P<version>\\S+),','\\g<version>') | join}}"
    os_type: "IOS"
    hw_type: "{{result.stdout[0] | regex_search('(?P<type>WS[A-Za-z0-9\\-]+)\\s+','\\g<type>') | join}}"




ios_xe:

---
- ios_command:
      provider: "{{ cli }}"
      commands: "show version"
  register: result
- set_fact:
    sw_version: "{{result.stdout[0] | regex_search('Version\\s+(?P<version>\\S+) RELEASE','\\g<version>') | join}}"
    os_type: "IOS-XE"
    hw_type: "{{result.stdout[0] | regex_search('(?P<type>WS[A-Za-z0-9\\-]+)\\s+','\\g<type>') | join}}"


Regards,
Simon





> On 24 Feb 2017, at 09:58, Ricardo Carrillo Cruz <notifications@github.com> wrote:
> 
> @bittsi <https://github.com/bittsi>
> Could you please provide a 'show ver' command output?
> I don't have a Cisco XE device to play with in our lab, I'm thinking about detecting the OS version via 'show ver' and branch the code in ios_facts depending if it's IOS or IOS-XE .
> 
> Thanks
> 
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub <https://github.com/ansible/ansible/issues/20443#issuecomment-282239337>, or mute the thread <https://github.com/notifications/unsubscribe-auth/ALqgC0vPKWtgECsSL8WEzhD7_L8XUh8hks5rfpu2gaJpZM4Ln2JP>.
> 


Cisco IOS Software, IOS-XE Software, Catalyst L3 Switch Software (CAT3K_CAA-UNIVERSALK9-M), Version 03.03.03SE RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2014 by Cisco Systems, Inc.
Compiled Sun 27-Apr-14 18:33 by prod_rel_team



Cisco IOS-XE software, Copyright (c) 2005-2014 by cisco Systems, Inc.
All rights reserved.  Certain components of Cisco IOS-XE software are
licensed under the GNU General Public License ("GPL") Version 2.0.  The

software code licensed under GPL Version 2.0 is free software that comes

with ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such
GPL code under the terms of GPL Version 2.0.
(http://www.gnu.org/licenses/gpl-2.0.html) For more details, see the
documentation or "License Notice" file accompanying the IOS-XE software,

or the applicable URL provided on the flyer accompanying the IOS-XE
software.



ROM: IOS-XE ROMMON
BOOTLDR: CAT3K_CAA Boot Loader (CAT3K_CAA-HBOOT-M) Version 1.2, RELEASE SOFTWARE (P)

sy-eng-ch-beb-s-025 uptime is 44 weeks, 5 days, 9 hours, 5 minutes
Uptime for this control processor is 44 weeks, 5 days, 9 hours, 8 minutes
System returned to ROM by reload
System restarted at 02:01:30 MEST Sun Apr 17 2016
System image file is "flash:packages.conf"
Last reload reason: reload



This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.

Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you

agree to comply with applicable laws and regulations. If you are unable

to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to

export@cisco.com.

License Level: Ipbase
License Type: Permanent
Next reload license Level: Ipbase

cisco WS-C3650-24PD (MIPS) processor with 4194304K bytes of physical memory.
Processor board ID FDO1748Q00Y
2 Virtual Ethernet interfaces
26 Gigabit Ethernet interfaces
2 Ten Gigabit Ethernet interfaces
2048K bytes of non-volatile configuration memory.
4194304K bytes of physical memory.
250456K bytes of Crash Files at crashinfo:.
1609272K bytes of Flash at flash:.
0K bytes of Dummy USB Flash at usbflash0:.
0K bytes of  at webui:.

Base Ethernet MAC Address          : 24:e9:b3:79:3b:80
Motherboard Assembly Number        : 73-15122-05
Motherboard Serial Number          : FDO17470CWH
Model Revision Number              : A0
Motherboard Revision Number        : A0
Model Number                       : WS-C3650-24PD
System Serial Number               : FDO1748Q00Y


Switch Ports Model              SW Version        SW Image              Mode
------ ----- -----              ----------        ----------            ----
*    1 28    WS-C3650-24PD      03.03.03SE        cat3k_caa-universalk9 INSTALL


Configuration register is 0x102

Cisco IOS Software, C3560C Software (C3560c405ex-UNIVERSALK9-M), Version 12.2(55)EX2, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2011 by Cisco Systems, Inc.
Compiled Wed 18-May-11 15:35 by prod_rel_team
Image text-base: 0x00003000, data-base: 0x02800000

ROM: Bootstrap program is C3560C boot loader
BOOTLDR: C3560C Boot Loader (C3560C-HBOOT-M) Version 12.2(55r)EX11, RELEASE SOFTWARE (fc1)

sy-eng-ch-grn-s-023 uptime is 9 weeks, 3 days, 17 hours, 35 minutes
System returned to ROM by power-on
System restarted at 16:25:24 MET Mon Dec 19 2016
System image file is "flash:/c3560c405ex-universalk9-mz.122-55.EX2/c3560c405ex-universalk9-mz.122-55.EX2.bin"


This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.

Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you

agree to comply with applicable laws and regulations. If you are unable

to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to

export@cisco.com.

License Level: ipbase
License Type: Permanent
Next reload license Level: ipbase

cisco WS-C3560CG-8PC-S (PowerPC) processor (revision D0) with 131072K bytes of memory.
Processor board ID FOC1730Y5BK
Last reset from power-on
2 Virtual Ethernet interfaces
10 Gigabit Ethernet interfaces
The password-recovery mechanism is enabled.

512K bytes of flash-simulated non-volatile configuration memory.
Base ethernet MAC Address       : 7C:95:F3:DF:F8:80
Motherboard assembly number     : 73-13272-07
Power supply part number        : 341-0407-01
Motherboard serial number       : FOC17303KM0
Power supply serial number      : DCB172423LH
Model revision number           : D0
Motherboard revision number     : A0
Model number                    : WS-C3560CG-8PC-S
System serial number            : FOC1730Y5BK
Top Assembly Part Number        : 800-33676-03
Top Assembly Revision Number    : A0
Version ID                      : V03
CLEI Code Number                : CMMD900ARC
Hardware Board Revision Number  : 0x00


Switch Ports Model              SW Version            SW Image
------ ----- -----              ----------            ----------
*    1 10    WS-C3560CG-8PC-S   12.2(55)EX2           C3560c405ex-UNIVERSALK9-M


Configuration register is 0xF

We plan to improve the support of different CLI commands per device type/versions in 2.4.
Putting 2.4 as milestone.
Additional show version from a Catalyst 4500 with SUP8 running 3.8.4 IOS-XE code. The issue for me seems to be the regex doesn't account for the missing `,` after the version number.

```Cisco
#show version
Cisco IOS Software, IOS-XE Software, Catalyst 4500 L3 Switch  Software (cat4500es8-UNIVERSALK9-M), Version 03.08.04.E RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2017 by Cisco Systems, Inc.
Compiled Thu 06-Apr-17 08:19 by prod_rel_team



Cisco IOS-XE software, Copyright (c) 2005-2015 by cisco Systems, Inc.
All rights reserved.  Certain components of Cisco IOS-XE software are
licensed under the GNU General Public License ("GPL") Version 2.0.  The
software code licensed under GPL Version 2.0 is free software that comes
with ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such
GPL code under the terms of GPL Version 2.0.
(http://www.gnu.org/licenses/gpl-2.0.html) For more details, see the
documentation or "License Notice" file accompanying the IOS-XE software,
or the applicable URL provided on the flyer accompanying the IOS-XE
software.



ROM: 15.1(1r)SG6
et-1a-gw1 uptime is 15 weeks, 1 day, 22 hours, 31 minutes
Uptime for this control processor is 15 weeks, 1 day, 22 hours, 33 minutes
System returned to ROM by SSO Switchover
System restarted at 21:48:55 MDT Wed May 17 2017
System image file is "bootflash:cat4500es8-universalk9.SPA.03.08.04.E.152-4.E4.bin"
Jawa Revision 3, RadTrooper Revision 0x0.0x41, Conan Revision 0x1B9E


Last reload reason: power-on



This product contains cryptographic features and is subject to United
States and local country laws governing import, export, transfer and
use. Delivery of Cisco cryptographic products does not imply
third-party authority to import, export, distribute or use encryption.
Importers, exporters, distributors and users are responsible for
compliance with U.S. and local country laws. By using this product you
agree to comply with applicable laws and regulations. If you are unable
to comply with U.S. and local laws, return this product immediately.

A summary of U.S. laws governing Cisco cryptographic products may be found at:
http://www.cisco.com/wwl/export/crypto/tool/stqrg.html

If you require further assistance please contact us by sending email to
export@cisco.com.


License Information for 'WS-X45-SUP8-E'
    License Level: ipbase   Type: Permanent Right-To-Use
    Next reboot license Level: ipbase

cisco WS-C4510R+E (P5040) processor (revision 2) with 4194304K bytes of physical memory.
Processor board ID FOX1514GKLQ
P5040 CPU at 2.2GHz, Supervisor 8-E
Last reset from PowerUp
5 Virtual Ethernet interfaces
360 Gigabit Ethernet interfaces
40 Ten Gigabit Ethernet interfaces
511K bytes of non-volatile configuration memory.

Configuration register is 0x2102
```
Should also note this is still an issue in 2.4 development.
The following regex change will fix the issue for the couple of switches I have looked at in my stock.

https://regex101.com/r/WCVPWb/1

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
@@ -184,7 +184,7 @@ def populate(self):
             self.facts['hostname'] = self.parse_hostname(data)
 
     def parse_version(self, data):
-        match = re.search(r'Version (\S+),', data)
+        match = re.search(r'Version (\S+?)(?:,\s|\s)', data)
         if match:
             return match.group(1)
 


Test patch: