# Buggy code
def virtualenv_ver(venv_bin, user=None, **kwargs):
    """
    return virtualenv version if exists
    """
    # Virtualenv package
    try:
        import virtualenv

        version = getattr(virtualenv, "__version__", None)
        if not version:
            version = virtualenv.virtualenv_version
        virtualenv_version_info = tuple(
            [int(i) for i in version.split("rc")[0].split(".")]
        )
    except ImportError:
        # Unable to import?? Let's parse the version from the console
        version_cmd = [venv_bin, "--version"]
        ret = __salt__["cmd.run_all"](
            version_cmd, runas=user, python_shell=False, **kwargs
        )
        if ret["retcode"] > 0 or not ret["stdout"].strip():
            raise CommandExecutionError(
                "Unable to get the virtualenv version output using '{0}'. "
                "Returned data: {1}".format(version_cmd, ret)
            )
        # 20.0.0 virtualenv changed the --version output. find version number
        ver = "".join(
            [x for x in ret["stdout"].strip().split() if re.search(r"^\d.\d*", x)]
        )
        virtualenv_version_info = tuple(
            [int(i) for i in ret["stdout"].strip().split("rc")[0].split(".")]
        )
    return virtualenv_version_info


# Traceback (ModuleNotFoundError)
==> amazon-ebs: [INFO    ] Running state [/project/.venv] at time 22:43:04.915533
==> amazon-ebs: [INFO    ] Executing state virtualenv.managed for [/project/.venv]
==> amazon-ebs: [INFO    ] Executing command ['/usr/local/bin/virtualenv', '--version'] as user 'ubuntu' in directory '/home/ubuntu'
==> amazon-ebs: [DEBUG   ] env command: ('su', '-s', '/bin/bash', '-', 'ubuntu', '-c', '/usr/bin/python3')
==> amazon-ebs: [DEBUG   ] stderr: virtualenv 20.0.4 from /usr/local/lib/python2.7/dist-packages/virtualenv/__init__.pyc
==> amazon-ebs: [DEBUG   ] An exception occurred in this state: Unable to get the virtualenv version output using '['/usr/local/bin/virtualenv', '--version']'. Returned data: {'pid': 13344, 'retcode': 0, 'stdout': '', 'stderr': 'virtualenv 20.0.4 from /usr/local/lib/python2.7/dist-packages/virtualenv/__init__.pyc'}
==> amazon-ebs: Traceback (most recent call last):
==> amazon-ebs:   File "/usr/lib/python3/dist-packages/salt/modules/virtualenv_mod.py", line 161, in create
==> amazon-ebs:     import virtualenv
==> amazon-ebs: ModuleNotFoundError: No module named 'virtualenv'
==> amazon-ebs:
==> amazon-ebs: During handling of the above exception, another exception occurred:
==> amazon-ebs:
==> amazon-ebs: Traceback (most recent call last):
==> amazon-ebs:   File "/usr/lib/python3/dist-packages/salt/state.py", line 1933, in call
==> amazon-ebs:     **cdata['kwargs'])
==> amazon-ebs:   File "/usr/lib/python3/dist-packages/salt/loader.py", line 1951, in wrapper
==> amazon-ebs:     return f(*args, **kwargs)
==> amazon-ebs:   File "/usr/lib/python3/dist-packages/salt/states/virtualenv_mod.py", line 209, in managed
==> amazon-ebs:     **kwargs
==> amazon-ebs:   File "/usr/lib/python3/dist-packages/salt/modules/virtualenv_mod.py", line 176, in create
==> amazon-ebs:     'Returned data: {1}'.format(version_cmd, ret)
==> amazon-ebs: salt.exceptions.CommandExecutionError: Unable to get the virtualenv version output using '['/usr/local/bin/virtualenv', '--version']'. Returned data: {'pid': 13344, 'retcode': 0, 'stdout': '', 'stderr': 'virtualenv 20.0.4 from /usr/local/lib/python2.7/dist-packages/virtualenv/__init__.pyc'}
==> amazon-ebs: [ERROR   ] An exception occurred in this state: Traceback (most recent call last):
==> amazon-ebs:   File "/usr/lib/python3/dist-packages/salt/modules/virtualenv_mod.py", line 161, in create
==> amazon-ebs:     import virtualenv
==> amazon-ebs: ModuleNotFoundError: No module named 'virtualenv'
==> amazon-ebs:
==> amazon-ebs: During handling of the above exception, another exception occurred:
==> amazon-ebs:
==> amazon-ebs: Traceback (most recent call last):
==> amazon-ebs:   File "/usr/lib/python3/dist-packages/salt/state.py", line 1933, in call
==> amazon-ebs:     **cdata['kwargs'])
==> amazon-ebs:   File "/usr/lib/python3/dist-packages/salt/loader.py", line 1951, in wrapper
==> amazon-ebs:     return f(*args, **kwargs)
==> amazon-ebs:   File "/usr/lib/python3/dist-packages/salt/states/virtualenv_mod.py", line 209, in managed
==> amazon-ebs:     **kwargs
==> amazon-ebs:   File "/usr/lib/python3/dist-packages/salt/modules/virtualenv_mod.py", line 176, in create
==> amazon-ebs:     'Returned data: {1}'.format(version_cmd, ret)
==> amazon-ebs: salt.exceptions.CommandExecutionError: Unable to get the virtualenv version output using '['/usr/local/bin/virtualenv', '--version']'. Returned data: {'pid': 13344, 'retcode': 0, 'stdout': '', 'stderr': 'virtualenv 20.0.4 from /usr/local/lib/python2.7/dist-packages/virtualenv/__init__.pyc'}
==> amazon-ebs:
==> amazon-ebs: [INFO    ] Completed state [/project/.venv] at time 22:43:05.357418 (duration_in_ms=441.883)

# Fixed code
def virtualenv_ver(venv_bin, user=None, **kwargs):
    """
    return virtualenv version if exists
    """
    # Virtualenv package
    try:
        import virtualenv

        version = getattr(virtualenv, "__version__", None)
        if not version:
            version = virtualenv.virtualenv_version
        virtualenv_version_info = tuple(
            [int(i) for i in version.split("rc")[0].split(".")]
        )
    except ImportError:
        # Unable to import?? Let's parse the version from the console
        version_cmd = [venv_bin, "--version"]
        ret = __salt__["cmd.run_all"](
            version_cmd, runas=user, python_shell=False, redirect_stderr=True, **kwargs
        )
        if ret["retcode"] > 0 or not ret["stdout"].strip():
            raise CommandExecutionError(
                "Unable to get the virtualenv version output using '{0}'. "
                "Returned data: {1}".format(version_cmd, ret)
            )
        # 20.0.0 virtualenv changed the --version output. find version number
        ver = "".join(
            [x for x in ret["stdout"].strip().split() if re.search(r"^\d.\d*", x)]
        )
        virtualenv_version_info = tuple([int(i) for i in ver.split("rc")[0].split(".")])
    return virtualenv_version_info