How does Ansible deals with the remote prompt?

After a successful login, does Ansible automatically figures out what the remote prompt is before sending the command?

The remote system is Linux and supports ssh login as user root or cliadmin.

The difference is when logged in as cliadmin, a Java application is spawned to interact with the user.

When logged in as cliadmin the user is restricted set of commands such as “ps”.

Using Ansible I created a playbook that calls the raw module to run ps command and it hangs.

Note using Ansible as root works fine but that does not spawn that Java application.

Also manually ssh as cliadmin also works if Ansible is not involved.

In Ansible is there support for providing what the remote prompt is?

Is there a way to provide some delay before Ansible sends the command?

I wonder if my issue is with Ansible sends the command before the remote end is ready to accept the command.

Here is how it hangs as user cliadmin:

TASK [run ps command in restricted environment as user cliadmin] ********************************************************************************************************

task path: /Users/kwong/ansibles/playbooks.yml:6

<172.20.5.21> ESTABLISH SSH CONNECTION FOR USER: cliadmin

<172.20.5.21> SSH: EXEC sshpass -d42 ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o ‘User=“cliadmin”’ -o ConnectTimeout=10 -o ControlPath=/Users/kwong/.ansible/cp/0bea8f5a62 -tt 172.20.5.21 ps

  
After a successful login, does Ansible automatically figures out what the
remote prompt is before sending the command?

Ansible doesn't login it run commands in non-interactive fashion.

In Ansible is there support for providing what the remote prompt is?

Is there a way to provide some delay before Ansible sends the command?

I wonder if my issue is with Ansible sends the command before the remote
end is ready to accept the command.

Since Ansible is using ssh it doesn't do anything about this, since that is something ssh handles.

Here is how it hangs as user cliadmin:

TASK [run ps command in restricted environment as user cliadmin]
********************************************************************************************************

task path: /Users/kwong/ansibles/playbooks.yml:6

<172.20.5.21> ESTABLISH SSH CONNECTION FOR USER: cliadmin

<172.20.5.21> SSH: EXEC sshpass -d42 ssh -vvv -C -o ControlMaster=auto -o
ControlPersist=60s -o StrictHostKeyChecking=no -o 'User="cliadmin"' -o
ConnectTimeout=10 -o ControlPath=/Users/kwong/.ansible/cp/0bea8f5a62 -tt
172.20.5.21 ps

As you can see here ansible is basicly running

  ssh cliadmin@172.20.5.21 ps

So to find out what happens you need to research how ssh handles runing command non-interactive or use the expect module that can handles this.

Thanks Kai for your response.
Just for clarification. Can Ansible support the following scenario?

  1. Ansible ssh into a remote system using username/password.
  2. Ansible waits for a specific prompt from the remote side.
  3. Ansible than sends the command.
  4. Ansible wait for the command response and return the output to the caller.
    I have a simple Python script using the Paramiko library to perform the above operations.
    But I am hoping I can use Ansible directly for this to interact with number of remote servers.
    I looked at the Ansible expect module example and seems like it first sends the command and than look for some response and sends the corresponding response.
    I also saw the cli_command and that seems to have the interaction I needed but it seemed to be only for network devices.

The expect module is perfect for this.

Instead of letting Ansible do ssh you, you set the task to "delegate_to: localhost" and let expect do ssh as the command.

I am confused how Ansible Expect Module will work in my use case.

https://docs.ansible.com/ansible/latest/modules/expect_module.html

Is this the Expect Module you mentioned?

From what I can gather, the Expect Module

  1. Connects to remote device and immediately sends the command

  2. Look for a set of responses

  3. When a response is matched, it sends the corresponding response value

What I need is:

  1. Connect to remote device

  2. Look for the remote prompt

  3. Sends the command

  4. Return the command result to the caller

Can you please provive a short example of sending a “ps” command that runs on the remote device using Ansible Expect Module?

I am confused how Ansible Expect Module will work in my use case.

https://docs.ansible.com/ansible/latest/modules/expect_module.html

Is this the Expect Module you mentioned?

Yes

From what I can gather, the Expect Module

1. Connects to remote device and immediately sends the command

The point here is that the command is the ssh.

2. Look for a set of responses

3. When a response is matched, it sends the corresponding response value

What I need is:

1. Connect to remote device

2. Look for the remote prompt

3. Sends the command

4. Return the command result to the caller

Can you please provive a short example of sending a "ps" command that runs
on the remote device using Ansible Expect Module?

Pseudocode:

- expect:
     command: ssh user@host
     responses:
       remote prompt:
         - ps
         - exit
   delegate_to: localhost

Exit is the command that logs you out.

If you need more help that this you need to provide output of all the manual step including ssh to the end where you exit.

When using the ssh connection plugin, Ansible relies on the `sshpass`
program to handle prompts, but only does so if a password was already
supplied.

That is not the remote prompt, but the local prompt where the ssh client ask for password.
What the OP is after is the remote prompt, after ssh is authenticated.

ah, sorry, for that you need to use something like the expect module.