summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAbhijeet Kasurde <akasurde@redhat.com>2023-09-18 16:50:50 +0200
committerGitHub <noreply@github.com>2023-09-18 16:50:50 +0200
commita1a6550daf305ec9815a7b12db42c68b63426878 (patch)
treed4814f6a9121340b1d6577778137d76aa7081d99
parentreboot: show last error message in verbose log (#81578) (diff)
downloadansible-a1a6550daf305ec9815a7b12db42c68b63426878.tar.xz
ansible-a1a6550daf305ec9815a7b12db42c68b63426878.zip
inventory_ini: Handle SyntaxWarning in ini parsing (#81707)
* handle SyntaxWarning ini inventory parsing Fixes: #81328 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
-rw-r--r--changelogs/fragments/inventory_ini.yml3
-rw-r--r--lib/ansible/plugins/inventory/ini.py7
-rw-r--r--test/integration/targets/inventory_ini/inventory.ini2
-rwxr-xr-xtest/integration/targets/inventory_ini/runme.sh3
4 files changed, 13 insertions, 2 deletions
diff --git a/changelogs/fragments/inventory_ini.yml b/changelogs/fragments/inventory_ini.yml
new file mode 100644
index 0000000000..fffc5d4266
--- /dev/null
+++ b/changelogs/fragments/inventory_ini.yml
@@ -0,0 +1,3 @@
+---
+bugfixes:
+- inventory_ini - handle SyntaxWarning while parsing ini file in inventory (https://github.com/ansible/ansible/issues/81457).
diff --git a/lib/ansible/plugins/inventory/ini.py b/lib/ansible/plugins/inventory/ini.py
index e1c144e43e..1ff4bf167d 100644
--- a/lib/ansible/plugins/inventory/ini.py
+++ b/lib/ansible/plugins/inventory/ini.py
@@ -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
diff --git a/test/integration/targets/inventory_ini/inventory.ini b/test/integration/targets/inventory_ini/inventory.ini
index a0c99adee0..a5de42119f 100644
--- a/test/integration/targets/inventory_ini/inventory.ini
+++ b/test/integration/targets/inventory_ini/inventory.ini
@@ -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
diff --git a/test/integration/targets/inventory_ini/runme.sh b/test/integration/targets/inventory_ini/runme.sh
index 81bf147590..919e18845d 100755
--- a/test/integration/targets/inventory_ini/runme.sh
+++ b/test/integration/targets/inventory_ini/runme.sh
@@ -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