Check to see if a line is already commented before moving on

Fixes #37939
This commit is contained in:
rallytime 2016-12-01 16:52:56 -07:00
parent f2c045520d
commit 8a685b1820

View file

@ -3340,9 +3340,16 @@ def comment(name, regex, char='#', backup='.bak'):
return _error(ret, check_msg)
unanchor_regex = regex.lstrip('^').rstrip('$')
comment_regex = char + unanchor_regex
# Check if the line is already commented
if __salt__['file.search'](name, comment_regex, multiline=True):
commented = True
else:
commented = False
# Make sure the pattern appears in the file before continuing
if not __salt__['file.search'](name, regex, multiline=True):
if commented or not __salt__['file.search'](name, regex, multiline=True):
if __salt__['file.search'](name, unanchor_regex, multiline=True):
ret['comment'] = 'Pattern already commented'
ret['result'] = True