# Buggy code
def update_config_file(self):
    """
    If the authorization not stored in the config file or reauthorize is True,
    update the config file with the new authorization.

    :return: None
    """

    path = os.path.expanduser(self.config_path)
    if not self.config_file_exists(path):
        self.create_config_file(path)

    try:
        # read the existing config
        config = json.load(open(path, "r"))
    except ValueError:
        self.log("Error reading config from %s" % (path))
        config = dict()

    if not config.get("auths"):
        self.log("Adding auths dict to config.")
        config["auths"] = dict()

    if not config["auths"].get(self.registry_url):
        self.log("Adding registry_url %s to auths." % (self.registry_url))
        config["auths"][self.registry_url] = dict()

    encoded_credentials = dict(
        auth=base64.b64encode(self.username + b":" + self.password), email=self.email
    )

    if config["auths"][self.registry_url] != encoded_credentials or self.reauthorize:
        # Update the config file with the new authorization
        config["auths"][self.registry_url] = encoded_credentials
        self.log(
            "Updating config file %s with new authorization for %s"
            % (path, self.registry_url)
        )
        self.results["actions"].append(
            "Updated config file %s with new authorization for %s"
            % (path, self.registry_url)
        )
        self.results["changed"] = True
        self.write_config(path, config)


# Traceback (nTypeError)
TASK [Log into DockerHub] ***********************************
task path: /root/core/docker.yml:66
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/cloud/docker/docker_login.py
<> ESTABLISH SSH CONNECTION FOR USER: root
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/cloud/docker/docker_login.py
<> ESTABLISH SSH CONNECTION FOR USER: root
<> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o Port=4422 -o 'IdentityFile="/root/.ssh/id_rsa4096"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=root -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/3b98f69a81 <> '/bin/sh -c '"'"'/usr/bin/python3 &amp;&amp; sleep 0'"'"''
<> (1, '', 'OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 19: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 25\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\nTraceback (most recent call last):\n  File "/tmp/ansible_zfnekcpe/ansible_module_docker_login.py", line 334, in <module>\n    main()\n  File "/tmp/ansible_zfnekcpe/ansible_module_docker_login.py", line 325, in main\n    LoginManager(client, results)\n  File "/tmp/ansible_zfnekcpe/ansible_module_docker_login.py", line 168, in __init__\n    self.login()\n  File "/tmp/ansible_zfnekcpe/ansible_module_docker_login.py", line 203, in login\n    self.update_config_file()\n  File "/tmp/ansible_zfnekcpe/ansible_module_docker_login.py", line 280, in update_config_file\n    auth=base64.b64encode(self.username + b\':\' + self.password),\nTypeError: Can\'t convert \'bytes\' object to str implicitly\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 1\r\n')
fatal: [94.130.11.31]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 19: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 25\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\nTraceback (most recent call last):\n  File \"/tmp/ansible_zfnekcpe/ansible_module_docker_login.py\", line 334, in <module>\n    main()\n  File \"/tmp/ansible_zfnekcpe/ansible_module_docker_login.py\", line 325, in main\n    LoginManager(client, results)\n  File \"/tmp/ansible_zfnekcpe/ansible_module_docker_login.py\", line 168, in __init__\n    self.login()\n  File \"/tmp/ansible_zfnekcpe/ansible_module_docker_login.py\", line 203, in login\n    self.update_config_file()\n  File \"/tmp/ansible_zfnekcpe/ansible_module_docker_login.py\", line 280, in update_config_file\n    auth=base64.b64encode(self.username + b':' + self.password),\nTypeError: Can't convert 'bytes' object to str implicitly\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 1\r\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
<> (1, '', 'OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 19: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 19\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\nTraceback (most recent call last):\n  File "/tmp/ansible_zzkf8ez8/ansible_module_docker_login.py", line 334, in <module>\n    main()\n  File "/tmp/ansible_zzkf8ez8/ansible_module_docker_login.py", line 325, in main\n    LoginManager(client, results)\n  File "/tmp/ansible_zzkf8ez8/ansible_module_docker_login.py", line 168, in __init__\n    self.login()\n  File "/tmp/ansible_zzkf8ez8/ansible_module_docker_login.py", line 203, in login\n    self.update_config_file()\n  File "/tmp/ansible_zzkf8ez8/ansible_module_docker_login.py", line 280, in update_config_file\n    auth=base64.b64encode(self.username + b\':\' + self.password),\nTypeError: Can\'t convert \'bytes\' object to str implicitly\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 1\r\n')
<> (1, '', 'OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016\r\ndebug1: Reading configuration data /etc/ssh/ssh_config\r\ndebug1: /etc/ssh/ssh_config line 19: Applying options for *\r\ndebug1: auto-mux: Trying existing master\r\ndebug2: fd 3 setting O_NONBLOCK\r\ndebug2: mux_client_hello_exchange: master version 4\r\ndebug3: mux_client_forwards: request forwardings: 0 local, 0 remote\r\ndebug3: mux_client_request_session: entering\r\ndebug3: mux_client_request_alive: entering\r\ndebug3: mux_client_request_alive: done pid = 22\r\ndebug3: mux_client_request_session: session request sent\r\ndebug1: mux_client_request_session: master session id: 2\r\nTraceback (most recent call last):\n  File "/tmp/ansible_112dub7t/ansible_module_docker_login.py", line 334, in <module>\n    main()\n  File "/tmp/ansible_112dub7t/ansible_module_docker_login.py", line 325, in main\n    LoginManager(client, results)\n  File "/tmp/ansible_112dub7t/ansible_module_docker_login.py", line 168, in __init__\n    self.login()\n  File "/tmp/ansible_112dub7t/ansible_module_docker_login.py", line 203, in login\n    self.update_config_file()\n  File "/tmp/ansible_112dub7t/ansible_module_docker_login.py", line 280, in update_config_file\n    auth=base64.b64encode(self.username + b\':\' + self.password),\nTypeError: Can\'t convert \'bytes\' object to str implicitly\ndebug3: mux_client_read_packet: read header failed: Broken pipe\r\ndebug2: Received exit status from master 1\r\n')

# Fixed code
def update_config_file(self):
    """
    If the authorization not stored in the config file or reauthorize is True,
    update the config file with the new authorization.

    :return: None
    """

    path = os.path.expanduser(self.config_path)
    if not self.config_file_exists(path):
        self.create_config_file(path)

    try:
        # read the existing config
        config = json.load(open(path, "r"))
    except ValueError:
        self.log("Error reading config from %s" % (path))
        config = dict()

    if not config.get("auths"):
        self.log("Adding auths dict to config.")
        config["auths"] = dict()

    if not config["auths"].get(self.registry_url):
        self.log("Adding registry_url %s to auths." % (self.registry_url))
        config["auths"][self.registry_url] = dict()

    b64auth = base64.b64encode(to_bytes(self.username) + b":" + to_bytes(self.password))
    auth = to_text(b64auth)

    encoded_credentials = dict(auth=auth, email=self.email)

    if config["auths"][self.registry_url] != encoded_credentials or self.reauthorize:
        # Update the config file with the new authorization
        config["auths"][self.registry_url] = encoded_credentials
        self.log(
            "Updating config file %s with new authorization for %s"
            % (path, self.registry_url)
        )
        self.results["actions"].append(
            "Updated config file %s with new authorization for %s"
            % (path, self.registry_url)
        )
        self.results["changed"] = True
        self.write_config(path, config)