diff --git a/changelogs/fragments/inventory_ini.yml b/changelogs/fragments/inventory_ini.yml new file mode 100644 index 00000000000..fffc5d42668 --- /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 e1c144e43ee..1ff4bf167d5 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 a0c99adee06..a5de42119ff 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 81bf1475904..919e18845d4 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