Always append a '/' to variable whose value is supplied as extra vars

17 views
Skip to first unread message

ishan jain

unread,
Aug 29, 2016, 7:43:31 AM8/29/16
to Ansible Project
I have a variable config_src which i use in a lot many places (host vars, inventory file, playbook) and its value is supplied at the run time as extra vars (-e)
This variable is actually a path variable, so i need to ensure that it is always ending in a '/'. As this variable is in use in a lot of places, i want to make sure that a '/' is appended to it in a single place as applying some filter to all the places is not feasible. My inventory file is something that is common for all playbooks, so i think i should append the '/' somewhere in the inventory file only.
How can i do that ?

ayush...@similarweb.com

unread,
Aug 30, 2016, 9:19:58 PM8/30/16
to Ansible Project
One thing I could think of is instead of passing in config_src as the -e arg, pass in dummy_src and declare config_src = {{ dummy_src|some_filter }} as a variable somewhere that makes sense for your specific structure (probably a top level default variable based on your initial post).

It might not even be necessary. For file paths, multiple slashes are collapsed into one on most OSes I've tried.

ishan jain

unread,
Aug 31, 2016, 2:28:43 AM8/31/16
to Ansible Project
Thanks for the Idea ayush :-)

Custom filter seemed to much work as there are a lot of possibilities to cover. What i ended up doing is appending a '/' wherever the var is used, like - {{config_src}}/
This ensure that you have to give correct path to point inside the folder. Any extra '/' will be ignored of course.
Reply all
Reply to author
Forward
0 new messages