From f83a1736ad9fd802424ec89b84c136f67e7956e5 Mon Sep 17 00:00:00 2001 From: damcav35 <51324122+damcav35@users.noreply.github.com> Date: Mon, 5 Dec 2022 20:25:23 +0100 Subject: [PATCH] fix case when BASTION var is not a string (#19) * feat: resolve jinja vars from hostvars Currently the bastion-ansible-wrapper does not mananage jinja2 vars. Example group vars defining bastion_host: ``` "bastion_host": "{{ bastion_fqdn }}" ``` -> this will be interpreted as a litteral string This PR tries to resolve the (maybe intricated) jinja2 vars by looking at them in hostvars * fix: fix case when a BASTION var is not a string Co-authored-by: Damien Cavagnini --- lib.py | 6 +++++- tests.py | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib.py b/lib.py index d21617e..5e86ef2 100644 --- a/lib.py +++ b/lib.py @@ -174,7 +174,11 @@ def get_var_within(my_value, hostvar, check_list=None): if check_list is None: check_list = [] - if my_value.startswith("{{") and my_value.endswith("}}"): + if ( + isinstance(my_value, str) + and my_value.startswith("{{") + and my_value.endswith("}}") + ): # ex: {{ my_jinja2_var }} -> lookup for 'my_jinja2_var' in hostvars key_name = my_value.replace("{{", "").replace("}}", "").strip() diff --git a/tests.py b/tests.py index 47a3378..c389f02 100644 --- a/tests.py +++ b/tests.py @@ -89,3 +89,9 @@ def test_get_var_not_a_jinja2_var(): hostvars = {"bastion_host": "{{ bastion_fqdn"} bastion_host = get_var_within(hostvars["bastion_host"], hostvars) assert bastion_host == hostvars["bastion_host"] + + +def test_get_var_not_a_string(): + hostvars = {"bastion_host": 68} + bastion_host = get_var_within(hostvars["bastion_host"], hostvars) + assert bastion_host == hostvars["bastion_host"]