add error handling when parsing values in ini files (#82718)

Fixes: #82717

Co-authored-by: Karl A. Grindley <kgrindley@ll.mit.edu>
pull/83291/merge
Karl G 4 months ago committed by GitHub
parent c6d5be5cac
commit 6bf6844a1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,3 @@
---
bugfixes:
- local - handle error while parsing values in ini files (https://github.com/ansible/ansible/issues/82717).

@ -1,17 +1,5 @@
# This file is part of Ansible # Copyright: Contributors to the Ansible project
# # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations from __future__ import annotations
@ -25,7 +13,6 @@ import ansible.module_utils.compat.typing as t
from ansible.module_utils.common.text.converters import to_text from ansible.module_utils.common.text.converters import to_text
from ansible.module_utils.facts.utils import get_file_content from ansible.module_utils.facts.utils import get_file_content
from ansible.module_utils.facts.collector import BaseFactCollector from ansible.module_utils.facts.collector import BaseFactCollector
from ansible.module_utils.six import PY3
from ansible.module_utils.six.moves import configparser, StringIO from ansible.module_utils.six.moves import configparser, StringIO
@ -91,12 +78,9 @@ class LocalFactCollector(BaseFactCollector):
# if that fails read it with ConfigParser # if that fails read it with ConfigParser
cp = configparser.ConfigParser() cp = configparser.ConfigParser()
try: try:
if PY3:
cp.read_file(StringIO(out)) cp.read_file(StringIO(out))
else:
cp.readfp(StringIO(out))
except configparser.Error: except configparser.Error:
fact = "error loading facts as JSON or ini - please check content: %s" % fn fact = f"error loading facts as JSON or ini - please check content: {fn}"
module.warn(fact) module.warn(fact)
else: else:
fact = {} fact = {}
@ -104,7 +88,13 @@ class LocalFactCollector(BaseFactCollector):
if sect not in fact: if sect not in fact:
fact[sect] = {} fact[sect] = {}
for opt in cp.options(sect): for opt in cp.options(sect):
try:
val = cp.get(sect, opt) val = cp.get(sect, opt)
except configparser.Error as ex:
fact = f"error loading facts as ini - please check content: {fn} ({ex})"
module.warn(fact)
continue
else:
fact[sect][opt] = val fact[sect][opt] = val
except Exception as e: except Exception as e:
fact = "Failed to convert (%s) to JSON: %s" % (fn, to_text(e)) fact = "Failed to convert (%s) to JSON: %s" % (fn, to_text(e))

@ -0,0 +1,2 @@
[bad fact]
value = this is a % bad % fact

@ -19,6 +19,7 @@
mode: '0775' mode: '0775'
- name: unreadable - name: unreadable
mode: '0000' mode: '0000'
- name: bad
- name: Create dangling symlink - name: Create dangling symlink
file: file:
@ -51,3 +52,4 @@
- setup_result['ansible_facts']['ansible_local']['basdscript'].startswith("Failure executing fact script") - setup_result['ansible_facts']['ansible_local']['basdscript'].startswith("Failure executing fact script")
- setup_result['ansible_facts']['ansible_local']['unreadable'].startswith('error loading facts') - setup_result['ansible_facts']['ansible_local']['unreadable'].startswith('error loading facts')
- setup_result['ansible_facts']['ansible_local']['dead_symlink'].startswith('Could not stat fact') - setup_result['ansible_facts']['ansible_local']['dead_symlink'].startswith('Could not stat fact')
- setup_result['ansible_facts']['ansible_local']['bad'].startswith('error loading facts as ini')

Loading…
Cancel
Save