How to remove trailing whitespaces from string in Jinja? - jinja2

In my case all the strings are single-worded and the trailing whitespaces from the strings need to be removed, e.g., 'hello '→ 'hello'; 'hello '→ 'hello'.
One approach could be using split, i.e,
{% if ' ' in word %}
{% set word = word.split(' ')[0] %}
{% endif %}
This works since the strings are one-worded. I am quite sure there must be a better approach.

Silly me! It is .strip(), i.e., 'hello '.strip().

It's not .strip() ... it's | trim . Such as {{ some_variable_string_name | trim }}

you can simply iterate though your list and remove the trailing spaces. by default split() function has whitespaces.
data=['hello ','hello ',...]
new_data=[]
for i in data:
new_data.append(i.split())

Related

How to pass variable while calling macros in post hook?

Currently I am calling macros in such a way:
"post_hook":["{{macros_append('string1','string2')}}"]})}}
I want to call it as
"post_hook":["{{macros_append(var1,var2)}}"]})}}
I have already tried setting variable before config like
{% set var1='value' %}
"post_hook":["{{macros_append(var1,var2)}}"]})}}
But this does not work. It does not take any value while calling macros.
This doesn't work because dbt is parsing the jinja of your post-hook in a different context from the model file. In short, the post-hook needs to stand on its own.
This is true of all configs. The hints are:
post_hook takes a list of strings. Those strings later get templated by the jinja templater. (this is why you quote them and nest the curlies! you should never really nest curlies otherwise)
Configs can also get passed in through .yml files, etc., which is partially why the templating is deferred
Your question omits the actual call to the config macro, which makes this a little more clear:
{{
config({
"post_hook": ["{{macros_append('string1','string2')}}"]
})
}}
So what are we to do? You could use jinja to build the string that gets passed into the config block. This is hacky and ugly, but it works:
(Note that ~ is the jinja string concatenation operator.)
{% set var1 = "string1" %}
{% set var2 = "string2" %}
{{
config({
"post_hook": ["{{ macros_append(" ~ var1 ~ "," ~ var2 ~ ") }}"]
})
}}
A slightly cleaner version of this would be to define the whole macro call in a variable, so you don't have to do the concatenation:
{% set my_hook = "{{ macros_append('string1', 'string2') }}" %}
{{
config({
"post_hook": [my_hook]
})
}}
A Better Way
Another option is to use the var() macro, which allows you to access a global variable in the jinja context. You define these global variables in your dbt_project.yml file:
...
vars:
var1: string1
var2: string2
and then you can access them with {{ var('var1') }} from any process that is templating jinja. In the case of your config block, that would look like:
{{
config({
"post_hook": ["{{ macros_append(var('var1'), var('var2')) }}"]
})
}}
Note that the post-hook here is just a string that contains the string "var('var1')", but that's fine, since the templater will fill that in later, when the string is templated.

Jinja2 lowercase only bool values

I have a dictionary of values, some of the values are integer, some are strings, some are boolean. In my jinja2 template I currently use this:
{% for key, value in account.properties.items() %}
<property key="{{ key }}" value="{{ value|string }}"/>
{% endfor %}
The problem is, that I want boolean values to be all lowercase, everything else should stay normal. Right now I get False and True instead of false and true.
I also tried with to_json, but then the string values have quotes and integer and bool doesn't, so for above example I have then ""string"" which is also something I don't want.
I now use to_json and replace to remove quotes. Still this could be a potential issue for strings with escaped quotes I guess, e.g. `"foo\"bar". But for now I'm not aware to have those.
value="{{ value | to_json | replace("\"", "") }}"

Call a constructed variable name in Jinja2 template?

Is there a way in Jinja2 to construct a variable name and then call it? I want to do something like this:
{% for type in ('Students', 'Faculty', 'Groups') %}
{% set import_name = 'latest_' + type|lower + '_import' %}
{{ type }}: {{ import_name.created_at }}
{% endfor %}
I would expect the output to be something like this:
Students: 5/26/2016
Faculty: 5/25/2016
Groups: 5/25/2016
I have the variables latest_students_import, latest_faculty_import, and latest_groups_import set in the template scope, and would like to avoid having a large conditional in my for loop. I set import_name based on the type, and then try to "call" import_name. I want something like {{ call(import_name) }}. Is this possible, or is there another way I can go about this?
In this case, I suppose I could do it in reverse order loop through the three template variable names, and then "print" the shortened name, capitalized, but I would prefer to do it this way.
One possibility is to create a dict or a list on the server-side which contains your variables. You can then send that object to Jinja as a template variable. As it stands you are just setting import_name equal to string, which won't have the .created_at attribute.

How to grab a string before hyphen using Jinja2?

My variable is as such:
api-20150901r1_6.38
Using Jinja, I need a way to grab the string BEFORE the hyphen. There will always be one hyphen so I dont need to worry about multiple instances.
Assuming input_string is api-20150901r1_6.38
You can use set
{% set mylist = input_string.split('-') %}
This is your value "api" in this case
{{ mylist[0] }}

Creating data-structures for ansible templates

I'm trying to create a simple config file that enumerates all the (hostname, ip_address) pairs as part of an ansible task. What I'd really like to do is something like this (using ansible's global datastructures groups and hostvars):
def grouped_hosts():
ret = {}
for group in groups:
ret[group] = {}
for host in groups[group]:
ret[group][host] = hostvars[host]['ansible_eth0']['ipv4']['address']
return json.dumps(ret)
Which would emit a data structure similar to:
{"webservers":{"web0":"1.2.3.4","web1":"1.2.3.5"},{"caches":{"cache0":"1.2.3.6"}}}
However, I don't know how to build and pass this data structure to my jinja2 template. I really want to be able to create that datastructure and just put {{ grouped_hosts()|to_nice_json }} and call it a day. But how do I write, and where do I put, that grouped_hosts() function?
I'm not sure what you're trying to create with your template, but if you just want to output this as a json structure, you can do it this way :
{
{% set gdelim = '' %}
{% for group in groups %}
{{ gdelim }}"{{group}}": {
{% set hdelim = '' %}
{% for host in groups[group] %}
{{ hdelim }}"{{ host }}": "{{hostvars[host]['ansible_eth0']['ipv4']['address']}}"
{% set hdelim = ',' %}
{% endfor %}
}
{% set gdelim = ',' %}
{% endfor %}
}
The gdelim and hdelim are here to set the delimiter when required (note the delimiters prefixing objects).
At first run, the delimiter is empty, and then ",". Since the objects are prefixed by the delimiter, you don't have trailing comas, and thus the resulting JSON is valid (but a bit ugly).