Skip to content
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 privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blockinfile: insert[before|after] throws: cannot use a string pattern on a bytes-like object #26793

Closed
Tux12Fun opened this issue Jul 14, 2017 · 4 comments · Fixed by #29166
Closed
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. module This issue/PR relates to a module. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@Tux12Fun
Copy link

ISSUE TYPE
  • Bug Report
COMPONENT NAME

blockinfile.py

ANSIBLE VERSION
ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
CONFIGURATION
OS / ENVIRONMENT

OS: Ubuntu 16.04 Xenial with Python3 (interpreter is set to Python3)

SUMMARY

Python3 changes the behavior for the search function as it seams.

STEPS TO REPRODUCE

Only affects insertbefore / insertafter

  - name: Insert new Prompt
    blockinfile:
      path:            /root/.bashrc
      insertafter:     '###_OLD-PROMPT_###.'
      block: |
               if [ "$USER" == "root" ]; then
                  PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(__cegit)\[\033[00m\]\$ '
               else
                  PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(__cegit)\[\033[00m\]\$ '
               fi
EXPECTED RESULTS

No error should occur. We are only replacing in a ASCII file a part.

ACTUAL RESULTS

Module crash

fatal: [******-wi1*********.local]: FAILED! => {"changed": false, "failed": true, "module_stderr": 
"Shared connection to ******-wi1.**********.local closed.\r\n", "module_stdout": 
"Traceback (most recent call last):\r\n  File \"/tmp/ansible_0s3yacif/ansible_module_blockinfile.py\", 
line 334, in <module>\r\n main()\r\n  File \"/tmp/ansible_0s3yacif/ansible_module_blockinfile.py\",
 line 284, in main\r\n    if insertre.search(line):\r\nTypeError: cannot use a string 
pattern on a bytes-like object\r\n", "msg": 
"MODULE FAILURE", "rc": 0}
BUGFIX

Caution, a developer should verify this, because I'am not firm with Python at all.
Neither with Python 2 / Python 3 differencees

FILE: ansible/lib/ansible/modules/files/blockinfile.py
                       Current Source Code                >>          Changed to
255
256    if insertafter not in (None, 'EOF'):
257        insertre = re.compile(insertafter)             >>        insertre = re.compile(b(insertafter))
258    elif insertbefore not in (None, 'BOF'):
259        insertre = re.compile(insertbefore)            >>        insertre = re.compile(b(insertbefore))
260    else:
261        insertre = None
262

@ansibot
Copy link
Contributor

ansibot commented Jul 14, 2017

@ansibot ansibot added affects_2.3 This issue/PR affects Ansible v2.3 bug_report module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Jul 14, 2017
@Tux12Fun Tux12Fun changed the title Blockinfile: insert[before|after] throws: Blockinfile: insert[before|after] throws: cannot use a string pattern on a bytes-like object Jul 14, 2017
@jctanner jctanner removed the needs_triage Needs a first human triage before being processed. label Jul 14, 2017
@smellman
Copy link

smellman commented Aug 7, 2017

In my case, I fix same problem using to_bytes() instead of b().

    if insertafter not in (None, 'EOF'):
        insertre = re.compile(to_bytes(insertafter))
    elif insertbefore not in (None, 'BOF'):
        insertre = re.compile(to_bytes(insertbefore))
    else:
        insertre = None

@DevOpsJon
Copy link

@smellman Your solution worked for me. Thanks.

@bak1an
Copy link
Contributor

bak1an commented Sep 8, 2017

Affects me as well on current devel.

I've used similar lineinfile module for now and it uses to_bytes(..., errors='surrogate_or_strict') to convert regex.

Going to make a PR to do the same in blockinfile.

bak1an added a commit to bak1an/ansible that referenced this issue Sep 8, 2017
abadger pushed a commit that referenced this issue Sep 9, 2017
abadger pushed a commit that referenced this issue Sep 9, 2017
Akasurde pushed a commit to Akasurde/ansible that referenced this issue Sep 10, 2017
prasadkatti pushed a commit to prasadkatti/ansible that referenced this issue Oct 1, 2017
BondAnthony pushed a commit to BondAnthony/ansible that referenced this issue Oct 5, 2017
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. module This issue/PR relates to a module. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants