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

1,397 views
Skip to first unread message

Maithreyi Narasimha

unread,
Sep 11, 2017, 5:23:01 AM9/11/17
to Ansible Project
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

Kai Stian Olstad

unread,
Sep 14, 2017, 9:59:52 AM9/14/17
to ansible...@googlegroups.com
Use the expect module.


--
Kai Stian Olstad

julian.mit...@gmail.com

unread,
Aug 29, 2018, 5:41:48 AM8/29/18
to Ansible Project
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?

Kai Stian Olstad

unread,
Aug 29, 2018, 5:49:18 AM8/29/18
to ansible...@googlegroups.com
On 29.08.2018 11:41, julian.mit...@gmail.com wrote:
> I tried executing off the expect module but cannot seem to get it
> right?

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.

--
Kai Stian Olstad

julian.mit...@gmail.com

unread,
Aug 29, 2018, 6:21:52 AM8/29/18
to Ansible Project
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 :

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


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


Json returned:

{
    "_ansible_parsed": true,
    "exception": "  File \"/tmp/ansible_nV8SnW/ansible_modlib.zip/ansible/module_utils/basic.py\", line 2056, in _check_argument_types\n    param[k] = type_checker(value)\n  File \"/tmp/ansible_nV8SnW/ansible_modlib.zip/ansible/module_utils/basic.py\", line 1907, in _check_type_dict\n    raise TypeError(\"dictionary requested, could not parse JSON or key=value\")\n",
    "_ansible_no_log": false,
    "changed": false,
    "invocation": {
        "module_args": {
            "command": "show platform cpu-load summary",
            "responses": "cpu",
            "timeout": 30,
            "echo": false
        }
    },
    "msg": "argument responses is of type <type 'str'> and we were unable to convert to dict: dictionary requested, could not parse JSON or key=value"
}

############################

To be honest I am unsure if A - I am using the expect module correctly or B - Is the command module even the right thing here?
These are non standard / custom devices. No shell to execute commands on, raw,shell and command fail to execute the "show platform cpu-load summary"

Kai Stian Olstad

unread,
Aug 29, 2018, 6:47:13 AM8/29/18
to ansible...@googlegroups.com
On 29.08.2018 12:21, julian.mit...@gmail.com wrote:
> 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.

--
Kai Stian Olstad

julian.mit...@gmail.com

unread,
Aug 29, 2018, 6:58:14 AM8/29/18
to Ansible Project
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 <module>\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"

Kai Stian Olstad

unread,
Aug 29, 2018, 7:02:03 AM8/29/18
to ansible...@googlegroups.com
On 29.08.2018 12:58, julian.mit...@gmail.com wrote:
> 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,

Python expect module is required on localhost not remote target.

--
Kai Stian Olstad

julian.mit...@gmail.com

unread,
Aug 29, 2018, 7:35:22 AM8/29/18
to Ansible Project
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

Kai Stian Olstad

unread,
Aug 29, 2018, 8:04:13 AM8/29/18
to ansible...@googlegroups.com
On 29.08.2018 13:35, julian.mit...@gmail.com wrote:
> 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__'

--
Kai Stian Olstad

julian.mit...@gmail.com

unread,
Aug 29, 2018, 8:22:31 AM8/29/18
to Ansible Project
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 <module>\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 <module>\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

Kai Stian Olstad

unread,
Aug 29, 2018, 9:20:31 AM8/29/18
to ansible...@googlegroups.com
On 29.08.2018 14:22, julian.mit...@gmail.com wrote:
> {
> "_ansible_parsed": false,
> "exception": "Traceback (most recent call last):\n File
> \"/tmp/ansible_iuEJo4/ansible_module_expect.py\", line 231, in
> <module>\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
> <module>\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.

--
Kai Stian Olstad

julian.mit...@gmail.com

unread,
Aug 30, 2018, 4:05:55 AM8/30/18
to Ansible Project
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::


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


  tasks:

  - name: Get CPU Load
    expect:
      command: ssh admin@########
      responses:
         - '##########'
         - 'show platform cpu-load summary'

Hashes are just the IP and password

JSON returned::

{
    "_ansible_parsed": true,
    "exception": "  File \"/tmp/ansible_gRkOAC/ansible_modlib.zip/ansible/module_utils/basic.py\", line 2111, in _check_argument_types\n    param[k] = type_checker(value)\n  File \"/tmp/ansible_gRkOAC/ansible_modlib.zip/ansible/module_utils/basic.py\", line 1958, in _check_type_dict\n    raise TypeError('%s cannot be converted to a dict' % type(value))\n",

    "_ansible_no_log": false,
    "changed": false,
    "invocation": {
        "module_args": {
            "command": "ssh admin@########",
            "responses": [
                "########",

                "show platform cpu-load summary"
            ],
            "timeout": 30,
            "echo": false
        }
    },
    "msg": "argument responses is of type <type 'list'> and we were unable to convert to dict: <type 'list'> cannot be converted to a dict"

Kai Stian Olstad

unread,
Aug 30, 2018, 5:55:19 AM8/30/18
to ansible...@googlegroups.com
On Thursday, 30 August 2018 10.05.54 CEST julian.mit...@gmail.com wrote:
> 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::
>
> ---
> - name: Check CPU Load
> hosts: all
> connection: local
> gather_facts: yes
>
>
> tasks:
>
> - name: Get CPU Load
> expect:
> command: ssh admin@########
> responses:
> - '##########'
> - 'show platform cpu-load summary'

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 Stian Olstad


julian.mit...@gmail.com

unread,
Aug 30, 2018, 6:18:03 AM8/30/18
to Ansible Project
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

Ravi Virmani

unread,
May 14, 2020, 8:43:28 AM5/14/20
to Ansible Project
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.
---
- name: expect
  expect:
     command: su - admin
     responses:
       command prompt:
          '$': /opt/sonus/sbx/Confd/sh/cliLogin.sh
          '>': source hello.txt
  register: ps

- debug: msg={{ ps.stdout }}




changed: [10.10.11.102] => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "cmd": "su - admin",
    "delta": "0:00:30.323888",
    "end": "2020-05-14 04:51:24.011019",
    "invocation": {
        "module_args": {
            "chdir": null,
            "command": "su - admin",
            "creates": null,
            "echo": false,
            "removes": null,
            "responses": {
                "command prompt": {
                    "$": "/opt/sonus/sbx/Confd/sh/cliLogin.sh",
                    ">": "source hello.txt"
                }
            },
            "timeout": 30
        }
    },
    "rc": 0,
    "start": "2020-05-14 04:50:53.687131",
    "stdout": "$ ",
    "stdout_lines": [
        "$ "
    ]
}

TASK [sbc_config : debug] **********************************************************************************************************************************************
task path: /var/ansible/ansible_course/SBC/roles/sbc_config/tasks/main.yaml:20
ok: [10.10.11.102] => {
    "msg": "$ "
}
META: ran handlers
META: ran handlers

PLAY RECAP *************************************************************************************************************************************************************
10.10.11.102               : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0




On Thursday, 30 August 2018 15:25:19 UTC+5:30, Kai Stian Olstad wrote:

Kai Stian Olstad

unread,
Jun 14, 2020, 5:27:45 AM6/14/20
to ansible...@googlegroups.com
On Thu, May 14, 2020 at 05:43:28AM -0700, Ravi Virmani wrote:
> 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

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages