Expect Module Help

Hello everyone,

I’m messing around with expect and I can’t seem to get a match to get the ping command to fire again a second time. I’m assuming that I’m making a very common matching mistake that is alluding me. Any help would be greatly appreciated!

Expect is used on command and script that is interactive, ping is not interactive.

Hey Kai,

Thanks for the reply. Is the same thing happening with expect playbook that I made to try to telnet?

Playbook:

---
- hosts: localhost
  connection: local
  gather_facts: false

  tasks:  
    - name: Expect Attempt  
      expect:  
        echo: yes  
        command: telnet 10.233.82.7 2033  
        responses:  
          Question:  
            - Connected to port* "echo"  
            - login: "admin"  
            - Password: "password" 
File "/tmp/ansible_piMb3N/ansible_module_expect.py", line 190, in main  
    encoding='utf-8')  

fatal: [localhost]: FAILED! => {  
    "changed": true,  
    "cmd": "telnet 10.233.82.7 2033",  
    "delta": "0:00:30.122472",  
    "end": "2018-03-08 13:08:12.487220",  
    "invocation": {  
        "module_args": {  
            "chdir": null,  
            "command": "telnet 10.233.82.7 2033",  
            "creates": null,  
            "echo": true,  
            "removes": null,  
            "responses": {  
                "Question": [  
                    "Connected to port* \"echo\"",  
                    {  
                        "login": "admin"  
                    },  
                    {  
                        "Password": "password"  
                    }  
                ]  
            },  
            "timeout": 30  
        }  
    },  
    "msg": "non-zero return code",  
    "rc": 1,  
    "start": "2018-03-08 13:07:42.364748",  
    "stdout": "Trying 10.233.82.7...\r\r\nConnected to 10.233.82.7.\r\r\nEscape   character is '^]'.\r\r\nConnected to port 33. ",  
    "stdout_lines": [  
        "Trying 10.233.82.7...",  
        "",  
        "Connected to 10.233.82.7.",  
        "",  
        "Escape character is '^]'.",  
        "",  
        "Connected to port 33. "  
    ]  
}  
        to retry, use: --limit @/home/sansible/ansible/expect-r1.retry  

PLAY RECAP   *********************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1  

Hey Kai,

Thanks for the reply. Is the same thing happening with expect playbook that
I made to try to telnet?

No, telnet is interactive, the problem you have now is wrong syntax.

---
- hosts: localhost
  connection: local
  gather_facts: false

  tasks:
    - name: Expect Attempt
      expect:
        echo: yes
        command: telnet 10.233.82.7 2033
        responses:
          Question:
            - Connected to port* "echo"
            - login: "admin"
            - Password: "password"

Question is the text/prompt/question you are checking for, it's not a directive for the expect module.

    - name: Expect Attempt
      expect:
        echo: yes
        command: telnet 10.233.82.7 2033
        responses:
          login: "admin"
          Password: "password"

When expect sees login in the output it will type admin and press ENTER and type password when it sees Password or password since the expect module is case insensitive by default.
I don't understand what you tried to do with 'Connected to port* "echo"'

You should also check out this tread for just a few days ago that address the same problem you are having now.
https://groups.google.com/forum/#!topic/ansible-project/Cq1NhB98vQA

Kai,

It seems that editing out the “question:” helped.

I still have to add [Connected to port 33. : “echo”] to my playbook though. If I omit that line, the playbook will time out.

[sansible@scspr0415857001 ansible]$ cat expect-r6.yaml