diff --git a/lib/ansible/module_utils/facts.py b/lib/ansible/module_utils/facts.py index 5ceeb405d55..2db9d8ccffe 100644 --- a/lib/ansible/module_utils/facts.py +++ b/lib/ansible/module_utils/facts.py @@ -2381,9 +2381,13 @@ class SunOSVirtual(Virtual): def get_file_content(path, default=None): data = default if os.path.exists(path) and os.access(path, os.R_OK): - data = open(path).read().strip() - if len(data) == 0: - data = default + try: + datafile = open(path) + data = datafile.read().strip() + if len(data) == 0: + data = default + finally: + datafile.close() return data def ansible_facts(module):