Skip to content

Commit

Permalink
Fix ansible-core version detection (#1417)
Browse files Browse the repository at this point in the history
* Fix ansible-core version detection.

* Fix error.

* Linting.
  • Loading branch information
felixfontein authored Mar 2, 2021
1 parent 1e23f79 commit a31bf5a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/ansiblelint/_prerun.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

from packaging import version

from ansiblelint.config import ansible_collections_path, collection_list, options
from ansiblelint.config import (
ansible_collections_path,
collection_list,
options,
parse_ansible_version,
)
from ansiblelint.constants import (
ANSIBLE_DEFAULT_ROLES_PATH,
ANSIBLE_MIN_VERSION,
Expand Down Expand Up @@ -45,10 +50,9 @@ def _get_ver_err() -> Tuple[str, str]:
"FATAL: Unable to retrieve ansible cli version: %s" % result.stdout,
)

match = re.match(r"^ansible ([^\s]+)", result.stdout)
if not match:
return ver, "FATAL: Unable parse ansible cli version: %s" % result.stdout
ver = match.group(1)
ver, error = parse_ansible_version(result.stdout)
if error is not None:
return "", error
try:
# pylint: disable=import-outside-toplevel
from ansible.release import __version__ as ansible_module_version
Expand Down
21 changes: 19 additions & 2 deletions src/ansiblelint/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Store configuration options as a singleton."""
import os
import re
import subprocess
import sys
from argparse import Namespace
from functools import lru_cache
from typing import Dict, List
from typing import Dict, List, Optional, Tuple

from packaging.version import Version

Expand Down Expand Up @@ -78,6 +79,19 @@ def ansible_collections_path() -> str:
return "ANSIBLE_COLLECTIONS_PATHS"


def parse_ansible_version(stdout: str) -> Tuple[str, Optional[str]]:
"""Parse output of 'ansible --version'."""
# ansible-core 2.11+: 'ansible [core 2.11.3]'
match = re.match(r"^ansible \[(?:core|base) ([^\]]+)\]", stdout)
if match:
return match.group(1), None
# ansible-base 2.10 and Ansible 2.9: 'ansible 2.x.y'
match = re.match(r"^ansible ([^\s]+)", stdout)
if match:
return match.group(1), None
return "", "FATAL: Unable parse ansible cli version: %s" % stdout


@lru_cache()
def ansible_version(version: str = "") -> Version:
"""Return current Version object for Ansible.
Expand All @@ -95,7 +109,10 @@ def ansible_version(version: str = "") -> Version:
stderr=subprocess.PIPE,
)
if proc.returncode == 0:
version = proc.stdout.splitlines()[0].split()[1]
version, error = parse_ansible_version(proc.stdout)
if error is not None:
print(error)
sys.exit(ANSIBLE_MISSING_RC)
else:
print(
"Unable to find a working copy of ansible executable.",
Expand Down

0 comments on commit a31bf5a

Please sign in to comment.