2

I am trying to use the telnet command with ansible expect module to push initial configuration but it's not working, I am suspecting the issue is with the responses section, here is my playbook:

- hosts: wired
  gather_facts: false
  connection: local
  tasks:
  - name: telnet,login and execute command
    ignore_errors: no
    expect:
      command: telnet "{{ inventory_hostname }}" 2009
      responses:
        .*>: "enable"
        .*Password: "mainpass"
        .*#: "show runn | in hostname"
    register: telnet_output
  - name: Debug output
    debug: var=telnet_output.stdout_lines

When I try to telnet from the terminal, I get this:

Trying 10.1.1.1...
Connected to switch.lab.local.
Escape character is '^]'.

I need to press enter, then I am greeted with:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
To log in to this device, use the following password/credentials:

enable password mainpass   
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


switch>

then I have to enter the command enable, to be prompted with the enable password:

switch>enable
Password: 
switch#

I am not able to translate the above to the responses section in my playbook, when I run it, I see this:

fatal: [switch]: FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": true,
    "cmd": "telnet \"switch\" 2009",
    "delta": "0:00:00.312863",
    "end": "2022-11-08 15:01:27.707396",
    "invocation": {
        "module_args": {
            "chdir": null,
            "command": "telnet \"switch\" 2009",
            "creates": null,
            "echo": false,
            "removes": null,
            "responses": {
                ".*#": "show runn | in hostname",
                ".*>": "enable",
                ".*Password": "mainpass",
                "Trying 10.1.1.1....*": "\n",
                "telnet: Unable to connect to remote host: Connection refused": ""
            },
            "timeout": 30
        }
    },
    "msg": "non-zero return code",
    "rc": 1,
    "start": "2022-11-08 15:01:27.394533",
    "stdout": "Trying 10.1.1.1...\r\ntelnet: Unable to connect to remote host: Connection refused",
    "stdout_lines": [
        "Trying 10.1.1.1...",
        "telnet: Unable to connect to remote host: Connection refused"
    ]
}

1 Answers1

0

Since you are seeing Connection Refused messaged I don't think the Ansible connection is working as you would expect / hope. Ansible has built special modules to handle network devices, see here: https://docs.ansible.com/ansible/latest/network/getting_started/network_differences.html

I think your best bet is to read through those docs on how to set this up. Also, telnet is transferred over the wire as plain text so you really should upgrade to SSH or a HTTPS API if the device supports it.

burkee75
  • 76
  • 3