Repository: ansible/ansible

Issue:
iosxr facts - incompatible show version command
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and devel branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report


##### COMPONENT NAME
<!---
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
-->
- iosxr_facts.py
- iosxr.py

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.4.+ 
ansible 2.5.0rc2

```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste 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).
-->
N/A

##### 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.
-->
N/A

##### SUMMARY
<!--- Explain the problem briefly -->
show command for gathering facts in IOS-XR are wrong set:
ios-xr will report a error receiving "show version brief"
correct command should be "show version"

Following packages should be corrected:
/usr/local/lib/python2.7/dist-packages/ansible/plugins/cliconf/iosxr.py

class Cliconf(CliconfBase):

    def get_device_info(self):
        device_info = {}

        device_info['network_os'] = 'iosxr'
        reply = self.get(b'show version brief')  <- wrong
        reply = self.get(b'show version')  <- correct

and accordingly:
/usr/local/lib/python2.7/dist-packages/ansible/modules/network/iosxr_1/iosxr_facts.py

class Default(FactsBase):

    def commands(self):
        return(['show version brief'])  <- wrong
        return(['show version'])  <- correct

##### 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

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:
Files identified in the description:
* [lib/ansible/module_utils/network/iosxr/iosxr.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/network/iosxr/iosxr.py)
* [lib/ansible/modules/network/iosxr/iosxr_facts.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/iosxr/iosxr_facts.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 @ganeshrn @gdpak @gundalow @kedarX @privateip @rcarrillocruz @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@tomatoosss Can you please paste "show version" from your iOS-xr device ? I noticed similar  problem with IOS_XR box image but I am not sure if it is common across hardware or with official ios-xrv /latest software release? 
@gdpak, in the example above is this a virtual instance (IOS-XRv9k):

RP/0/RP0/CPU0:ios#show version
Mon Mar  5 02:02:34.622 UTC

Cisco IOS XR Software, Version 6.2.3
Copyright (c) 2013-2017 by Cisco Systems, Inc.

Build Information:
 Built By     : ahoang
 Built On     : Thu Dec 21 19:30:35 PST 2017
 Build Host   : iox-lnx-053
 Workspace    : /auto/srcarchive11/production/6.2.3/xrv9k/workspace
 Version      : 6.2.3
 Location     : /opt/cisco/XR/packages/

cisco IOS-XRv 9000 () processor 
System uptime is 3 hours, 20 minutes

RP/0/RP0/CPU0:ios#
RP/0/RP0/CPU0:ios#
RP/0/RP0/CPU0:ios#
RP/0/RP0/CPU0:ios#show version brief
                               ^
% Invalid input detected at '^' marker.



On "real" ios-xr instance (5.3.4) both commands will work.
" show version brief" will deliver basic information
" show version" will deliver basic information + active packages information

 #sh version
Mon Mar 12 12:55:35.040 CET

Cisco IOS XR Software, Version 5.3.4[Default]
Copyright (c) 2017 by Cisco Systems, Inc.

ROM: System Bootstrap, Version 3.04(20160331:102636) [ASR9K ROMMON],  

X uptime is 16 weeks, 6 days, 19 hours, 50 minutes

cisco ASR9K Series (P4040) processor with 8388608K bytes of memory.
P4040 processor at 1500MHz, Revision 3.0
ASR-9001 Chassis

2 Management Ethernet
4 TenGigE
4 DWDM controller(s)
4 WANPHY controller(s)
219k bytes of non-volatile configuration memory.
2868M bytes of hard disk.
3915760k bytes of disk0: (Sector size 512 bytes).

Configuration register on node 0/RSP0/CPU0 is 0x2102
Boot device on node 0/RSP0/CPU0 is disk0:
Package active on node 0/RSP0/CPU0:
(...)


#sh version brief 
Mon Mar 12 12:55:47.926 CET

Cisco IOS XR Software, Version 5.3.4[Default]
Copyright (c) 2017 by Cisco Systems, Inc.

ROM: System Bootstrap, Version 3.04(20160331:102636) [ASR9K ROMMON],  

x uptime is 16 weeks, 6 days, 19 hours, 51 minutes

cisco ASR9K Series (P4040) processor with 8388608K bytes of memory.
P4040 processor at 1500MHz, Revision 3.0
ASR-9001 Chassis

2 Management Ethernet
4 TenGigE
4 DWDM controller(s)
4 WANPHY controller(s)
219k bytes of non-volatile configuration memory.
2868M bytes of hard disk.
3915760k bytes of disk0: (Sector size 512 bytes).



Until we don’t catch and parse the active packages information, perhaps it make sense to execute only "show version " to avoid errors between different versions.


Resolving patch:
diff --git a/lib/ansible/modules/network/iosxr/iosxr_facts.py b/lib/ansible/modules/network/iosxr/iosxr_facts.py
--- a/lib/ansible/modules/network/iosxr/iosxr_facts.py
+++ b/lib/ansible/modules/network/iosxr/iosxr_facts.py
@@ -136,12 +136,12 @@ def commands(self):
 class Default(FactsBase):
 
     def commands(self):
-        return(['show version brief'])
+        return(['show version | utility head -n 20'])
 
     def populate(self, results):
-        self.facts['version'] = self.parse_version(results['show version brief'])
-        self.facts['image'] = self.parse_image(results['show version brief'])
-        self.facts['hostname'] = self.parse_hostname(results['show version brief'])
+        self.facts['version'] = self.parse_version(results['show version | utility head -n 20'])
+        self.facts['image'] = self.parse_image(results['show version | utility head -n 20'])
+        self.facts['hostname'] = self.parse_hostname(results['show version | utility head -n 20'])
 
     def parse_version(self, data):
         match = re.search(r'Version (\S+)$', data, re.M)
diff --git a/lib/ansible/plugins/cliconf/iosxr.py b/lib/ansible/plugins/cliconf/iosxr.py
--- a/lib/ansible/plugins/cliconf/iosxr.py
+++ b/lib/ansible/plugins/cliconf/iosxr.py
@@ -35,7 +35,7 @@ def get_device_info(self):
         device_info = {}
 
         device_info['network_os'] = 'iosxr'
-        reply = self.get(b'show version brief')
+        reply = self.get(b'show version | utility head -n 20')
         data = to_text(reply, errors='surrogate_or_strict').strip()
 
         match = re.search(r'Version (\S+)$', data, re.M)


Test patch:
diff --git a/test/units/modules/network/iosxr/fixtures/show_version___utility_head_-n_20 b/test/units/modules/network/iosxr/fixtures/show_version___utility_head_-n_20
new file mode 100644
--- /dev/null
+++ b/test/units/modules/network/iosxr/fixtures/show_version___utility_head_-n_20
@@ -0,0 +1,18 @@
+Cisco IOS XR Software, Version 6.0.0[Default]
+Copyright (c) 2015 by Cisco Systems, Inc.
+
+ROM: GRUB, Version 1.99(0), DEV RELEASE
+
+iosxr01 uptime is 11 weeks, 6 days, 2 hours, 2 minutes
+System image file is "bootflash:disk0/xrvr-os-mbi-6.0.0/mbixrvr-rp.vm"
+
+cisco IOS XRv Series (Pentium Celeron Stepping 3) processor with 3169911K bytes
+of memory.
+Pentium Celeron Stepping 3 processor at 3836MHz, Revision 2.174
+IOS XRv Chassis
+
+1 Management Ethernet
+6 GigabitEthernet
+97070k bytes of non-volatile configuration memory.
+866M bytes of hard disk.
+2321392k bytes of disk0: (Sector size 512 bytes).
diff --git a/test/units/modules/network/iosxr/test_iosxr_facts.py b/test/units/modules/network/iosxr/test_iosxr_facts.py
--- a/test/units/modules/network/iosxr/test_iosxr_facts.py
+++ b/test/units/modules/network/iosxr/test_iosxr_facts.py
@@ -57,6 +57,7 @@ def load_from_file(*args, **kwargs):
                     command = item
                 filename = str(command).replace(' ', '_')
                 filename = filename.replace('/', '7')
+                filename = filename.replace('|', '_')
                 output.append(load_fixture(filename))
             return output