Repository: ansible/ansible

Issue:
iosxr_config admin: yes - Causes all following commands to fail
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
- iosxr_config module

##### ANSIBLE VERSION
```
$ ansible --version     
ansible 2.5.0                                       
  config file = /etc/ansible/ansible.cfg            
  configured module search path = [u'/home/raulgs/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']                                                                                                
  ansible python module location = /usr/lib/python2.7/site-packages/ansible                              
  executable location = /usr/bin/ansible            
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
```

##### CONFIGURATION
```
DEFAULT_ROLES_PATH(/etc/ansible/ansible.cfg) = [u'/etc/ansible/roles', u'/usr/share/ansible/roles']
HOST_KEY_CHECKING(/etc/ansible/ansible.cfg) = False
```

##### OS / ENVIRONMENT
- 3.10.0-693.11.6.el7.x86_64 #1 SMP Thu Jan 4 01:06:37 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

##### SUMMARY
iosxr_config with admin: yes statement is not leaving the admin mode after running the command. I am pretty sure that this worked with <2.5. Other commands from the same module with admin: yes - as well as commands from other modules are not working too.

##### STEPS TO REPRODUCE
Run below
```
- name: configure root                             
  iosxr_config:                                    
    admin: yes                                     
    lines:                                         
      - username admins

- name: get user
  iosxr_command:                                    
    commands:                                       
      - sh run form | i username
```
##### EXPECTED RESULTS
Module should jump out of the admin mode after leaving the configure root section.

##### ACTUAL RESULTS
After running a iosxr_config command with admin: yes variable - NO other command can be executed! 

```
16.04.18 13:57:35  TASK [common : configure root] *************************************************
16.04.18 13:57:35  task path: /home/jenkins-ci/config_iosxr/roles/common/tasks/main.yaml:326
16.04.18 13:57:35  <rt-1> using connection plugin network_cli (was local)
16.04.18 13:57:35  <rt-1> starting connection from persistent connection plugin
16.04.18 13:57:36  <rt-1> found existing local domain socket, using it!
16.04.18 13:57:36  <rt-1> updating play_context for connection
16.04.18 13:57:36  <rt-1> 
16.04.18 13:57:36  <rt-1> local domain socket path is /home/jenkins-ci/.ansible/pc/bcf1355494
16.04.18 13:57:36  <rt-1> socket_path: /home/jenkins-ci/.ansible/pc/bcf1355494
16.04.18 13:57:36  Using module file /usr/lib/python2.7/site-packages/ansible/modules/network/iosxr/iosxr_config.py
16.04.18 13:57:36  <rt-1> ESTABLISH LOCAL CONNECTION FOR USER: jenkins-ci
16.04.18 13:57:36  <rt-1> EXEC /bin/sh -c 'echo ~ && sleep 0'
16.04.18 13:57:36  <rt-1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231 `" && echo ansible-tmp-1523879856.88-230380841114231="` echo /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231 `" ) && sleep 0'
16.04.18 13:57:36  <rt-1> PUT /home/jenkins-ci/.ansible/tmp/ansible-local-32627i6OfvX/tmpbrAMDt TO /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231/iosxr_config.py
16.04.18 13:57:36  <rt-1> EXEC /bin/sh -c 'chmod u+x /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231/ /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231/iosxr_config.py && sleep 0'
16.04.18 13:57:36  <rt-1> EXEC /bin/sh -c '/usr/bin/python /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231/iosxr_config.py && sleep 0'
16.04.18 13:57:42  <rt-1> EXEC /bin/sh -c 'rm -f -r /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231/ > /dev/null 2>&1 && sleep 0'
16.04.18 13:57:42  changed: [rt-decix-1] => (item={u'group': u'root-system', u'name': u'root'}) => {
16.04.18 13:57:42      "changed": true, 
16.04.18 13:57:42      "commands": [
16.04.18 13:57:42          "username root", 
16.04.18 13:57:42          "username root group root-system", 
16.04.18 13:57:42          "username root secret 0 ********"
16.04.18 13:57:42      ], 
16.04.18 13:57:42      "invocation": {
16.04.18 13:57:42          "module_args": {
16.04.18 13:57:42              "admin": true, 
16.04.18 13:57:42              "after": null, 
16.04.18 13:57:42              "backup": false, 
16.04.18 13:57:42              "before": null, 
16.04.18 13:57:42              "comment": "configured by iosxr_config", 
16.04.18 13:57:42              "config": null, 
16.04.18 13:57:42              "force": false, 
16.04.18 13:57:42              "host": null, 
16.04.18 13:57:42              "lines": [
16.04.18 13:57:42                  "username root", 
16.04.18 13:57:42                  "username root group root-system", 
16.04.18 13:57:42                  "username root secret 0 ********"
16.04.18 13:57:42              ], 
16.04.18 13:57:42              "match": "line", 
16.04.18 13:57:42              "parents": null, 
16.04.18 13:57:42              "password": null, 
16.04.18 13:57:42              "port": null, 
16.04.18 13:57:42              "provider": {
16.04.18 13:57:42                  "host": null, 
16.04.18 13:57:42                  "password": null, 
16.04.18 13:57:42                  "port": null, 
16.04.18 13:57:42                  "ssh_keyfile": null, 
16.04.18 13:57:42                  "timeout": null, 
16.04.18 13:57:42                  "transport": "cli", 
16.04.18 13:57:42                  "username": null
16.04.18 13:57:42              }, 
16.04.18 13:57:42              "replace": "line", 
16.04.18 13:57:42              "src": null, 
16.04.18 13:57:42              "ssh_keyfile": null, 
16.04.18 13:57:42              "timeout": null, 
16.04.18 13:57:42              "username": null
16.04.18 13:57:42          }
16.04.18 13:57:42      }, 
16.04.18 13:57:42      "item": {
16.04.18 13:57:42          "group": "root-system", 
16.04.18 13:57:42          "name": "root"
16.04.18 13:57:42      }
16.04.18 13:57:42  }
16.04.18 13:57:42  
16.04.18 13:57:42  TASK [common : get user] *******************************************************
16.04.18 13:57:42  task path: /home/jenkins-ci/config_iosxr/roles/common/tasks/main.yaml:338
16.04.18 13:57:42  <rt-1> using connection plugin network_cli (was local)
16.04.18 13:57:42  <rt-1> starting connection from persistent connection plugin
16.04.18 13:57:43  <rt-1> found existing local domain socket, using it!
16.04.18 13:57:43  <rt-1> updating play_context for connection
16.04.18 13:57:43  <rt-1> 
16.04.18 13:57:43  <rt-1> local domain socket path is /home/jenkins-ci/.ansible/pc/bcf1355494
16.04.18 13:57:43  <rt-1> socket_path: /home/jenkins-ci/.ansible/pc/bcf1355494
16.04.18 13:57:43  <rt-1> wrong context, sending exit to device
16.04.18 13:57:43  The full traceback is:
16.04.18 13:57:43  Traceback (most recent call last):
16.04.18 13:57:43    File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 138, in run
16.04.18 13:57:43      res = self._execute()
16.04.18 13:57:43    File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 554, in _execute
16.04.18 13:57:43      result = self._handler.run(task_vars=variables)
16.04.18 13:57:43    File "/usr/lib/python2.7/site-packages/ansible/plugins/action/iosxr.py", line 97, in run
16.04.18 13:57:43      conn.send_command('abort')
16.04.18 13:57:43    File "/usr/lib/python2.7/site-packages/ansible/module_utils/connection.py", line 146, in __rpc__
16.04.18 13:57:43      raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
16.04.18 13:57:43  ConnectionError: abort
16.04.18 13:57:43  
                                 ^
