Use telnet connection to remote host

I am new to ansible and I am trying to estrablish a telnet connection from a macine to a GNS3 switch (does not support ssh) that is running in a different machine on port 5003 using a ansible playbook. When i type the corresponding telnet command in the terminal it works as expected but when I try to use a playbook using the
ansible.netcommon.telnet module it doesn’t work. I wanted to run commands and save the output from each one.

My hosts file is very simple and looks like this (in the future would contain more devices):

[switch]
sw1 ansible_hosts=192.168.56.176 ansible_port=5003

The project configuration is:

[defaults]
inventory=/home/ar/Diss_Network/ansible/tests/hosts
host_key_checking=False

[inventory]
enable_plugins=host_list, script, auto, yaml, ini, toml

and the ansible playbook (switch doesn’t have neither username nor password ):

---
- hosts: sw1
  tasks:
  - name: run show command
    ansible.netcommon.telnet:
      prompts:
      - '[>#]'
      command:
      - show version

This playbook returns:

fatal: [sw1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: kex_exchange_identification: banner line contains invalid characters\r\nbanner exchange: Connection to 192.168.56.176 port 5003: invalid format", "unreachable": true}

I’m having trouble understanding the error because telnet typically doesn’t involve key management. Can telnet effectively establish communication with remote hosts? Is there another ways to establish a telnet connection?

Add gather_facts: false, so ansible doesn’t try to ssh and gather facts before running the tasks. The telnet module isn’t a connection plugin either, so it isn’t going to respect your ansible_port variable if that’s the intended telnet port. It does appear to use the remote_user/addr though. You might have to try setting user to null or an empty string "".

- hosts: sw1
  gather_facts: false
  tasks:
  - name: run show command
    ansible.netcommon.telnet:
      port: "{{ ansible_port | default(omit) }}"
      prompts:
      - '[>#]'
      command:
      - show version
      #user: null 
1 Like

Maybe this should be documented, I don’t think it already is :wink:

I mean, it’s listed as a module, not a connection type, so that part is sort of documented.

Also, in the synopsis:

  • Executes a low-down and dirty telnet command, not going through the module subsystem.
  • This is mostly to be used for enabling ssh on devices that only have telnet enabled by default.
1 Like

Your modifications solved the error, but the playbook execution hangs. I tried to create a netcat server in the destination host with

nc -l 5050

and I successfully tested connecting to it using a telnet command, establishing bidirectional communication. However, when I ran the Ansible playbook, no action occurred. I attempted to utilize the “send_newline: true” parameter, which sent a “\n” to the netcat server but nothing happened after that.

Unfortunately, I have no experience with this module, and barely any experience with network automation via Ansible. I can only help with the Ansible-centric issues here.

If I were in your shoes, I would try increasing verbosity to max to see if anything stands out in what Ansible is doing. I don’t know anything about your netcat server, so Idk what “prompt” should be expected, but I don’t think Ansible will send any commands until it sees the prompt and that might be why it’s hanging.

1 Like