# Introduce a new module option ‘encoding’ to the lineinfile module

**URL:** <https://forum.ansible.com/t/introduce-a-new-module-option-encoding-to-the-lineinfile-module/41745>\
**Category:** Project Discussions\
**Tags:** community, community-meeting, module, zos\
**Created:** [April 15, 2025, 4:53pm UTC](https://forum.ansible.com/t/introduce-a-new-module-option-encoding-to-the-lineinfile-module/41745 "2025-04-15T16:53:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ketankelkar](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ketankelkar/32/1047_2.png) [@ketankelkar](https://forum.ansible.com/u/ketankelkar)\
**Post date:** [April 15, 2025, 4:53pm UTC](https://forum.ansible.com/t/introduce-a-new-module-option-encoding-to-the-lineinfile-module/41745/1 "2025-04-15T16:53:45Z")

</div>

There is a [draft PR](https://github.com/ansible/ansible/pull/84999) for the `lineinfile` module which introduces a new module option `encoding` to add compatibility for target files not encoded in UTF-8.

Currently, the `lineinfile` module code does a binary-read on a target file and puts the contents as bytes in a buffer. This buffer is assumed to contain UTF-8 encoded bytes upon which regex matching operations and write operations are done. If a target file is not UTF-8 encoded, the regex matching does not work correctly because the regex comparison is a UTF-8 regex pattern compared to non-UTF-8 encoded bytes. And since write operations are done by adding UTF-8 bytes to the buffer, in the case of a non-UTF-8 encoded file, since this buffer would not contain UTF-8 encoded bytes, when the buffer is written to the file, the resulting file contains characters from multiple encodings.

The proposed change introduces a new module option `encoding`, which when specified reads the file contents into a Unicode text buffer instead of bytes so that regex matching is done in Unicode and write operations are done by adding Unicode chars to the buffer instead of UTF-8 bytes. Since Python3 strings internally represent characters in Unicode, all the Unicode operations are just simply Python string operations. File reads and writes are done in text-mode so that the optional encoding parameter can be specified when opening the file descriptor ([docs for Python open function](https://docs.python.org/3/library/functions.html#open)).

An alternative approach was explored where the initial file read was done in text-mode and converted to UTF-8 bytes so the remainder of the code could remain unchanged until the write operation at the very end which also involved a conversion. The Unicode approach seems overall cleaner even though it requires more changes. See the code diff for the other approach [here](https://github.com/ansible/ansible/compare/devel...ketankelkar:ansible:lineinfile-add-encoding-param).

See the code in the PR [here](https://github.com/ansible/ansible/pull/84999).

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [April 15, 2025, 4:55pm UTC](https://forum.ansible.com/t/introduce-a-new-module-option-encoding-to-the-lineinfile-module/41745/2 "2025-04-15T16:55:44Z")

</div>

FYI, adding something like this just to lineinfile does not make as much sense as creating a common facility for all modules that alter files to use.

---

<div class="post-metadata">

**Author:** ![ketankelkar](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ketankelkar/32/1047_2.png) [@ketankelkar](https://forum.ansible.com/u/ketankelkar)\
**Post date:** [April 15, 2025, 8:35pm UTC](https://forum.ansible.com/t/introduce-a-new-module-option-encoding-to-the-lineinfile-module/41745/3 "2025-04-15T20:35:46Z")

</div>

The initial thinking was to start with `lineinfile` , move on to `blockinfile` and then to any other suitable modules in the `ansible.builtin` collection one by one.

A common facility sounds like a good idea though I’m not sure what would that look like. Would it be like a class in `module_utils` which each relevant module calls?

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [April 15, 2025, 9:07pm UTC](https://forum.ansible.com/t/introduce-a-new-module-option-encoding-to-the-lineinfile-module/41745/4 "2025-04-15T21:07:09Z")

</div>

> [@ketankelkar](#):
>
> a good idea though I’m not sure what would that look like. Would it be like a class in `module_utils` which each relevant module ca

mostly, we already have such things like ‘atomic\_move’, permissions and other fs handling
