Repository: ansible/ansible

Issue:
Python3 -  docker_service - TypeError: cannot use a string pattern on a bytes-like object
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
docker_service

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.5.0
  config file = /Users/Christian/Development/Datalogi/Speciale/source/ansible.cfg
  configured module search path = ['/Users/Christian/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/ansible
  executable location = /Library/Frameworks/Python.framework/Versions/3.5/bin/ansible
  python version = 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
[defaults]
inventory = hosts
roles_path = roles

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
Local:
macOS 10.12.6

Remote:
Vagrant box: bento/ubuntu-16.04
Python 3.5.2

##### SUMMARY
<!--- Explain the problem briefly -->

I'm new to Ansible and I get this error trying to use the docker_service module running a docker-compose file.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Run subscription service
  docker_service:
    project_src: /skycave/subscription-service
    state: present
```

hosts:
```
[subscription-server]
1.1.1.100

[all:vars]
ansible_user=root
ansible_python_interpreter=/usr/bin/python3
```
<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
ok: [1.1.1.100]

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
fatal: [1.1.1.100]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Shared connection to 1.1.1.100 closed.\r\n", "module_stdout": "Starting subscriptionservice_subscriptionservice_1 ... \r\r\n\u001b[1A\u001b[2K\rStarting subscriptionservice_subscriptionservice_1 ... \u001b[32mdone\u001b[0m\r\u001b[1BTraceback (most recent call last):\r\n  File \"/tmp/ansible_vrilashu/ansible_module_docker_service.py\", line 1016, in <module>\r\n    main()\r\n  File \"/tmp/ansible_vrilashu/ansible_module_docker_service.py\", line 1011, in main\r\n    result = ContainerManager(client).exec_module()\r\n  File \"/tmp/ansible_vrilashu/ansible_module_docker_service.py\", line 608, in exec_module\r\n    result = self.cmd_up()\r\n  File \"/tmp/ansible_vrilashu/ansible_module_docker_service.py\", line 703, in cmd_up\r\n    get_stdout(fd_name)\r\n  File \"/tmp/ansible_vrilashu/ansible_module_docker_service.py\", line 512, in get_stdout\r\n    new_line = re.sub(r'\\x1b\\[.+m', '', line.encode('ascii'))\r\n  File \"/usr/lib/python3.5/re.py\", line 182, in sub\r\n    return _compile(pattern, flags).sub(repl, string, count)\r\nTypeError: cannot use a string pattern on a bytes-like object\r\n", "msg": "MODULE FAILURE", "rc": 0}
```



Discussion:
cc @chouseknecht
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I'm getting the same error on a slightly different environment:

#### Ansible version
```
ansible 2.3.2.0
  config file = /home/bert/workspace/core-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]
```

#### OS / environment
Local:
Linux mint 18.2

Remote:
Ubuntu 14.04
Python 3.4.3

#### Result

```
fatal: [******]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Creating cadvisor ... \r\nCreating node-exporter ... \r\n\u001b[1A\u001b[2K\rCreating cadvisor ... \u001b[32mdone\u001b[0m\r\u001b[1B\u001b[1A\u001b[2K\rCreating node-exporter ... \u001b[32mdone\u001b[0m\r\u001b[1BTraceback (most recent call last):\n  File \"/tmp/ansible_7rjooqxf/ansible_module_docker_service.py\", line 1016, in <module>\n    main()\n  File \"/tmp/ansible_7rjooqxf/ansible_module_docker_service.py\", line 1011, in main\n    result = ContainerManager(client).exec_module()\n  File \"/tmp/ansible_7rjooqxf/ansible_module_docker_service.py\", line 608, in exec_module\n    result = self.cmd_up()\n  File \"/tmp/ansible_7rjooqxf/ansible_module_docker_service.py\", line 703, in cmd_up\n    get_stdout(fd_name)\n  File \"/tmp/ansible_7rjooqxf/ansible_module_docker_service.py\", line 512, in get_stdout\n    new_line = re.sub(r'\\x1b\\[.+m', '', line.encode('ascii'))\n  File \"/usr/lib/python3.4/re.py\", line 179, in sub\n    return _compile(pattern, flags).sub(repl, string, count)\nTypeError: can't use a string pattern on a bytes-like object\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}
```

Resolving patch:
diff --git a/lib/ansible/modules/cloud/docker/docker_service.py b/lib/ansible/modules/cloud/docker/docker_service.py
--- a/lib/ansible/modules/cloud/docker/docker_service.py
+++ b/lib/ansible/modules/cloud/docker/docker_service.py
@@ -520,7 +520,7 @@ def get_redirected_output(path_name):
     with open(path_name, 'r') as fd:
         for line in fd:
             # strip terminal format/color chars
-            new_line = re.sub(r'\x1b\[.+m', '', line.encode('ascii'))
+            new_line = re.sub(r'\x1b\[.+m', '', line)
             output.append(new_line)
     fd.close()
     os.remove(path_name)


Test patch: