Skip to content

Commit

Permalink
fix case when BASTION var is not a string (#19)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
damcav35 and Damien Cavagnini authored Dec 5, 2022
1 parent e89c6ed commit f83a173
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 6 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit f83a173

Please sign in to comment.