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:
parent
68e9e1c999
commit
38dd49eb00
5 changed files with 49 additions and 1 deletions
2
changelogs/fragments/74404_ansible_inventory.yml
Normal file
2
changelogs/fragments/74404_ansible_inventory.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- ansible-inventory - handle an exception while parsing inventory in toml format (https://github.com/ansible/ansible/issues/74404).
|
|
@ -179,7 +179,15 @@ class InventoryCLI(CLI):
|
||||||
raise AnsibleError(
|
raise AnsibleError(
|
||||||
'The python "toml" library is required when using the TOML output format'
|
'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:
|
else:
|
||||||
import json
|
import json
|
||||||
from ansible.parsing.ajson import AnsibleJSONEncoder
|
from ansible.parsing.ajson import AnsibleJSONEncoder
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
all:
|
||||||
|
children:
|
||||||
|
somegroup:
|
||||||
|
hosts:
|
||||||
|
something:
|
||||||
|
7.2: bar
|
||||||
|
ungrouped: {}
|
|
@ -0,0 +1,7 @@
|
||||||
|
all:
|
||||||
|
children:
|
||||||
|
somegroup:
|
||||||
|
hosts:
|
||||||
|
something:
|
||||||
|
foo: bar
|
||||||
|
ungrouped: {}
|
|
@ -81,3 +81,27 @@
|
||||||
that:
|
that:
|
||||||
- result is failed
|
- result is failed
|
||||||
- '"ERROR! Could not match supplied host pattern, ignoring: invalid" in result.stderr'
|
- '"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'
|
||||||
|
|
Loading…
Reference in a new issue