# Buggy code
def _get_csr_domains(self):
    """
    Parse the CSR and return the list of requested domains
    """
    openssl_csr_cmd = [self._openssl_bin, "req", "-in", self.csr, "-noout", "-text"]
    _, out, _ = self.module.run_command(openssl_csr_cmd, check_rc=True)

    domains = set([])
    common_name = re.search(r"Subject:.*? CN\s?=\s?([^\s,;/]+)", out.decode("utf8"))
    if common_name is not None:
        domains.add(common_name.group(1))
    subject_alt_names = re.search(
        r"X509v3 Subject Alternative Name: \n +([^\n]+)\n",
        out.decode("utf8"),
        re.MULTILINE | re.DOTALL,
    )
    if subject_alt_names is not None:
        for san in subject_alt_names.group(1).split(", "):
            if san.startswith("DNS:"):
                domains.add(san[4:])
    return domains


# Traceback (KeyError)
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py", line 812, in <module>
main()
File "/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py", line 802, in main
client = ACMEClient(module)
File "/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py", line 521, in __init__
self.account = ACMEAccount(module)
File "/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py", line 316, in __init__
self.directory = ACMEDirectory(module)
File "/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py", line 289, in __init__
self.directory = simple_get(self.module, self.directory_root)
File "/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py", line 191, in simple_get
if info['content-type'].startswith('application/json'):
KeyError: 'content-type'

fatal: [default]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py\", line 812, in <module>\n    main()\n  File \"/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py\", line 802, in main\n    client = ACMEClient(module)\n  File \"/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py\", line 521, in __init__\n    self.account = ACMEAccount(module)\n  File \"/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py\", line 316, in __init__\n    self.directory = ACMEDirectory(module)\n  File \"/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py\", line 289, in __init__\n    self.directory = simple_get(self.module, self.directory_root)\n  File \"/tmp/ansible_f44oyk38/ansible_module_letsencrypt.py\", line 191, in simple_get\n    if info['content-type'].startswith('application/json'):\nKeyError: 'content-type'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def _get_csr_domains(self):
    """
    Parse the CSR and return the list of requested domains
    """
    openssl_csr_cmd = [self._openssl_bin, "req", "-in", self.csr, "-noout", "-text"]
    _, out, _ = self.module.run_command(openssl_csr_cmd, check_rc=True)

    domains = set([])
    common_name = re.search(
        r"Subject:.*? CN\s?=\s?([^\s,;/]+)", to_text(out, errors="surrogate_or_strict")
    )
    if common_name is not None:
        domains.add(common_name.group(1))
    subject_alt_names = re.search(
        r"X509v3 Subject Alternative Name: \n +([^\n]+)\n",
        to_text(out, errors="surrogate_or_strict"),
        re.MULTILINE | re.DOTALL,
    )
    if subject_alt_names is not None:
        for san in subject_alt_names.group(1).split(", "):
            if san.startswith("DNS:"):
                domains.add(san[4:])
    return domains