From c4f43eb965528ac42aaf18d347f912320055246d Mon Sep 17 00:00:00 2001 From: nichivo Date: Mon, 19 Sep 2016 16:56:29 +1000 Subject: [PATCH] Insert missing option line before blank lines (#4747) --- lib/ansible/modules/files/ini_file.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/files/ini_file.py b/lib/ansible/modules/files/ini_file.py index 6bb962b157c..2d6307f77a8 100644 --- a/lib/ansible/modules/files/ini_file.py +++ b/lib/ansible/modules/files/ini_file.py @@ -154,8 +154,12 @@ def do_ini(module, filename, section=None, option=None, value=None, state='prese if within_section: if state == 'present': # insert missing option line at the end of the section - ini_lines.insert(index, assignment_format % (option, value)) - changed = True + for i in range(index, 0, -1): + # search backwards for previous non-blank or non-comment line + if not re.match(r'^[ \t]*([#;].*)?$', ini_lines[i - 1]): + ini_lines.insert(i, assignment_format % (option, value)) + changed = True + break elif state == 'absent' and not option: # remove the entire section del ini_lines[section_start:index]