diff --git a/changelogs/fragments/inv_json_sort_types_fix.yml b/changelogs/fragments/inv_json_sort_types_fix.yml new file mode 100644 index 00000000000..cb9225d0bf8 --- /dev/null +++ b/changelogs/fragments/inv_json_sort_types_fix.yml @@ -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). diff --git a/lib/ansible/cli/inventory.py b/lib/ansible/cli/inventory.py index 9f423747e5a..58df4a5a279 100644 --- a/lib/ansible/cli/inventory.py +++ b/lib/ansible/cli/inventory.py @@ -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 diff --git a/test/integration/targets/inventory/inv_with_int.yml b/test/integration/targets/inventory/inv_with_int.yml new file mode 100644 index 00000000000..5b2f21da711 --- /dev/null +++ b/test/integration/targets/inventory/inv_with_int.yml @@ -0,0 +1,6 @@ +all: + hosts: + testing123: + x: + a: 1 + 0: 2 diff --git a/test/integration/targets/inventory/runme.sh b/test/integration/targets/inventory/runme.sh index 4e42ce7012a..87bef4474ab 100755 --- a/test/integration/targets/inventory/runme.sh +++ b/test/integration/targets/inventory/runme.sh @@ -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 "$@"