inventory_ini: Handle SyntaxWarning in ini parsing (#81707)

* handle SyntaxWarning ini inventory parsing

Fixes: #81328

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/81712/head
Abhijeet Kasurde 9 months ago committed by GitHub
parent 2793dfa594
commit a1a6550daf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
---
bugfixes:
- inventory_ini - handle SyntaxWarning while parsing ini file in inventory (https://github.com/ansible/ansible/issues/81457).

@ -75,6 +75,7 @@ host4 # same host as above, but member of 2 groups, will inherit vars from both
import ast
import re
import warnings
from ansible.inventory.group import to_safe_group_name
from ansible.plugins.inventory import BaseFileInventoryPlugin
@ -341,9 +342,11 @@ class InventoryModule(BaseFileInventoryPlugin):
(int, dict, list, unicode string, etc).
'''
try:
v = ast.literal_eval(v)
with warnings.catch_warnings():
warnings.simplefilter("ignore", SyntaxWarning)
v = ast.literal_eval(v)
# Using explicit exceptions.
# Likely a string that literal_eval does not like. We wil then just set it.
# Likely a string that literal_eval does not like. We will then just set it.
except ValueError:
# For some reason this was thought to be malformed.
pass

@ -1,3 +1,5 @@
gitlab-runner-01 ansible_host=gitlab-runner-01.internal.example.net ansible_user=root
[local]
testhost ansible_connection=local ansible_become=no ansible_become_user=ansibletest1

@ -3,3 +3,6 @@
set -eux
ansible-playbook -v -i inventory.ini test_ansible_become.yml
ansible-inventory -v -i inventory.ini --list 2> out
test "$(grep -c 'SyntaxWarning' out)" -eq 0

Loading…
Cancel
Save