Juniper/ansible-junos-stdlib

Unable to make a PyEZ connection: ConnectError(host: x.x.x.x, msg: Unexpected session close

ntchilo opened this issue · 0 comments

Issue Type

  • Bug Report

Module Name

juniper.device collection and Python libraries version

ansible [core 2.17.3]
config file = /home/ansible/project/ansible.cfg
configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/ansible/.local/lib/python3.10/site-packages/ansible
ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
executable location = /home/ansible/.local/bin/ansible
python version = 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0] (/usr/bin/python3)
jinja version = 3.0.3
libyaml = True

/home/ansible/.ansible/collections/ansible_collections
Collection Version
juniper.device 1.0.5

ansible==10.3.0
ansible-core==2.17.3
attrs==21.2.0
Automat==20.2.0
Babel==2.8.0
bcrypt==3.2.0
blinker==1.4
certifi==2020.6.20
cffi==1.17.0
chardet==4.0.0
click==8.0.3
cloud-init==23.3.3
colorama==0.4.4
command-not-found==0.3
configobj==5.0.6
configparser==7.1.0
constantly==15.1.0
cryptography==3.4.8
dbus-python==1.2.18
distro==1.7.0
distro-info==1.1+ubuntu0.2
future==1.0.0
httplib2==0.20.2
hyperlink==21.0.0
icdiff==1.9.1
idna==3.3
importlib-metadata==4.6.4
incremental==21.3.0
jeepney==0.7.1
Jinja2==3.0.3
jsnapy==1.3.8
jsonpatch==1.32
jsonpointer==2.0
jsonschema==3.2.0
junos-eznc==2.7.1
jxmlease==1.0.3
keyring==23.5.0
launchpadlib==1.10.16
lazr.restfulclient==0.14.4
lazr.uri==1.0.6
lxml==5.3.0
MarkupSafe==2.0.1
more-itertools==8.10.0
ncclient==0.6.15
netifaces==0.11.0
oauthlib==3.2.0
packaging==24.1
paramiko==3.4.1
pexpect==4.8.0
ptyprocess==0.7.0
pyasn1==0.4.8
pyasn1-modules==0.2.1
pycparser==2.22
PyGObject==3.42.1
PyHamcrest==2.0.2
PyJWT==2.3.0
PyNaCl==1.5.0
pyOpenSSL==21.0.0
pyparsing==2.4.7
pyrsistent==0.18.1
pyserial==3.5
python-apt==2.4.0+ubuntu3
python-debian==0.1.43+ubuntu1.1
python-magic==0.4.24
pytz==2022.1
PyYAML==5.4.1
requests==2.25.1
resolvelib==1.0.1
scp==0.15.0
SecretStorage==3.3.1
service-identity==18.1.0
six==1.16.0
sos==4.5.6
ssh-import-id==5.11
systemd-python==234
transitions==0.9.2
Twisted==22.1.0
ubuntu-advantage-tools==8001
ubuntu-drivers-common==0.0.0
ufw==0.36.1
unattended-upgrades==0.1
urllib3==1.26.5
wadllib==1.3.6
xkit==0.0.0
xmltodict==0.13.0
yamlordereddictloader==0.4.2
zipp==1.0.0
zope.interface==5.4.0

OS / Environment

Model: ex4300-48t
Junos: 20.4R3-S3.4

Summary

I am trying to connect to a juniper device via console port. The console port is directly connected to a console server. The Connection Mode I want to use is documented in juniper.net as "SSH through a console server".

The console server that I have does not require any user/password to logon. That is, when I run the command "ssh x.x.x.x -p 1111", I will be returned Login and Password prompt of the device. Upon entering "admin" as login user and password, I will be connected to the device. Example:

ssh x.x.x.x -p 1111
Welcome to the MRV Communications' LX Series Console Server
Port 1 Speed 9600
X11 forwarding request failed on channel 0
▒M▒EXIT IMMEDIATELY

device_host_name (ttyu0)
login:

where device_host_name is the previously configured device host name and x.x.x.x is the console server ip address.

However, using ansible playbook and the juniper.device module does not work for me. I got cnx_err exception.

Steps to reproduce

Playbook
======
- name: Test console connection via teminal server

  hosts: eas_console
  collections:
    - juniper.device
  connection: local
  gather_facts: no

  tasks:
    - name: Execute show command
      command:
        host: "{{ ansible_ssh_host }}"
        user: "{{ juniper_user }}"
        passwd: "{{ juniper_passwd }}"
        port: "{{ ansible_port }}"
        commands:
         - "show version"
      register: response

    - name: Show response
      debug:
        var: response

