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..cee79035795 --- /dev/null +++ b/changelogs/fragments/inv_json_sort_types_fix.yml @@ -0,0 +1,2 @@ +bugfixes: + - Deal with failures when sorting JSON and you have incompatible key types. diff --git a/lib/ansible/cli/inventory.py b/lib/ansible/cli/inventory.py index 5fd6aa667eb..a63afdef196 100644 --- a/lib/ansible/cli/inventory.py +++ b/lib/ansible/cli/inventory.py @@ -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 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 dba8a519475..7ce16cf54ef 100755 --- a/test/integration/targets/inventory/runme.sh +++ b/test/integration/targets/inventory/runme.sh @@ -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 "$@"