On Sunday, May 6, 2018 at 10:38:14 AM UTC-4, Karl Auer wrote:
>
> How does "~" differ from "+"?
>
>
> "{{ 'hello' ~ name ~ 'there!' }}"
> vs
> "{{ 'hello' + name + 'there!' }}"
>
> ?
>

Since this is an incredibly common question, I've written up a short 
explanation with examples:
https://immensefail.tumblr.com/post/173645339395/jinja-faq-concatenation

While they’re often interchangeable, developing a habit of using the 
concatenation operator means that you don’t have to worry about or code 
around the situations where they’re not.


TASK [Addition isn't concatenation] ********************************************
ok: [localhost] => {
    "13 + 37": "50"
}

TASK [Concatenation is concatenation] ******************************************
ok: [localhost] => {
    "13 ~ 37": "1337"
}

TASK [Concatenation handles mixed types] ***************************************
ok: [localhost] => {
    "13 ~ '37'": "1337"
}

TASK [It's possible to do the coercion manually, but who wants to do that?] ****
ok: [localhost] => {
    "13 | string + '37'": "1337"
}

TASK [Addition doesn't like mixed types] ***************************************
fatal: [localhost]: FAILED! => {"msg": "Unexpected templating type error 
occurred on ({{13 + '37'}}): unsupported operand type(s) for +: 'int' and 
'str'"}

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/70403e1f-e1ee-4e56-a87d-ec1251eaf9bf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to