# Buggy code
def export_key(
    keyids=None, secret=False, user=None, gnupghome=None, use_passphrase=False
):
    """
    Export a key from the GPG keychain

    keyids
        The key ID(s) of the key(s) to be exported. Can be specified as a comma
        separated string or a list. Anything which GnuPG itself accepts to
        identify a key - for example, the key ID or the fingerprint could be
        used.

    secret
        Export the secret key identified by the ``keyids`` information passed.

    user
        Which user's keychain to access, defaults to user Salt is running as.
        Passing the user as ``salt`` will set the GnuPG home directory to the
        ``/etc/salt/gpgkeys``.

    gnupghome
        Specify the location where GPG keyring and related files are stored.

    use_passphrase
        Whether to use a passphrase with the signing key. Passphrase is received
        from Pillar.

    CLI Example:

    .. code-block:: bash

        salt '*' gpg.export_key keyids=3FAD9F1E

        salt '*' gpg.export_key keyids=3FAD9F1E secret=True

        salt '*' gpg.export_key keyids="['3FAD9F1E','3FBD8F1E']" user=username

    """
    gpg = _create_gpg(user, gnupghome)

    if isinstance(keyids, str):
        keyids = keyids.split(",")

    if use_passphrase:
        gpg_passphrase = __salt__["pillar.get"]("gpg_passphrase")
        if not gpg_passphrase:
            raise SaltInvocationError("gpg_passphrase not available in pillar.")
        ret = gpg.export_keys(keyids, secret, passphrase=gpg_passphrase)
    else:
        ret = gpg.export_keys(keyids, secret, expect_passphrase=False)
    return ret


# Traceback (ValueError)
$ salt-ssh 'X' gpg.export_key keyids='X' user=X secret=True
X
----------
retcode:
1
stderr:
Traceback (most recent call last):
File "/var/tmp/.conf_7c7bca_salt/salt-call", line 27, in <module>
salt_call()
File "/var/tmp/.conf_7c7bca_salt/pyall/salt/scripts.py", line 449, in salt_call
client.run()
File "/var/tmp/.conf_7c7bca_salt/pyall/salt/cli/call.py", line 58, in run
caller.run()
File "/var/tmp/.conf_7c7bca_salt/pyall/salt/cli/caller.py", line 112, in run
ret = self.call()
File "/var/tmp/.conf_7c7bca_salt/pyall/salt/cli/caller.py", line 220, in call
self.opts, data, func, args, kwargs
File "/var/tmp/.conf_7c7bca_salt/pyall/salt/executors/direct_call.py", line 12, in execute
return func(*args, **kwargs)
File "/var/tmp/.conf_7c7bca_salt/pyall/salt/modules/gpg.py", line 807, in export_key
return gpg.export_keys(keyids, secret)
File "/usr/lib/python3/dist-packages/gnupg.py", line 1260, in export_keys
raise ValueError('For GnuPG >= 2.1, exporting secret keys '
ValueError: For GnuPG >= 2.1, exporting secret keys needs a passphrase to be provided
stdout:

# Fixed code
def export_key(
    keyids=None, secret=False, user=None, gnupghome=None, use_passphrase=False
):
    """
    Export a key from the GPG keychain

    keyids
        The key ID(s) of the key(s) to be exported. Can be specified as a comma
        separated string or a list. Anything which GnuPG itself accepts to
        identify a key - for example, the key ID or the fingerprint could be
        used.

    secret
        Export the secret key identified by the ``keyids`` information passed.

    user
        Which user's keychain to access, defaults to user Salt is running as.
        Passing the user as ``salt`` will set the GnuPG home directory to the
        ``/etc/salt/gpgkeys``.

    gnupghome
        Specify the location where GPG keyring and related files are stored.

    use_passphrase
        Whether to use a passphrase with the signing key. Passphrase is received
        from Pillar.

        .. versionadded: 3003

    CLI Example:

    .. code-block:: bash

        salt '*' gpg.export_key keyids=3FAD9F1E

        salt '*' gpg.export_key keyids=3FAD9F1E secret=True

        salt '*' gpg.export_key keyids="['3FAD9F1E','3FBD8F1E']" user=username

    """
    gpg = _create_gpg(user, gnupghome)

    if isinstance(keyids, str):
        keyids = keyids.split(",")

    if use_passphrase:
        gpg_passphrase = __salt__["pillar.get"]("gpg_passphrase")
        if not gpg_passphrase:
            raise SaltInvocationError("gpg_passphrase not available in pillar.")
        ret = gpg.export_keys(keyids, secret, passphrase=gpg_passphrase)
    else:
        ret = gpg.export_keys(keyids, secret, expect_passphrase=False)
    return ret