Skip to content

Inventory Host class's __repr__/__str__ can return None (TypeError) #24545

Description

@movermeyer
ISSUE TYPE
  • Bug Report
COMPONENT NAME

Ansible, inventory script

ANSIBLE VERSION
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
CONFIGURATION

No changes to ansible.cfg

OS / ENVIRONMENT

N/A

SUMMARY

Ansible's Inventory Host class's __repr__/__str__ can return None. This is not allowed in Python and raises TypeError.

STEPS TO REPRODUCE

I was using the Cobbler inventory script and it created an Ansible Host object that had no hostname. This alone is unexpected (seems to be a bug in the Cobbler inventory script (#24546)), but that is not what this issue is about. While trying to debug the problem, I tried to print out the host that was having issues. However even just printing the object was causing Python to crash.

This is a result of Host object returning just self.name (which is None) from its __repr__ method. This is not allowed in Python.
And yet, the Host __init__ defaults the name to None, so this failure is actually the default case.

The simplest possible example is probably:

from ansible.inventory.host import Host
print Host()
EXPECTED RESULTS

I would expect it to print a string representation of the Host, (perhaps something like "Host:{self.name}") instead of crashing Python.

ACTUAL RESULTS
python test.py
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    print Host()
TypeError: __str__ returned non-string (type NoneType)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    affects_2.3This issue/PR affects Ansible v2.3bugThis issue/PR relates to a bug.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions