Fix file descriptor leak in lineinfile module.
diff --git a/changelogs/fragments/57327-fix-file-descriptor-leak.yaml b/changelogs/fragments/57327-fix-file-descriptor-leak.yaml
new file mode 100644
index 00000000000000..cee6d4f066e351
--- /dev/null
+++ b/changelogs/fragments/57327-fix-file-descriptor-leak.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+- lineinfile - fix a race / file descriptor leak when writing the file (https://github.com/ansible/ansible/issues/57327)
diff --git a/lib/ansible/modules/files/lineinfile.py b/lib/ansible/modules/files/lineinfile.py
index 698bd4872f1aed..86bc44e4f2eefc 100644
--- a/lib/ansible/modules/files/lineinfile.py
+++ b/lib/ansible/modules/files/lineinfile.py
@@ -215,7 +215,7 @@
 def write_changes(module, b_lines, dest):
 
     tmpfd, tmpfile = tempfile.mkstemp()
-    with open(tmpfile, 'wb') as f:
+    with os.fdopen(tmpfd, 'wb') as f:
         f.writelines(b_lines)
 
     validate = module.params.get('validate', None)

##### SUMMARY

Fix a minor file descriptor leak in the lineinfile module.

Fixes #57327

##### ISSUE TYPE

- Bugfix Pull Request

##### COMPONENT NAME

- lineinfile

##### ADDITIONAL INFORMATION

This simply restores the prior `os.fdopen()` call so that the unmanaged file descriptor is consumed by a Python file object.