Skip to content

Commit

Permalink
always force galaxy to install collections (#164)
Browse files Browse the repository at this point in the history
ansible-galaxy 2.16 will always look in system paths, even if using
`ANSIBLE_COLLECTIONS_PATH`.  So if you have the `ansible` package installed,
galaxy will not install the collections used by the role in the .tox path
if the collections are provided by `ansible`.  Use `--force-with-deps` if
supported by `ansible-galaxy`, otherwise, use `--force`.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
  • Loading branch information
richm authored Apr 10, 2024
1 parent 7c93105 commit b3a4626
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/tox_lsr/test_scripts/runqemu.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,29 @@ def is_ansible_env_var_supported(env_var_name):
return False


def is_ansible_cmd_line_option_supported(cmd, option):
"""See if ansible or galaxy supports the given option --option."""
# cmd is a list e.g. ["ansible-galaxy", "collection", "install"]
result = subprocess.check_output( # nosec
cmd + ["--help"], stderr=subprocess.STDOUT, encoding="utf-8"
)
# look for name: ENV_VAR_NAME in output
match = re.search(r" {} ".format(option), result)
if match:
return True
return False


def get_galaxy_force_flag():
"""Get flag to use to force collection reinstall."""
cmd = ["ansible-galaxy", "collection", "install"]
if is_ansible_cmd_line_option_supported(cmd, "--force-with-deps"):
return "--force-with-deps"
if is_ansible_cmd_line_option_supported(cmd, "--force"):
return "--force"
return ""


if is_ansible_env_var_supported("ANSIBLE_COLLECTIONS_PATH"):
COLL_PATH_ENV_VAR = "ANSIBLE_COLLECTIONS_PATH"
else:
Expand Down Expand Up @@ -1165,7 +1188,6 @@ def run_ansible_playbooks( # noqa: C901
def install_requirements(sourcedir, collection_path, test_env, collection):
"""Install reqs from {meta,tests}/collection-requirements.yml, if any."""
collection_save_file = None
force_flag = None
if collection:
save_collection = os.path.join(
collection_path,
Expand All @@ -1188,7 +1210,6 @@ def install_requirements(sourcedir, collection_path, test_env, collection):
save_collection,
]
)
force_flag = "--force"
coll_rqf = os.path.join(sourcedir, "meta", "collection-requirements.yml")
tests_rqf = os.path.join(sourcedir, "tests", "collection-requirements.yml")
galaxy_env = {COLL_PATH_ENV_VAR: collection_path}
Expand All @@ -1198,14 +1219,13 @@ def install_requirements(sourcedir, collection_path, test_env, collection):
"ansible-galaxy",
"collection",
"install",
get_galaxy_force_flag(),
"-p",
collection_path,
"-vv",
"-r",
reqfile,
]
if force_flag:
ag_cmd.append(force_flag)
subprocess.check_call( # nosec
ag_cmd,
stdout=sys.stdout,
Expand Down Expand Up @@ -1254,6 +1274,7 @@ def setup_callback_plugins(pretty, profile, profile_task_limit, test_env):
"-p",
os.environ["LSR_TOX_ENV_TMP_DIR"],
"-vv",
get_galaxy_force_flag(),
"ansible.posix",
],
stdout=sys.stdout,
Expand Down

0 comments on commit b3a4626

Please sign in to comment.