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
pull/70816/merge
Brian Coca 3 years ago committed by GitHub
parent 6514027957
commit 527bff6b79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -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

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

@ -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 "$@"

Loading…
Cancel
Save