Troubleshoot Ansible Connection Errors When Managing Junos Devices
The following sections outline connection errors that you might encounter when using Ansible to manage Junos devices. These sections also present potential causes and solutions for each error.
Troubleshoot Failed Connection, Unknown Command, or Interpreter Not Found Errors
Problem
Description
When you execute a juniper.device module or a
junipernetworks.junos module from the
juniper.device collection, the Ansible control node
generates an error about a failed connection, an unknown command, or not
being able to locate the Python interpreter. For example:
UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ", "unreachable": true}
or
unknown command: /bin/sh\r\n
or
[ERROR]: Task failed: Action failed: The module interpreter '/home/user/projects/Ansible/.venv/bin/python' was not found.
Cause
These errors can arise when the Ansible control node does not run the module locally.
Normally Ansible requires Python on the managed node. The Ansible control node
sends the module to the node, where it is executed and then removed. The
juniper.device modules do not require Python on the managed
device because the modules use the Junos XML API and NETCONF to interface with
the device. Therefore, to perform operations on Junos devices, you must run the
modules locally on the Ansible control node where Python is installed. If
Ansible tries to execute a module directly on the Junos device, it generates an
error.
Solution
To direct the Ansible control node to run the modules locally, you must define the appropriate connection parameters for your module set. You can define the parameters in different locations, for example, in your inventory file, in your host or group variable files, in your playbook, or as command-line arguments. The connection type varies depending on the module set you choose, and in some cases, the individual module. For more information, see:
Troubleshoot Unknown Host Errors
Problem
Description
When you execute a juniper.device module or a
junipernetworks.junos module from the
juniper.device collection, the Ansible control node
generates an error about an unknown host, host pattern, or address.
"msg": "Unable to make a PyEZ connection: ConnectUnknownHostError(dc1a.example.net)"
or
[WARNING]: Could not match supplied host pattern, ignoring: name
or
"msg": "[Errno -5] No address associated with hostname"
Cause
These errors occur when the Ansible inventory file does not define the host or the Ansible control node is unable to resolve the hostname.
When you execute an Ansible module either directly or from a playbook, any host referenced in the module arguments or the playbook must be defined in the Ansible inventory file. The default location for the inventory file is /etc/ansible/hosts. The Ansible control node must be able to resolve the hostnames of any hosts that you define in the inventory file.
Solution
Update the Ansible inventory file to include the missing host, and ensure that DNS resolution is working correctly.
For information about the Ansible inventory file, see Understanding the Ansible Inventory File When Managing Junos Devices as well as the official Ansible documentation at https://www.ansible.com/.
Troubleshoot Refused Connection Errors and Socket Errors
Problem
Description
When you execute a juniper.device module or a
junipernetworks.junos module from the
juniper.device collection, the Ansible control node
generates a ConnectRefusedError error or a socket error.
For example:
"msg": "Unable to make a PyEZ connection: ConnectRefusedError(dc1a.example.net)"
or
"msg": "Could not open socket to 198.51.100.101:830"
Cause
The most likely cause for these errors is that NETCONF over SSH is not enabled on the Junos device.
To quickly test whether NETCONF is enabled, verify that the user account executing the Ansible module can successfully start a NETCONF session with the device.
user@ansible-cn:~$ ssh user@dc1a.example.net -p 830 -s netconf
If the user can successfully establish a NETCONF session with the device on either the default NETCONF port (830) or a port that is specifically configured for NETCONF on your device, then NETCONF is enabled. Otherwise, you must enable NETCONF over SSH on the device.
Solution
Enable the NETCONF-over-SSH service on the Junos device.
[edit] user@host# set system services netconf ssh user@host# commit
Troubleshoot Host Network OS Errors
Problem
Description
When you execute a junipernetworks.junos module from the
juniper.device collection
(juniper.device.junos_* module), the Ansible control
node generates a host network os error. For example:
[ERROR]: Task failed: Unable to automatically determine host network os. Please manually configure ansible_network_os value for this host
Cause
The juniper.device.junos_* modules require that you specify the
Ansible network OS as juniper.device.junos for target hosts
running Junos OS or target hosts running Junos OS Evolved.
Solution
Define the ansible_network_os variable in the location
appropriate for your environment and in the syntax required for the file's
format. For example, you can define the variable in your inventory file or in
your host or group variable files.
The following sample INI-formatted inventory file defines the Ansible network OS
as juniper.device.junos for the hosts in the
junos inventory group:
[junos] router1.example.com router2.example.com router3.example.com [junos:vars] ansible_network_os=juniper.device.junos ansible_connection=ansible.netcommon.netconf