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"
]
}