Inventory file
======== 
[terminal_server]
eas_console ansible_ssh_host=x.x.x.x ansible_ssh_port=1111 juniper_user=admin juniper_passwd=valid_device_password_here

[all:vars]
ansible_python_interpreter=/usr/bin/python3


Note that I did not specify cs_user and cs_passwd parameters in the playbook since the console server doen not require logon credential.

Expected results

I am expecting the juniper.device module can connect to the device via console port and the playbook will then execute the "show version" task.

Actual results

Running ansible-playbook -vvv playbooks/connect_eas.yaml -i inventory/console

ansible-playbook [core 2.17.3]
  config file = /home/ansible/project/ansible.cfg
  configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/ansible/.local/lib/python3.10/site-packages/ansible
  ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/ansible/.local/bin/ansible-playbook
  python version = 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True
Using /home/ansible/project/ansible.cfg as config file
host_list declined parsing /home/ansible/project/inventory/console as it did not pass its verify_file() method
script declined parsing /home/ansible/project/inventory/console as it did not pass its verify_file() method
auto declined parsing /home/ansible/project/inventory/console as it did not pass its verify_file() method
Parsed /home/ansible/project/inventory/console inventory source with ini plugin
redirecting (type: callback) ansible.builtin.yaml to community.general.yaml
redirecting (type: callback) ansible.builtin.yaml to community.general.yaml
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: connect_eas.yaml **************************************************************************************************************************************************
1 plays in playbooks/connect_eas.yaml

PLAY [Test console connection via teminal server] ***************************************************************************************************************************
<x.x.x.x> ESTABLISH LOCAL CONNECTION FOR USER: ansible
<x.x.x.x> EXEC /bin/sh -c 'echo ~ansible && sleep 0'
<x.x.x.x> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ansible/.ansible/tmp `"&& mkdir "` echo /home/ansible/.ansible/tmp/ansible-tmp-1724615606.0557683-27636-126618754267988 `" && echo ansible-tmp-1724615606.0557683-27636-126618754267988="` echo /home/ansible/.ansible/tmp/ansible-tmp-1724615606.0557683-27636-126618754267988 `" ) && sleep 0'
Using module file /home/ansible/.ansible/collections/ansible_collections/juniper/device/plugins/modules/command.py
<x.x.x.x> PUT /home/ansible/.ansible/tmp/ansible-local-27633r6ac6ee1/tmpziz8jjz3 TO /home/ansible/.ansible/tmp/ansible-tmp-1724615606.0557683-27636-126618754267988/AnsiballZ_command.py
<x.x.x.x> EXEC /bin/sh -c 'chmod u+x /home/ansible/.ansible/tmp/ansible-tmp-1724615606.0557683-27636-126618754267988/ /home/ansible/.ansible/tmp/ansible-tmp-1724615606.0557683-27636-126618754267988/AnsiballZ_command.py && sleep 0'
<x.x.x.x> EXEC /bin/sh -c '/usr/bin/python3 /home/ansible/.ansible/tmp/ansible-tmp-1724615606.0557683-27636-126618754267988/AnsiballZ_command.py && sleep 0'
<x.x.x.x> EXEC /bin/sh -c 'rm -f -r /home/ansible/.ansible/tmp/ansible-tmp-1724615606.0557683-27636-126618754267988/ > /dev/null 2>&1 && sleep 0'

TASK [Execute show command] *************************************************************************************************************************************************
task path: /home/ansible/project/playbooks/connect_eas.yaml:11
The full traceback is:
  File "/tmp/ansible_command_payload_m8so3hwh/ansible_command_payload.zip/ansible_collections/juniper/device/plugins/module_utils/juniper_junos_common.py", line 1097, in open
    self.dev.open()
  File "/usr/local/lib/python3.10/dist-packages/jnpr/junos/device.py", line 1429, in open
    raise cnx_err
fatal: [eas_console]: FAILED! => changed=false
  invocation:
    module_args:
      attempts: null
      baud: null
      commands:
      - show version
      console: null
      cs_passwd: null
      cs_user: null
      dest: null
      dest_dir: null
      formats: null
      host: x.x.x.x
      huge_tree: false
      ignore_warning: null
      level: null
      logdir: null
      logfile: null
      mode: null
      passwd: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
      port: 1111
      return_output: true
      ssh_config: null
      ssh_private_key_file: null
      timeout: 20
      user: admin
  msg: 'Unable to make a PyEZ connection: ConnectError(host: x.x.x.x, msg: Unexpected session close)'

PLAY RECAP ******************************************************************************************************************************************************************
eas_console                : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0