Skip to content

Commit

Permalink
Fix ansible version parsing (#3143)
Browse files Browse the repository at this point in the history
Reuse code from ansible-lint for reading ansible-version
and avoid bug where presence of ANSIBLE_DEBUG would
break version loading.
  • Loading branch information
ssbarnea authored Jun 5, 2021
1 parent cea1e79 commit 75c397d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 57 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ jobs:
- tox_env: lint
- tox_env: docs
- tox_env: py36
PREFIX: PYTEST_REQPASS=438
PREFIX: PYTEST_REQPASS=436
- tox_env: py37
PREFIX: PYTEST_REQPASS=438
PREFIX: PYTEST_REQPASS=436
- tox_env: py38
PREFIX: PYTEST_REQPASS=438
PREFIX: PYTEST_REQPASS=436
- tox_env: py39
PREFIX: PYTEST_REQPASS=438
PREFIX: PYTEST_REQPASS=436
- tox_env: py36-devel
PREFIX: PYTEST_REQPASS=438
PREFIX: PYTEST_REQPASS=436
- tox_env: py39-devel
PREFIX: PYTEST_REQPASS=438
PREFIX: PYTEST_REQPASS=436
- tox_env: packaging
- tox_env: eco
- tox_env: dockerfile
Expand Down
34 changes: 2 additions & 32 deletions src/molecule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
from typing import Callable, MutableMapping, TypeVar
from uuid import uuid4

from packaging.version import Version
from ansiblelint.config import ansible_version

from molecule import api, interpolation, platforms, scenario, state, util
from molecule.constants import RC_SETUP_ERROR
from molecule.dependency import ansible_galaxy, shell
from molecule.model import schema_v3
from molecule.provisioner import ansible
from molecule.util import boolean, lru_cache, run_command, sysexit
from molecule.util import boolean

LOG = logging.getLogger(__name__)
MOLECULE_DEBUG = boolean(os.environ.get("MOLECULE_DEBUG", "False"))
Expand Down Expand Up @@ -455,32 +454,3 @@ def set_env_from_file(env: MutableMapping[str, str], env_file: str) -> MutableMa
return env

return env


@lru_cache()
def ansible_version(version: str = "") -> Version:
"""Return current Version object for Ansible.
If version is not mentioned, it returns current version as detected.
When version argument is mentioned, it return converts the version string
to Version object in order to make it usable in comparisons.
"""
if not version:
proc = run_command(["ansible", "--version"], quiet=True)
if proc.returncode != 0:
LOG.fatal(
"Unable to find a working copy of ansible executable. Read https://molecule.readthedocs.io/en/latest/installation.html\n%s",
proc,
)
sysexit(RC_SETUP_ERROR)

# First line of the `ansible --version` output is:
#
# 1. `ansible <version>` on ansible < 2.10 and on ansible-base.
# 2. `ansible [core <version>]` on ansible-core.
#
# The code below grabs the last component in that line and strips trailing ] if
# present.
version = proc.stdout.splitlines()[0].split()[-1].rstrip("]")

return Version(version)
19 changes: 0 additions & 19 deletions src/molecule/test/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,22 +360,3 @@ def test_write_config(config_instance):
config_instance.write()

assert os.path.isfile(config_instance.config_file)


@pytest.mark.parametrize(
"input,output",
[
("ansible 2.9.18", "2.9.18"), # ansible < 2.10 and ansible-base
("ansible [core 2.11.0b1.post0]", "2.11.0b1.post0"), # ansible-core
],
)
def test_ansible_version_parsing(mocker, input, output):
run_command = mocker.patch.object(config, "run_command")
run_command.return_value.returncode = 0
run_command.return_value.stdout = input + "\nother stuff\n here\n"

# Results from ansible_version are cached, so we need to kill the cache before each
# test so that we can actually test parsing of different strings.
config.ansible_version.cache_clear()

assert output == str(config.ansible_version())

0 comments on commit 75c397d

Please sign in to comment.