From 38dd49eb005dc784aad4809a9ee98dc84ff60eec Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 5 May 2021 14:12:52 +0530 Subject: [PATCH] ansible-inventory: Handle exception in toml parsing (#74486) Handle stacktrace raise while parsing inventory in toml format Fixes: #74404 Signed-off-by: Abhijeet Kasurde --- .../fragments/74404_ansible_inventory.yml | 2 ++ lib/ansible/cli/inventory.py | 10 +++++++- .../files/invalid_sample.yml | 7 ++++++ .../ansible-inventory/files/valid_sample.yml | 7 ++++++ .../targets/ansible-inventory/tasks/main.yml | 24 +++++++++++++++++++ 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/74404_ansible_inventory.yml create mode 100644 test/integration/targets/ansible-inventory/files/invalid_sample.yml create mode 100644 test/integration/targets/ansible-inventory/files/valid_sample.yml diff --git a/changelogs/fragments/74404_ansible_inventory.yml b/changelogs/fragments/74404_ansible_inventory.yml new file mode 100644 index 00000000000..94963c5fd14 --- /dev/null +++ b/changelogs/fragments/74404_ansible_inventory.yml @@ -0,0 +1,2 @@ +bugfixes: +- ansible-inventory - handle an exception while parsing inventory in toml format (https://github.com/ansible/ansible/issues/74404). diff --git a/lib/ansible/cli/inventory.py b/lib/ansible/cli/inventory.py index a63afdef196..135fe1fa974 100644 --- a/lib/ansible/cli/inventory.py +++ b/lib/ansible/cli/inventory.py @@ -179,7 +179,15 @@ class InventoryCLI(CLI): raise AnsibleError( 'The python "toml" library is required when using the TOML output format' ) - results = toml_dumps(stuff) + try: + results = toml_dumps(stuff) + except KeyError as e: + raise AnsibleError( + 'The source inventory contains a non-string key (%s) which cannot be represented in TOML. ' + 'The specified key will need to be converted to a string. Be aware that if your playbooks ' + 'expect this key to be non-string, your playbooks will need to be modified to support this ' + 'change.' % e.args[0] + ) else: import json from ansible.parsing.ajson import AnsibleJSONEncoder diff --git a/test/integration/targets/ansible-inventory/files/invalid_sample.yml b/test/integration/targets/ansible-inventory/files/invalid_sample.yml new file mode 100644 index 00000000000..f7bbe0cf018 --- /dev/null +++ b/test/integration/targets/ansible-inventory/files/invalid_sample.yml @@ -0,0 +1,7 @@ +all: + children: + somegroup: + hosts: + something: + 7.2: bar + ungrouped: {} diff --git a/test/integration/targets/ansible-inventory/files/valid_sample.yml b/test/integration/targets/ansible-inventory/files/valid_sample.yml new file mode 100644 index 00000000000..477f82f2308 --- /dev/null +++ b/test/integration/targets/ansible-inventory/files/valid_sample.yml @@ -0,0 +1,7 @@ +all: + children: + somegroup: + hosts: + something: + foo: bar + ungrouped: {} \ No newline at end of file diff --git a/test/integration/targets/ansible-inventory/tasks/main.yml b/test/integration/targets/ansible-inventory/tasks/main.yml index d8458eca50d..7ec924f21fe 100644 --- a/test/integration/targets/ansible-inventory/tasks/main.yml +++ b/test/integration/targets/ansible-inventory/tasks/main.yml @@ -81,3 +81,27 @@ that: - result is failed - '"ERROR! Could not match supplied host pattern, ignoring: invalid" in result.stderr' + +- name: Install toml package + pip: + name: + - toml + state: present + +- name: "test option: --toml with valid group name" + command: ansible-inventory --list --toml -i {{ role_path }}/files/valid_sample.yml + register: result + +- assert: + that: + - result is succeeded + +- name: "test option: --toml with invalid group name" + command: ansible-inventory --list --toml -i {{ role_path }}/files/invalid_sample.yml + ignore_errors: true + register: result + +- assert: + that: + - result is failed + - '"ERROR! The source inventory contains a non-string key" in result.stderr'