Repository: ansible/ansible

Issue:
Connection from net_cli should wait for prompt even on error
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
ios_config

##### ANSIBLE VERSION
```
ansible 2.3.0.0
  config file = /home/adminlocal/noiua-ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2]
```

##### CONFIGURATION
N/A

##### OS / ENVIRONMENT
N/A

##### SUMMARY
When an error is detected while configuration lines are sent, the module immediately exit without waiting for the prompt to appear in the output.
If ignore_error is used on the failing task, the connection is left "dirty", potentially with some output not read wich will be used as output for the remaining tasks.

##### STEPS TO REPRODUCE
```yaml
---
    - ios_config:
        lines:
          - crypto key generate rsa modulus 1024
      ignore_errors: true
    - ios_command:
        commands:
          - show switch
```

The output of the config line is as follow on the switch;
```
Switch(config)#crypto key generate rsa modulus 1024
% You already have RSA keys defined named Switch.intranet.
% They will be replaced.

% The key modulus size is 1024 bits
% Generating 1024 bit RSA keys, keys will be non-exportable...
[OK] (elapsed time was 1 seconds)

Switch(config)#
```
The switch hangs will generating the key (1 second).

##### EXPECTED RESULTS
The failling of the first task shouldn't impact the second task. All the output of the first task should be in the error message of the first task.

##### ACTUAL RESULTS
As the switch hangs will generating the key, the output is not entirely returned as error.
Only the following is returned:
```
% You already have RSA keys defined named Switch.intranet.
% They will be replaced.

% The key modulus size is 1024 bits
% Generating 1024 bit RSA keys, keys will be non-exportable...
```

Full run:
```
(ansible_venv)adminlocal@ansible-thibault:~/noiua-ansible (splunk_logs) $ time ansible-playbook -i inventory/samples/templates/hosts playbooks/test.yml -vvvv
Using /home/adminlocal/noiua-ansible/ansible.cfg as config file
Loading callback plugin skippy of type stdout, v2.0 from /opt/ansible_venv/local/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc

PLAYBOOK: test.yml *********************************************************************************************************************************************************************************************************************************************
1 plays in playbooks/test.yml

PLAY [all] *****************************************************************************************************************************************************************************************************************************************************
META: ran handlers

TASK [ios_config] **********************************************************************************************************************************************************************************************************************************************
task path: /home/adminlocal/noiua-ansible/playbooks/test.yml:6
Using module file /opt/ansible_venv/local/lib/python2.7/site-packages/ansible/modules/network/ios/ios_config.py
<10.20.124.52> ESTABLISH LOCAL CONNECTION FOR USER: adminlocal
<10.20.124.52> EXEC /bin/sh -c 'echo ~ && sleep 0'
<10.20.124.52> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/adminlocal/.ansible/tmp/ansible-tmp-1492635903.97-138825881250746 `" && echo ansible-tmp-1492635903.97-138825881250746="` echo /home/adminlocal/.ansible/tmp/ansible-tmp-1492635903.97-138825881250746 `" ) && sleep 0'
<10.20.124.52> PUT /tmp/tmpoCK6zK TO /home/adminlocal/.ansible/tmp/ansible-tmp-1492635903.97-138825881250746/ios_config.py
<10.20.124.52> EXEC /bin/sh -c 'chmod u+x /home/adminlocal/.ansible/tmp/ansible-tmp-1492635903.97-138825881250746/ /home/adminlocal/.ansible/tmp/ansible-tmp-1492635903.97-138825881250746/ios_config.py && sleep 0'
<10.20.124.52> EXEC /bin/sh -c '/usr/bin/python /home/adminlocal/.ansible/tmp/ansible-tmp-1492635903.97-138825881250746/ios_config.py && sleep 0'
fatal: [c3650]: FAILED! => {
    "changed": false, 
    "command": "crypto key generate rsa modulus 1024", 
    "failed": true, 
    "invocation": {
        "module_args": {
            "after": null, 
            "auth_pass": null, 
            "authorize": null, 
            "backup": false, 
            "before": null, 
            "config": null, 
            "defaults": false, 
            "force": false, 
            "host": null, 
            "lines": [
                "crypto key generate rsa modulus 1024"
            ], 
            "match": "line", 
            "multiline_delimiter": "@", 
            "parents": null, 
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "port": null, 
            "provider": null, 
            "replace": "line", 
            "save": false, 
            "src": null, 
            "ssh_keyfile": null, 
            "timeout": null, 
            "username": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
        }
    }, 
    "msg": "crypto key generate rsa modulus 1024\r\n% You already have RSA keys defined named Switch.intranet.\r\n% They will be replaced.\r\n\r\n% The key modulus size is 1024 bits\r\n% Generating 1024 bit RSA keys, keys will be non-exportable...", 
    "rc": 1
}
...ignoring

TASK [ios_command] *********************************************************************************************************************************************************************************************************************************************
task path: /home/adminlocal/noiua-ansible/playbooks/test.yml:10
Using module file /opt/ansible_venv/local/lib/python2.7/site-packages/ansible/modules/network/ios/ios_command.py
<10.20.124.52> ESTABLISH LOCAL CONNECTION FOR USER: adminlocal
<10.20.124.52> EXEC /bin/sh -c 'echo ~ && sleep 0'
<10.20.124.52> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/adminlocal/.ansible/tmp/ansible-tmp-1492635907.15-228324234258419 `" && echo ansible-tmp-1492635907.15-228324234258419="` echo /home/adminlocal/.ansible/tmp/ansible-tmp-1492635907.15-228324234258419 `" ) && sleep 0'
<10.20.124.52> PUT /tmp/tmpHmsSfR TO /home/adminlocal/.ansible/tmp/ansible-tmp-1492635907.15-228324234258419/ios_command.py
<10.20.124.52> EXEC /bin/sh -c 'chmod u+x /home/adminlocal/.ansible/tmp/ansible-tmp-1492635907.15-228324234258419/ /home/adminlocal/.ansible/tmp/ansible-tmp-1492635907.15-228324234258419/ios_command.py && sleep 0'
<10.20.124.52> EXEC /bin/sh -c '/usr/bin/python /home/adminlocal/.ansible/tmp/ansible-tmp-1492635907.15-228324234258419/ios_command.py && sleep 0'
fatal: [c3650]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_args": {
            "auth_pass": null, 
            "authorize": null, 
            "commands": [
                "show switch"
            ], 
            "host": null, 
            "interval": 1, 
            "match": "all", 
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "port": null, 
            "provider": null, 
            "retries": 10, 
            "ssh_keyfile": null, 
            "timeout": null, 
            "username": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", 
            "wait_for": null
        }
    }, 
    "msg": "\r\n[OK] (elapsed time was 1 seconds)\r\n\r\nSwitch(config)#show switch\r\n                 ^\r\n% Invalid input detected at '^' marker.\r\n\r\nSwitch(config)#", 
    "rc": 1
}
        to retry, use: --limit @/home/adminlocal/noiua-ansible/playbooks/test.retry

PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************
c3650                      : ok=1    changed=0    unreachable=0    failed=1   


real    0m6.265s
user    0m2.120s
sys     0m0.368s
```



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/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
@@ -35,7 +35,7 @@ class TerminalModule(TerminalBase):
 
     terminal_stderr_re = [
         re.compile(r"% ?Error"),
-        re.compile(r"^% \w+", re.M),
+        #re.compile(r"^% \w+", re.M),
         re.compile(r"% ?Bad secret"),
         re.compile(r"invalid input", re.I),
         re.compile(r"(?:incomplete|ambiguous) command", re.I),


Test patch: