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

but warn that content cound not be sorted because of this

* added tests

(cherry picked from commit 527bff6b79)
pull/73777/head
Brian Coca 4 years ago committed by GitHub
parent d78867e493
commit df5d595984
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- ansible-inventory CLI - Deal with failures when sorting JSON and you have incompatible key types (https://github.com/ansible/ansible/issues/68950).

@ -182,7 +182,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

@ -82,3 +82,5 @@ 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
ansible-inventory -i inv_with_int.yml --list "$@"

Loading…
Cancel
Save