Don't fail for mixed typed keys (#73726)

* Don't fail for mixed typed keys

  but warn that content cound not be sorted because of this

* added tests
This commit is contained in:
Brian Coca 2021-03-03 14:24:50 -05:00 committed by GitHub
parent 6514027957
commit 527bff6b79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 1 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Deal with failures when sorting JSON and you have incompatible key types.

View file

@ -183,7 +183,11 @@ class InventoryCLI(CLI):
else:
import json
from ansible.parsing.ajson import AnsibleJSONEncoder
results = json.dumps(stuff, cls=AnsibleJSONEncoder, sort_keys=True, indent=4, preprocess_unsafe=True)
try:
results = json.dumps(stuff, cls=AnsibleJSONEncoder, sort_keys=True, indent=4, preprocess_unsafe=True)
except TypeError as e:
results = json.dumps(stuff, cls=AnsibleJSONEncoder, sort_keys=False, indent=4, preprocess_unsafe=True)
display.warning("Could not sort JSON output due to issues while sorting keys: %s" % to_native(e))
return results

View file

@ -0,0 +1,6 @@
all:
hosts:
testing123:
x:
a: 1
0: 2

View file

@ -85,3 +85,6 @@ if ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=True ansible -m ping localhost -i "$
echo "Empty directory should cause failure when ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=True"
exit 1
fi
# ensure we don't traceback on inventory due to variables with int as key
ansible-inventory -i inv_with_int.yml --list "$@"