Repository: ansible/ansible

Issue:
sros_config commands get stuck if prompt changes
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
Module sros_config

##### ANSIBLE VERSION
```
ansible 2.2.1.0
```

##### CONFIGURATION

##### OS / ENVIRONMENT
CentOS-7

##### SUMMARY
When running SROS command that changes the prompt from "#" to "$", sros_config module hangs as it doesn't expect that prompt.
```
*A:cont1-dev2# configure router bgp group "vsc" neighbor 10.0.0.2
*A:cont1-dev2>config>router>bgp>group>neighbor$
```

I got it fixed by modifying CLI_PROMPTS_RE in /usr/lib/python2.7/site-packages/ansible/module_utils/sros.py from this original value:
```
    CLI_PROMPTS_RE = [
        re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
        re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
    ]

```

into this (only the first regexp changed):
```
    CLI_PROMPTS_RE = [
        re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#|\$) ?$"),
        re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
    ]

```

Then delete precompiled file sros.pyc to make the new code active.

##### STEPS TO REPRODUCE
Run command 

> configure router bgp group "vsc" neighbor 10.0.0.2

 in SROS device using sros_config module.

##### EXPECTED RESULTS
Also commands that change prompt as "$" should be possible to run.

##### ACTUAL RESULTS
Command sending goes to timeout because sros_config is not expecting $ in prompt.
```
TASK [vsc-conf : BGP configuration part2] **************************************
Tuesday 02 May 2017  10:25:11 +0300 (0:00:03.646)       0:00:26.170 ***********
fatal: [cont1-dev2 -> localhost]: FAILED! => {"changed": false, "commands": ["configure router bgp group \"vsc\" family vpn-ipv4 evpn", "configure router bgp group \"vsc\" min-route-advertisement 1", "configure router bgp group \"vsc\" type internal", "configure router bgp group \"vsc\" local-address 10.2.190.41", "configure router bgp group \"vsc\" neighbor 10.0.0.2"], "failed": true, "msg": "timeout trying to send command: configure router bgp group \"vsc\" neighbor 10.0.0.2\r"}
```


Discussion:
cc @privateip @gundalow @Qalthos
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@privateip do you recall fixing this for 2.3?
Unfortunately it seems that sros_config module is totally broken in Ansible 2.3. The very basic functionality of feeding in tab-aligned input files causes failures as Ansible is running commands on wrong levels of CLI.

This is the error:
```
            "src": "exit all\nconfigure\n#--------------------------------------------------\necho \"System Configuration\"\n#--------------------------------------------------\n    system\n        name \"cont2-dev2\"\n        snmp\n        exit\n        time\n            ntp\n                ntp-server authenticate\n                authentication-key 1 key \"xxxxxx" hash2 type message-digest\n                server \"xx.xx.xx.xx\" prefer\n                server \"yy.yy.yy.yy\"\n                server \"zz.zz.zz.zz\"\n                no shutdown\n\n            sntp\n                shutdown\n\n            dst-zone EEST\n                start last sunday march 03:00\n                end last sunday october 04:00\n\n            zone EET\n\n        thresholds\n            rmon\n",
            "ssh_keyfile": null,
            "timeout": null,
            "username": null
        }
    },
    "msg": "ntp-server authenticate\r\n                                  ^\r\nError: Bad command. \r\n*A:cont2-dev2>config>system>time# ",
    "rc": 1
}
```

When I'm running this input:
```
exit all
configure
#--------------------------------------------------
echo "System Configuration"
#--------------------------------------------------
    system
        name "{{ inventory_hostname }}"
        snmp
        exit
        time
            ntp
                ntp-server authenticate
                authentication-key 1 key "{{ vsc_config.vsc_ntp_auth_key }}" hash2 type message-digest
                server "{{ global_config.ntp_server_list_ip[0] }}" prefer
                server "{{ global_config.ntp_server_list_ip[1] }}"
                server "{{ global_config.ntp_server_list_ip[2] }}"
                no shutdown

            sntp
                shutdown

            dst-zone EEST
                start last sunday march 03:00
                end last sunday october 04:00

            zone EET

        thresholds
            rmon
```

The problem is that sros_config of Ansible 2.3.0.0 does not run "ntp-server authenticate" under "ntp" but tries to run it on higher level.

The same code and config works ok with Ansible 2.2.1.0
After side-tracking to above mentioned more severe issue in Ansible 2.3 (I reported https://github.com/ansible/ansible/issues/24874 about that), here's answer to @calfonso asking whether the prompt handling has been fixed in 2.3. The answer is that it is **not** fixed, exactly the same prompt configuration exists in 2.3 as in 2.2, but due to refactoring the configuration is found in different file: /usr/lib/python2.7/site-packages/ansible/plugins/terminal/sros.py


Resolving patch:
diff --git a/lib/ansible/plugins/terminal/sros.py b/lib/ansible/plugins/terminal/sros.py
--- a/lib/ansible/plugins/terminal/sros.py
+++ b/lib/ansible/plugins/terminal/sros.py
@@ -28,7 +28,7 @@
 class TerminalModule(TerminalBase):
 
     terminal_stdout_re = [
-        re.compile(br"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
+        re.compile(br"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#|\$) ?$"),
         re.compile(br"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
     ]
 


Test patch: