ansible-inventory: Handle exception in toml parsing (#74486)

Handle stacktrace raise while parsing inventory in toml format

Fixes: #74404

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2021-05-05 14:12:52 +05:30 committed by GitHub
parent 68e9e1c999
commit 38dd49eb00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 1 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- ansible-inventory - handle an exception while parsing inventory in toml format (https://github.com/ansible/ansible/issues/74404).

View file

@ -179,7 +179,15 @@ class InventoryCLI(CLI):
raise AnsibleError(
'The python "toml" library is required when using the TOML output format'
)
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

View file

@ -0,0 +1,7 @@
all:
children:
somegroup:
hosts:
something:
7.2: bar
ungrouped: {}

View file

@ -0,0 +1,7 @@
all:
children:
somegroup:
hosts:
something:
foo: bar
ungrouped: {}

View file

@ -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'