From df4109d94659ad2a343b0732e63d44104823580b Mon Sep 17 00:00:00 2001 From: Ales Nosek Date: Wed, 2 Dec 2015 20:31:27 -0800 Subject: [PATCH] Fix #2475 ini_file module: bracklets in key break idempotence Escape the regex special characters in the option name. --- lib/ansible/modules/files/ini_file.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/files/ini_file.py b/lib/ansible/modules/files/ini_file.py index fe583c65afb..86a7f35ddda 100644 --- a/lib/ansible/modules/files/ini_file.py +++ b/lib/ansible/modules/files/ini_file.py @@ -98,6 +98,22 @@ import ConfigParser import sys import os +# ============================================================== +# match_opt + +def match_opt(option, line): + option = re.escape(option) + return re.match('%s *=' % option, line) \ + or re.match('# *%s *=' % option, line) \ + or re.match('; *%s *=' % option, line) + +# ============================================================== +# match_active_opt + +def match_active_opt(option, line): + option = re.escape(option) + return re.match('%s *=' % option, line) + # ============================================================== # do_ini @@ -140,9 +156,7 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese if within_section and option: if state == 'present': # change the existing option line - if re.match('%s *=' % option, line) \ - or re.match('# *%s *=' % option, line) \ - or re.match('; *%s *=' % option, line): + if match_opt(option, line): newline = '%s = %s\n' % (option, value) changed = ini_lines[index] != newline ini_lines[index] = newline @@ -153,14 +167,14 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese line = ini_lines[index] if line.startswith('['): break - if re.match('%s *=' % option, line): + if match_active_opt(option, line): del ini_lines[index] else: index = index + 1 break else: # comment out the existing option line - if re.match('%s *=' % option, line): + if match_active_opt(option, line): ini_lines[index] = '#%s' % ini_lines[index] changed = True break