From 454da7d603c61e2353103184c62873df1b4a50a2 Mon Sep 17 00:00:00 2001 From: Rhys Campbell Date: Thu, 25 Jul 2024 09:35:05 +0200 Subject: [PATCH] Update include_vars function --- .../molecule/default/tests/test_default.py | 23 +++++++++++++++++-- .../molecule/default/tests/test_default.py | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/roles/mongodb_auth/molecule/default/tests/test_default.py b/roles/mongodb_auth/molecule/default/tests/test_default.py index 7d600622d..83ace491c 100644 --- a/roles/mongodb_auth/molecule/default/tests/test_default.py +++ b/roles/mongodb_auth/molecule/default/tests/test_default.py @@ -9,10 +9,29 @@ def include_vars(host): - ansible = host.ansible("include_vars", - 'file="../../defaults/main.yml"', + current_dir = os.path.dirname(os.path.abspath(__file__)) + path_components = current_dir.split(os.sep) + trim_count = 0 + # Weird bug where the path of this file is incorrect + # It seems the ansible module, at least when used here + # Used to run in a different directory, meaning the + # relative path was not correct. This method should mean it's + # always correct whatever the context. + for component in path_components: + if component.startswith("mongodb_"): + break + else: + trim_count += 1 + trim_count = (len(path_components) - 1) - trim_count + # Trim off the dirs after the role dir + trimmed_components = path_components[:-trim_count] + trimmed_path = os.sep.join(trimmed_components) + vars_file_path = os.path.join(trimmed_path, 'defaults', 'main.yml') + ansible = host.ansible('include_vars', + f'file="{vars_file_path}"', False, False) + # print(str(ansible)) return ansible diff --git a/roles/mongodb_repository/molecule/default/tests/test_default.py b/roles/mongodb_repository/molecule/default/tests/test_default.py index b6f4bc36c..85ca8d28c 100644 --- a/roles/mongodb_repository/molecule/default/tests/test_default.py +++ b/roles/mongodb_repository/molecule/default/tests/test_default.py @@ -30,7 +30,7 @@ def include_vars(host): f'file="{vars_file_path}"', False, False) - print(str(ansible)) + # print(str(ansible)) return ansible