summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Coca <bcoca@users.noreply.github.com>2021-03-08 15:58:02 -0500
committerGitHub <noreply@github.com>2021-03-08 14:58:02 -0600
commitdf5d5959845a06e7a0cbf4281f777fbf7489904e (patch)
treef6a1e1d761e6481beec1afbadb12b4a686365fe1
parentd78867e493a5dcc77ce3c9df146cbdce0f76ffb2 (diff)
downloadansible-df5d5959845a06e7a0cbf4281f777fbf7489904e.tar.gz
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 527bff6b79081b942c0ac9d0b1e306b99ffa81a6)
-rw-r--r--changelogs/fragments/inv_json_sort_types_fix.yml2
-rw-r--r--lib/ansible/cli/inventory.py6
-rw-r--r--test/integration/targets/inventory/inv_with_int.yml6
-rwxr-xr-xtest/integration/targets/inventory/runme.sh2
4 files changed, 15 insertions, 1 deletions
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 0000000000..cb9225d0bf
--- /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 9f423747e5..58df4a5a27 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 0000000000..5b2f21da71
--- /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 4e42ce7012..87bef4474a 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 "$@"