question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New inventory requires "hostname" even when not needed

See original GitHub issue

Version 2.0.0 has introduced a regression requiring the hostname key to be defined for individual inventory hosts. This is a poor assumption since an environment with name resolution (DNS, local hosts file, or otherwise) should not require such information. For example, this inventory worked in Version 1.x because the hostname csr1000v is resolvable.

$ cat hosts.yaml 
---
csr1000v:
  groups: ["csr"]

$ ping csr1000v -c 1
PING CSR1000V (172.31.55.203) 56(84) bytes of data.
64 bytes from CSR1000V (172.31.55.203): icmp_seq=1 ttl=255 time=1.06 ms

--- CSR1000V ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 1.066/1.066/1.066/0.000 ms

However when running the existing Nornir runbook, an exception is raised because the hostname key is undefined. Even worse, the error message says ip or host must be defined which aren’t even the right keys.

$ python3 get_facts_ios.py
napalm_get**********************************************************************
* csr1000v ** changed : False **************************************************
vvvv napalm_get ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv ERROR
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/nornir/core/task.py", line 62, in start
    r = self.task(self, **self.params)
  File "/usr/local/lib/python3.6/site-packages/nornir/plugins/tasks/networking/napalm_get.py", line 49, in napalm_get
    device = task.host.get_connection("napalm", task.nornir.config)
  File "/usr/local/lib/python3.6/site-packages/nornir/core/inventory.py", line 297, in get_connection
    **self.get_connection_parameters(connection).dict(),
  File "/usr/local/lib/python3.6/site-packages/nornir/core/inventory.py", line 347, in open_connection
    configuration=configuration,
  File "/usr/local/lib/python3.6/site-packages/nornir/plugins/connections/napalm.py", line 43, in open
    connection.open()
  File "/usr/local/lib/python3.6/site-packages/napalm/ios/ios.py", line 138, in open
    netmiko_optional_args=self.netmiko_optional_args,
  File "/usr/local/lib/python3.6/site-packages/napalm/base/base.py", line 82, in _netmiko_open
    **netmiko_optional_args)
  File "/usr/local/lib/python3.6/site-packages/netmiko/ssh_dispatcher.py", line 218, in ConnectHandler
    return ConnectionClass(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/netmiko/base_connection.py", line 170, in __init__
    raise ValueError("Either ip or host must be set")
ValueError: Either ip or host must be set

^^^^ END napalm_get ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I recommend two things:

  1. Change the error message to say “The ‘hostname’ key must be defined for each host when name resolution is unavailable”
  2. Change the logic to ensure that if name resolution succeeds that the exception is not raised.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
anuzelligcommented, Dec 27, 2018

I agree that an option to autopopulate the hostname would be nice, but for now I’m using the following as a simple workaround:

nr = InitNornir(config_file="config.yaml")
# Assign hostname if it was not set in the hosts file
for key, host in nr.inventory.hosts.items():
    if host.hostname is None:
        host.hostname = key
1reaction
dmfigolcommented, Dec 19, 2018

I am against changing the current defaults, but I am open to the feature “auto-populating the hostname” (as long as it is not default!) because “Explicit is better than implicit”. That said, I agree that the error Nick encountered is very confusing. I think we should fix our default connection plugins (netmiko, napalm) to check if the hostname is None and raise a custom exception with a sane message.

@nickrusso42518 you can specify the hostname instead of IP.

---
csr1000v:
  hostname: csr1000v
  groups: ["csr"]
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to build your inventory
The simplest inventory is a single file with a list of hosts and groups. The default location for this file is /etc/ansible/hosts ....
Read more >
How to run Ansible without specifying the inventory but ...
The trick is that after ip address/dns name, put the comma inside the quotes and requires ' hosts: all ' in your playbook....
Read more >
What is the purpose of setting hostname/FQDN in hosts file?
The hostnames need not be stored in DNS (or have a DNS domain at all) unless you own multiple devices sharing a private...
Read more >
PDQ Inventory not resolving hostnames any more? - Support
All PCs latest Windows 10. All machines "fresh and clean" with the necessary firewall settings for PDQ. The computer CAN resolve hostnames from...
Read more >
How To Set Up Ansible Inventories
In order follow this guide, you'll need: ... Move to that directory and open a new inventory file using your text editor of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found