ansibletelnetalpine-linuxgns3

Ansible: Telnet module hangs after connecting


I am using the telnet module – Executes a low-down and dirty telnet command.

This is my playbook:

---
- hosts: localhost
  connection: local
  gather_facts: no

  vars:

    telnet_link: "x.x.x.200"
    telnet_port: "5001"

  tasks:

    - name: Configure Static IP via Telnet
      ansible.netcommon.telnet:
        host: "{{ telnet_link }}"
        port: "{{ telnet_port }}"
        crlf: true
        send_newline: true
        prompts:
          - '[alpine:~#]'
        command:
          - echo "yes"

The Ansible playbook cannot resolve the command, and I think it is caused either by the carriage return or the alpine:~# prompt. See below:

me@my-vm:~/lab$ telnet x.x.x.200 5001
Trying x.x.x.200...
Connected to x.x.x.200.
Escape character is '^]'.
                           <- I press enter here
alpine:~#
alpine:~#
telnet> quit
Connection closed.

What I want is to give a static IP to my Alpine in GNS3 lab, so telnet link is more or less the only way.

I am open to alternatives, thank you a lot!


Solution

  • I found a solution.

    This Ansible module will check for login prompt regardless of the login_prompt option presence.

    I had to provide empty options to make it work, here is the playbook:

    ---
    - hosts: localhost
      connection: local
      gather_facts: false
    
      vars:
    
        telnet_link: "x.x.x.200"
        telnet_port: "5001"
    
      tasks:
    
        - name: Configure Static IP via Telnet
          ansible.netcommon.telnet:
            host: "{{ telnet_link }}"
            port: "{{ telnet_port }}"
            crlf: true
            send_newline: true
            login_prompt: ""
            user: ""
            password_prompt: ""
            password: ""
            prompts:
              - '[alpine:~#]'
            command:
              - echo "ok"
    

    Happy New Year !