16.04.18 13:57:43  % Invalid input detected at '^' marker.
16.04.18 13:57:43  RP/0/RSP0/CPU0:rt-decix-1(admin)#
16.04.18 13:57:43  fatal: [rt-decix-1]: FAILED! => {
16.04.18 13:57:43      "msg": "Unexpected failure during module execution.", 
16.04.18 13:57:43      "stdout": ""
16.04.18 13:57:43  }
16.04.18 13:57:43  	to retry, use: --limit @/home/jenkins-ci/config_iosxr/roles/conf_device.retry
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/network/iosxr/iosxr_config.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/iosxr/iosxr_config.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 @gdpak @privateip @rcarrillocruz
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I just downgraded ansible to version 2.4.0 on my notebook an run the exact same playbook without any issues. So something must have changed with version 2.5 :-1: 
@rgarcia89 Are you able to test out #39594 PR ?


@rcarrillocruz I am currently in Copenhagen @KubeCon.
Monday should be realistic for a test.
I will let you know once I did it
Thanks :)

Resolving patch:
diff --git a/lib/ansible/module_utils/network/iosxr/iosxr.py b/lib/ansible/module_utils/network/iosxr/iosxr.py
--- a/lib/ansible/module_utils/network/iosxr/iosxr.py
+++ b/lib/ansible/module_utils/network/iosxr/iosxr.py
@@ -426,6 +426,8 @@ def load_config(module, command_filter, commit=False, replace=False,
         elif commit:
             commit_config(module, comment=comment)
             conn.edit_config('end')
+            if admin:
+                conn.edit_config('exit')
         else:
             conn.discard_changes()
 


Test patch: