1

I would like to use the expect module in an ansible playbook to telnet through a termserver to reach a NetApp filer. I tried the telnet module, but using the port argument after the ip address doesn't seem to work. Manually I would type the following:

[sansible@scspr0415857001 ansible]$ telnet 10.233.82.7 2033  
Trying 10.233.82.7...  
Connected to 10.233.82.7.  
Escape character is '^]'.  
Connected to port 33.  

login: admin  
Password:  

rtpsb-f6280-02a-1::> 

~~~~~~~~~~~~~~~~~~~

I think my issue is that the playbook is getting caught up on simulating a return keystroke after "Connected to port 33."

I've also tried using echo -ne '\n'

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" 

The full traceback is:

  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  

Any help would be greatly appreciated!

3
  • The log indicates that it connected ok, to me it looks like the next pattern matching fails. Maybe try looking for the promt returned after the connection? Mar 9, 2018 at 15:51
  • How do you edit to get rid of the stack exchange formatting? Mar 13, 2018 at 20:05

1 Answer 1

1

I figured out the issue. The "Question:" parameter needs to be omitted:
---
- hosts: localhost
connection: local
gather_facts: false

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.