Ansible expect module with Oracle SBC

Hi.

I am having an issue with Ansible’s expect module.

I am trying to run simple commands on a Oracle SBC, but getting some errors.

Playbook:

  • name: Check CPU
    hosts: all
    connection: local
    gather_facts: yes

tasks:

  • name: Get CPU load
    expect:
    command: ssh admin@#########
    responses:
    'password: ':
  • #######
    ‘#’:
  • sh platform cpu-load

Error:

fatal: [######]: FAILED! => { “changed”: false, “invocation”: { “module_args”: { “chdir”: null, “command”: “ssh admin@######”, “creates”: null, “echo”: false, “removes”: null, “responses”: { “#”: [ “sh platform cpu-load” ], "password: ": [ “######” ] }, “timeout”: 30 } }, “msg”: “No remaining responses for ‘#’, output was ’ sh platform cpu-load\r\nTimestamp: 10:49:41 Wed 2020-02-05\r\nTotal load : 4%\r\nCPU 00 load : 5%\r\nCPU 01 load : 3%\r\nCPU 02 load : 5%\r\nCPU 03 load : 5%\r\nVOXBDLHA1#'” }

I #ed out the IP and password

Your assistance will be appreciated.

Hi.

Hi, if you had posted a plain text mail instead of HTML you would have gotten a
replay a lot sooner.
As you can see bellow you formatting is hard to read.

tasks:

- name: Get CPU load
expect:
command: ssh admin@#########
responses:
'password: ':
- #######
'#':
- sh platform cpu-load

Error:

fatal: [######]: FAILED! => { "changed": false, "invocation": {
"module_args": { "chdir": null, "command": "ssh admin@######", "creates":
null, "echo": false, "removes": null, "responses": { "#": [ "sh platform
cpu-load" ], "password: ": [ "######" ] }, "timeout": 30 } }, "msg": "No
remaining responses for '#', output was ' sh platform
cpu-load\r\nTimestamp: 10:49:41 Wed 2020-02-05\r\nTotal load : 4%\r\nCPU 00
load : 5%\r\nCPU 01 load : 3%\r\nCPU 02 load : 5%\r\nCPU 03 load :
5%\r\nVOXBDLHA1#'" }

As the msg says expect has gotten a prompt # put have no more command to give.
Remember that expect is you substitute for a human typing inn commands, so you
need to provide all command from start, ssh in you case, to the exit of the ssh command.

So you need to add an element to the item "#" in your responses to exit the command.

Hi Kai.

Thank you for taking the time to get back to me.

It is highly appreciated, and apologies for the formatting of my request, I will correct this in future.

I am in uncharted waters here as I am new to Ansible, and coding in general, but I have made some changes to the code:

  • name: Check CPU
    hosts: 10.18.170.200
    connection: local
    gather_facts: yes

tasks:

  • name: Get CPU load
    expect:
    command: ssh admin@10.18.170.200
    responses:
    (?i)password: “#######”

  • name: basic command
    expect:
    command: show platform cpu-load summary
    responses:
    (?i)#:

I am getting the below

fatal: [10.18.170.200]: FAILED! => {
“changed”: false,
“invocation”: {
“module_args”: {
“chdir”: null,
“command”: “show platform cpu-load”,
“creates”: null,
“echo”: false,
“removes”: null,
“responses”: {
“(?i)#”: null
},
“timeout”: 30
}
},
“msg”: “The command was not found or was not executable: show.”
}

You can't split it like that.
Two tasks in Ansible have nothing do to with each other.

The expect command will always be the ssh and in the response section it will
contain the command for the interface.

So it will be something like this.

  - name: Get CPU load
    expect:
      command: ssh admin@10.18.170.200
      responses:
        (?i)password: "#######"
        #:
    - show platform cpu-load summary
    - exit

When expect sees "password" it will type "#######" and hit enter.
When it sees "#" it will write the first element in the list(show...) of the
second time expect sees "#" it will write "exit" and hit enter.
(I presume exit terminate the connection, if not change it to the correct command)

Thank you Kai.

I think I am making progress, is this the output that is expected:

“* ",
"
Disconnect IMMEDIATELY if you are not an authorized user! ",
"
All access and use may be monitored and/or recorded. * “,
“* ",
"
*****************************************************************”,
“”,
“”,
“SBCHA1# show sessions”,
“11:17:24-107 Capacity=4000”,
“Session Statistics – Period – -------- Lifetime --------”,
" Active High Total Total PerMax High”,
“Total Sessions 1606 1661 3346 128530803 15238 2452”,
“SIP Sessions 1606 1661 3346 128530803 15238 2452”,
“H.323 Calls 0 0 0 0 0 0”,
“”,
“SIP Audio/Video Statistics – Period – -------- Lifetime --------”,
" Active High Total Total PerMax High”,
“Audio Calls 1541 1592 2582 90699117 8060 2096”,
“Video Calls 0 0 0 113 7 2”,
“Messaging Sessions 0 0 0 0 0 0”,
“SBCHA1# sh platform cpu-load”,
“Timestamp: 11:17:24 Thu 2020-02-13”,
“Total load : 9%”,
“CPU 00 load : 9%”,
“CPU 01 load : 9%”,
“CPU 02 load : 10%”,
“CPU 03 load : 9%”,
“SBCHA1# exit”,
"SBCHA1> "
]

As a note, the SBC does have a ASCII banner, not sure if it may cause issues.

Thank you Kai.

I think I am making progress, is this the output that is expected:

        "*
*",
        "* Disconnect IMMEDIATELY if you are not an authorized user!
*",
        "* All access and use may be monitored and/or recorded. *
",
        "*
*",
        
"******************************************************************",
        "",
        "",
        "SBCHA1# show sessions",
        "11:17:24-107 Capacity=4000",
        "Session Statistics -- Period -- -------- Lifetime
--------",
        " Active High Total Total PerMax
High",
        "Total Sessions 1606 1661 3346 128530803 15238
2452",
        "SIP Sessions 1606 1661 3346 128530803 15238
2452",
        "H.323 Calls 0 0 0 0 0
0",
        "",
        "SIP Audio/Video Statistics -- Period -- -------- Lifetime
--------",
        " Active High Total Total PerMax
High",
        "Audio Calls 1541 1592 2582 90699117 8060
2096",
        "Video Calls 0 0 0 113 7
2",
        "Messaging Sessions 0 0 0 0 0
0",
        "SBCHA1# sh platform cpu-load",
        "Timestamp: 11:17:24 Thu 2020-02-13",
        "Total load : 9%",
        "CPU 00 load : 9%",
        "CPU 01 load : 9%",
        "CPU 02 load : 10%",
        "CPU 03 load : 9%",
        "SBCHA1# exit",
        "SBCHA1> "
    ]

It is as expected.
Expect doesn't do any magic it only looks for the key in the output and types
the response an hit enter and shows all the output.

As a note, the SBC does have a ASCII banner, not sure if it may cause
issues.

If the banner contain the same string as in our responses it will. That why
it's important that the responses are unique.

HI Kai.

When running this command via Ansible I am getting the following:

The full traceback is:
File “/tmp/ansible_expect_payload__etk4d92/ansible_expect_payload.zip/ansible/modules/commands/expect.py”, line 123, in wrapped

fatal: [######]: FAILED! => {
“changed”: false,
“invocation”: {
“module_args”: {
“chdir”: null,
“command”: “ssh admin@#########”,
“creates”: null,
“echo”: false,
“removes”: null,
“responses”: {
“#”: [
“show xcode load -detail”,
“exit”
],
“(?i)password”: “#######”
},
“timeout”: 30
}
},
“msg”: “No remaining responses for ‘#’, output was ‘Sess Current Maximum\r\n === === ===== ======= =======\r\n 00 0 14 3.33% 29.28%\r\n 00 1 17 2.97% 29.02%\r\n 01 0 - - -\r\n 01 1 - - -\r\n 02 0 - - -\r\n 02 1 - - -\r\n 03 0 - - -\r\n 03 1 - - -\r\n 04 0 - - -\r\n 04 1 - - -\r\n 05 0 - - -\r\n 05 1 - - -\r\n 06 0 - - -\r\n 06 1 - - -\r\n 07 0 - - -\r\n 07 1 - - -\r\n 08 0 - - -\r\n 08 1 - - -\r\n 09 0 - - -\r\n 09 1 - - -\r\n 10 0 - - -\r\n 10 1 - - -\r\n 11 0 - - -\r\n 11 1 - - -\r\nSBCHA1#’”
}

The output of the command is as below:

HA1# show xcode load -detail
12:37:54
Total Sessions: 334
----- Load -----
TCM DSP #Sess Current Maximum
=== === ===== ======= =======
00 0 85 18.78% 32.74%
00 1 82 18.80% 32.80%
01 0 82 18.58% 32.70%
01 1 85 18.56% 32.68%
02 0 - - -
02 1 - - -
03 0 - - -
03 1 - - -
04 0 - - -
04 1 - - -
05 0 - - -
05 1 - - -
06 0 - - -
06 1 - - -
07 0 - - -
07 1 - - -
08 0 - - -
08 1 - - -
09 0 - - -
09 1 - - -
10 0 - - -
10 1 - - -
11 0 - - -
11 1 - - -

I am guessing it is the - character that may be causing an issue?

HI Kai.

When running this command via Ansible I am getting the following:

fatal: [######]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "chdir": null,
            "command": "ssh admin@#########",
            "creates": null,
            "echo": false,
            "removes": null,
            "responses": {
                "#": [
                    "show xcode load -detail",
                    "exit"
                ],
                "(?i)password": "#######"
            },
            "timeout": 30
        }
    },
    "msg": "No remaining responses for '#', output was 'Sess Current

As the message says, expect still get the # prompt but doesn't have any more
responses to type in.

Maximum\r\n === === ===== ======= =======\r\n 00 0 14
3.33% 29.28%\r\n 00 1 17 2.97% 29.02%\r\n 01 0 -
     - -\r\n 01 1 - - -\r\n 02 0 -
- -\r\n 02 1 - - -\r\n 03 0 - -
      -\r\n 03 1 - - -\r\n 04 0 - -
  -\r\n 04 1 - - -\r\n 05 0 - -
-\r\n 05 1 - - -\r\n 06 0 - -
-\r\n 06 1 - - -\r\n 07 0 - -
-\r\n 07 1 - - -\r\n 08 0 - -
-\r\n 08 1 - - -\r\n 09 0 - -
-\r\n 09 1 - - -\r\n 10 0 - -
-\r\n 10 1 - - -\r\n 11 0 - -
-\r\n 11 1 - - -\r\nSBCHA1#'"

The last part "\r\nSBCHA1#" show that expect get the prompt again.

The output of the command is as below:

HA1# show xcode load -detail
12:37:54
Total Sessions: 334
              ----- Load -----
  TCM DSP #Sess Current Maximum

In the output you have a #. Since you have said to expect that "#" is the sting
to trigger a response it will type exit<ENTER> here, but since this is not the
prompt this causes issues.

The text expect should trigger on in responses has to be unique, an in you case
# is not. So you need to add more characters to it.
If the prompt always is "BCHA1#" you can use that instead of only "#"

Hi Kai.

Trust you are well, we are testing adding new configs via Ansible on the SBC, just struggling, to handle the various prompt changes, as you navigate the different configuration levels, and I have tweaked the playbook, but not coming right.

Here is the below output.

The full traceback is:
File “/tmp/ansible_expect_payload_zp_yu2yh/ansible_expect_payload.zip/ansible/modules/commands/expect.py”, line 123, in wrapped
fatal: [###########]: FAILED! => {
“changed”: false,
“invocation”: {
“module_args”: {
“chdir”: null,
“command”: “ssh admin@##########”,
“creates”: null,
“echo”: false,
“removes”: null,
“responses”: {
“(?i)password”: “########”,
“(configure)#”: [
“media-manager realm-config”
],
“(realm-config)#”: [
“identifier TEST_REALM_2”,
“addr-prefix 10.1.0.0/29”,
“network-interfaces PEER_F00:0”,
“mm-in-realm disabled”,
“mm-in-network enabled”,
“mm-same-ip disabled”,
“mm-in-system enabled”,
“access-control-trust-level medium”,
“invalid-signal-threshold 1’”,
“maximum-signal-threshold 4000”,
“untrusted-signal-threshold 0”,
“options sip-connect-pbx-reg”,
“qos-enable enabled”,
“accounting-enable enabled”,
“done”,
“quit”
],
“HA1#”: [
“save-config”,
“verify-confog”,
“activate-config”
]
},
“timeout”: 30
}
},
“msg”: “No remaining responses for ‘HA1#’, output was ’ activate-config\r\nActivate-Config received, processing.\r\nwaiting for request to finish\r\nRequest to ‘ACTIVATE-CONFIG’ has Finished, \r\nActivate Complete\r\nLABHA1#'”
}
…ignoring

Here is my playbook:

  • name: Add config centrex child realm
    hosts: 10.11.171.20
    connection: local
    gather_facts: yes

tasks:

  • name: Add config
    expect:
    command: ssh admin@##########
    responses:
    (?i)password: “##########”
    ‘HA1#’:

  • conf t
    ‘(configure)#’:

  • media-manager realm-config
    ‘(realm-config)#’:

  • identifier TEST_REALM_2

  • addr-prefix 10.1.0.0/29

  • network-interfaces PEER_F00:0

  • mm-in-realm disabled

  • mm-in-network enabled

  • mm-same-ip disabled

  • mm-in-system enabled

  • access-control-trust-level medium

  • invalid-signal-threshold 1’

  • maximum-signal-threshold 4000

  • untrusted-signal-threshold 0

  • options sip-connect-pbx-reg

  • qos-enable enabled

  • accounting-enable enabled

  • done

  • quit
    ‘HA1#’:

  • save-config

  • verify-confog

  • activate-config
    ignore_errors: true
    register: labsbc

  • debug: var=labsbc.stdout_lines