8000 Blockinfile - Add new module option - 'encoding' by ketankelkar · Pull Request #85291 · ansible/ansible · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Blockinfile - Add new module option - 'encoding' #85291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and 10000 privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: devel
Choose a base branch
from

Conversation

ketankelkar
Copy link
SUMMARY

This PR for the blockinfile module introduces a new module option encoding to add compatibility for target files not encoded in UTF-8. It mirrors the PR for lineinfile (#84999)
 
Currently, the blockinfile 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 buffer containing Unicode characters instead of bytes so that regex matching is done in Unicode and write operations are done by adding Unicode characters 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 (https://docs.python.org/3/library/functions.html#open).

ISSUE TYPE
  • Feature Pull Request

@ansibot ansibot added feature This issue/PR relates to a feature request. needs_triage Needs a first human triage before being processed. module This issue/PR relates to a module. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jun 10, 2025
@ansibot
Copy link
Contributor
ansibot commented Jun 10, 2025

The test ansible-test sanity --test pep8 [explain] failed with 4 errors:

lib/ansible/modules/blockinfile.py:110:26: W291: trailing whitespace
lib/ansible/modules/blockinfile.py:264:13: E225: missing whitespace around operator
lib/ansible/modules/blockinfile.py:374:11: E111: indentation is not a multiple of 4
lib/ansible/modules/blockinfile.py:375:11: E111: indentation is not a multiple of 4

The test ansible-test sanity --test pylint [explain] failed with 2 errors:

lib/ansible/modules/blockinfile.py:201:0: unused-import: Unused b imported from ansible.module_utils.six
lib/ansible/modules/blockinfile.py:203:0: unused-import: Unused to_bytes imported from ansible.module_utils.common.text.converters

click here for bot help

Signed-off-by: Ketan Kelkar <ktnklkr@gmail.com>
@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Jun 10, 2025
@ketankelkar
Copy link
Author
ketankelkar commented Jun 10, 2025

@ketankelkar ketankelkar changed the title Blockinfile add encoding param unicode compare Blockinfile - Add new module option - 'encoding' Jun 10, 2025
@s-hertel s-hertel removed the needs_triage Needs a first human triage before being processed. label Jun 17, 2025
@ketankelkar ketankelkar marked this pull request as ready for review June 23, 2025 18:46
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jun 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature This issue/PR relates to a feature request. module This issue/PR relates to a module. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0