diff --git a/lib/ansible/modules/files/find.py b/lib/ansible/modules/files/find.py index 3613f1e609..b08105df08 100644 --- a/lib/ansible/modules/files/find.py +++ b/lib/ansible/modules/files/find.py @@ -250,19 +250,24 @@ def sizefilter(st, size): def contentfilter(fsname, pattern): - '''filter files which contain the given expression''' + """ + Filter files which contain the given expression + :arg fsname: Filename to scan for lines matching a pattern + :arg pattern: Pattern to look for inside of line + :rtype: bool + :returns: True if one of the lines in fsname matches the pattern. Otherwise False + """ if pattern is None: return True - try: - f = open(fsname) - prog = re.compile(pattern) - for line in f: - if prog.match(line): - f.close() - return True + prog = re.compile(pattern) + + try: + with open(fsname) as f: + for line in f: + if prog.match(line): + return True - f.close() except: pass