ISSUE TYPE
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)
ISSUE TYPE
COMPONENT NAME
Ansible, inventory script
ANSIBLE VERSION
CONFIGURATION
No changes to
ansible.cfgOS / 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 isNone) from its__repr__method. This is not allowed in Python.And yet, the Host
__init__defaults the name toNone, so this failure is actually the default case.The simplest possible example is probably:
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