Unable to run command via Ansible to Sonus and Acme/Oracle SBC

Hi All,

We have been unsuccessful in trying to run a basic “show version” or any show command on both Sonus and Acme/Oracle SBC via Ansible using the command or shell or raw module.
The version of the Sonus box(Sonus 5200 SBC) is V05.00.03R002. We were able to successfully SSH to the boxes but none of the commands ran after that.

It would be helpful if someone could help with this.

Thanks,
My3

Use the expect module.

Did you ever come right with this?
I tried executing off the expect module but cannot seem to get it right?

Would you have an example by chance?

If you show the output of the command when you run them manually and the expect you have tried, I can always help getting the expect up and running.

Fantastic.
So the box is running acme sbc.
Aim to achieve the following via AWX / Ansible:
***************# show platform cpu-load summary <------- the command to run
Total load: 11%
CPU#00 14%
CPU#01 8%

Load average: 2.86 3.00 3.00 2/281 7385

Playbook contents :

Fantastic.
So the box is running acme sbc.
Aim to achieve the following via AWX / Ansible:
***************# show platform cpu-load summary <------- the command to run
Total load: 11%
          CPU#00 14%
          CPU#01 8%

Load average: 2.86 3.00 3.00 2/281 7385

You have redacted the most important part the prompt.
Expect is petty easy, it looks for a string/text, when it sees that ecpect respond with a command and press enter.
This string must be unique or else expect will type a command at the wrong place.
But if you do not have # anywhere else you can use that.

Playbook contents :

---
- name: Check CPU Load
  hosts: all
  connection: local
  gather_facts: yes

  tasks:

  - name: Get CPU Load
    expect:
      command: show platform cpu-load summary

The command is the ssh command you need to login.

So you should a provided the whole output above, staring from a Linux prompt above.
Think of expect as a user that is typing the command manually. So need to start from prompt to now what is needed in responses bellow, because every keystroke you do expect has to do them also.

      responses:
        cpu

The responses part is where you have the question answer section, this is the string expect is waiting for and the command it should type.

So in your case we need to use the prompt/string # since you redacted the rest

   responses:
     '#': show platform cpu-load summary

So the # is the question/string/prompt and the part after colon is the command to run.
The quotes is needed if not # is taken as a comment.

If you would like to run more than one command per unique prompt they need to be in a list:

   responses:
     '#':
       - show platform cpu-load summary
       - show something else

You probably need to handle the password prompt in the responses too.

Thanks a mil!

Seem to be making progress, still received a failure though, looks like the python module is required on target, which does not appear to be possible now,
May need to look at an alternative

{
“_ansible_parsed”: true,
“exception”: " File "/tmp/ansible_e1OJFD/ansible_module_expect.py", line 95, in \n import pexpect\n",
“_ansible_no_log”: false,
“changed”: false,
“invocation”: {
“module_args”: {
“chdir”: null,
“responses”: {
“#”: [
“show platform cpu-load summary”
]
},
“removes”: null,
“echo”: false,
“creates”: null,
“command”: null,
“timeout”: 30
}
},
“msg”: “The pexpect python module is required”

}

Python expect module is required on localhost not remote target.

Perhaps the invocation is then off,
I have confirmed the modules existence on the local machine.
Re-installed and updated. is certainly there.
Error persists

The requirement
https://docs.ansible.com/ansible/latest/modules/expect_module.html#requirements

is pexpect >= 3.3

If that is what you have installed then it tends to be that you have more than one Python version installed, and the pyhton module is not installed in the one Ansible uses.

"ansible --version" can tall a little bit about the version of Python.

default Ansible run /usr/bin/python

You can check if the module exist and it's version with
/usr/bin/python -c 'import pexpect;print pexpect.__version__'

Awesome!
Issue was module location within the awx_task container!

{
“_ansible_parsed”: false,
“exception”: “Traceback (most recent call last):\n File "/tmp/ansible_iuEJo4/ansible_module_expect.py", line 231, in \n main()\n File "/tmp/ansible_iuEJo4/ansible_module_expect.py", line 152, in main\n if args.strip() == ‘’:\nAttributeError: ‘NoneType’ object has no attribute ‘strip’\n”,
“_ansible_no_log”: false,
“module_stderr”: “Traceback (most recent call last):\n File "/tmp/ansible_iuEJo4/ansible_module_expect.py", line 231, in \n main()\n File "/tmp/ansible_iuEJo4/ansible_module_expect.py", line 152, in main\n if args.strip() == ‘’:\nAttributeError: ‘NoneType’ object has no attribute ‘strip’\n”,
“changed”: false,
“module_stdout”: “”,
“rc”: 1,
“msg”: “MODULE FAILURE”

}

Error has changed to the above.
Ansible version 2.6, pexpect 4.6.0 and python 2.7
Suspecting this error is now related to my playbook though

Probably and without it I don't know how you expect someone to help.

Thank you for all your assistance thusfar Kai,
One last question,
I have gotten the remaining issues resolved however just stuck on this last hurdle:
PLAYBOOK::

responses consist of a question/prompt/string that expect is looking for, and when it sees that string it will issue the command/answers.
But your responses is missing the question/prompt/string part, you only have the command.

Without the output when you are doing this manually it's kind of hard to help, but you need something like

   responses:
     password prompt: ##########
     command prompt: show platform cpu-load summary

You can also write it like this

   responses:
     password prompt:
       - ##########
     command prompt:
       - show platform cpu-load summary

And "password prompt" and "command prompt" is the question/prompt/string expect should be looking for before it issue the command/answer.

command prompt is probably
  '#': show platform cpu-load summary

judging from your previous mails.

Kai,
If you were in South Africa I would be sending you a bottle of Whiskey right now!
Thank you so much for all your assistance.

I just need to add a time interval between the password and show cpu-load but other than that it is working beautifully.

Thanks again

Hello Kai

I am also facing same problem and in my case command prompt is ‘>’ . Where I need to hit ‘configure’ first and prompt changed to ‘%’ for further config commands.
It is same sonus box and I am switching from ‘root’ to ‘admin’ to execute configuration commands via ansible expect module.

Below is the playbook and output response. Ansible response is successful but nothing change on remote SBC machine.

If you can suggest, how execution is possible on different prompts.

Hello Kai

I am also facing same problem and in my case command prompt is '>' . Where

You should have create a new thread instead of resurrect a almost 2 year old thread that has been resolved.

I need to hit 'configure' first and prompt changed to '%' for further
config commands.
It is same sonus box and I am switching from 'root' to 'admin' to execute
configuration commands via ansible expect module.

Below is the playbook and output response. Ansible response is successful
but nothing change on remote SBC machine.

If you can suggest, how execution is possible on different prompts.
---
- name: expect
  expect:
     command: su - admin
     responses:
       command prompt:
          '$': /opt/sonus/sbx/Confd/sh/cliLogin.sh
          '>': source hello.txt
  register: ps

Expect is here looking for the string "command prompt" and not "$" or ">".
responses need to contain the sting expect in the key and what is should type in the value.

Expect uses regex and $ is a regex special character so that need to be escaped.

  - name: expect
    expect:
       command: su - admin
       responses:
         '\$': /opt/sonus/sbx/Confd/sh/cliLogin.sh
         '>': source hello.txt
    register: ps