diff --git a/lib/ansible/modules/database/mssql/mssql_db.py b/lib/ansible/modules/database/mssql/mssql_db.py index 42239d4df29..ac23e0be7e8 100644 --- a/lib/ansible/modules/database/mssql/mssql_db.py +++ b/lib/ansible/modules/database/mssql/mssql_db.py @@ -122,8 +122,7 @@ def db_delete(conn, cursor, db): def db_import(conn, cursor, module, db, target): if os.path.isfile(target): - backup = open(target, 'r') - try: + with open(target, 'r') as backup: sqlQuery = "USE [%s]\n" % db for line in backup: if line is None: @@ -135,8 +134,6 @@ def db_import(conn, cursor, module, db, target): sqlQuery += line cursor.execute(sqlQuery) conn.commit() - finally: - backup.close() return 0, "import successful", "" else: return 1, "cannot find target file", "cannot find target file" diff --git a/lib/ansible/modules/database/postgresql/postgresql_pg_hba.py b/lib/ansible/modules/database/postgresql/postgresql_pg_hba.py index dc223c6e88f..6a8451100da 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_pg_hba.py +++ b/lib/ansible/modules/database/postgresql/postgresql_pg_hba.py @@ -285,18 +285,17 @@ class PgHba(object): self.comment = [] # read the pg_hbafile try: - file = open(self.pg_hba_file, 'r') - for line in file: - line = line.strip() - # uncomment - if '#' in line: - line, comment = line.split('#', 1) - self.comment.append('#' + comment) - try: - self.add_rule(PgHbaRule(line=line)) - except PgHbaRuleError: - pass - file.close() + with open(self.pg_hba_file, 'r') as file: + for line in file: + line = line.strip() + # uncomment + if '#' in line: + line, comment = line.split('#', 1) + self.comment.append('#' + comment) + try: + self.add_rule(PgHbaRule(line=line)) + except PgHbaRuleError: + pass self.unchanged() except IOError: pass