Assigning variable from jinja template evaluation

160 views
Skip to first unread message

a akin

unread,
Jul 30, 2016, 11:09:52 AM7/30/16
to Ansible Project
I am hoping someone would be able to point out what I'm doing wrong here-

I have a jinja template:
{% if subnet == 'subnet-ab1' %}
subnet1
{% elif subnet == "subnetab2' %}
subnet2
{% elif subnet == "subnetab3' %}
subnet3
{% elif subnet == "subnetab4' %}
subnet4
{% else %}
subnet5
{% endif %}

I am assigning the variable in the main playbook like this:

group_id: "{{ lookup('template', 'sec_grp.j2") }}"

But I am getting this error:

template error while templating string: expected token 'end of statement block', got 'subnet'


The thing is, the below works:
{% if subnet == 'subnet-ab1' %}
subnet1
{% endif %}


but I need more to add more lines.

Thanks.

Kai Stian Olstad

unread,
Jul 30, 2016, 11:33:42 AM7/30/16
to ansible...@googlegroups.com
On 30. juli 2016 17:09, a akin wrote:
> I am hoping someone would be able to point out what I'm doing wrong here-
>
> I have a jinja template:
> {% if subnet == 'subnet-ab1' %}
> subnet1
> {% elif subnet == "subnetab2' %}
> subnet2
> {% elif subnet == "subnetab3' %}
> subnet3
> {% elif subnet == "subnetab4' %}
> subnet4
> {% else %}
> subnet5
> {% endif %}

Your are mixing single and double quotes.

>
> I am assigning the variable in the main playbook like this:
>
> group_id: "{{ lookup('template', 'sec_grp.j2") }}"

Ditto.

--
Kai Stian Olstad

a akin

unread,
Jul 30, 2016, 11:41:40 AM7/30/16
to Ansible Project, ansible-pr...@olstad.com
I am sorry, those were typos:

template:
{% if subnet == 'subnet-ab1' %}
subnet1
{% elif subnet == 'subnetab2' %}
subnet2
{% elif subnet == 'subnetab3' %}
subnet3
{% elif subnet == 'subnetab4' %}
subnet4
{% else %}
subnet5
{% endif %}

playbook:

group_id: "{{ lookup('template', 'sec_grp.j2') }}"

Kai Stian Olstad

unread,
Jul 30, 2016, 12:21:46 PM7/30/16
to ansible...@googlegroups.com
On 30. juli 2016 17:41, a akin wrote:
> I am sorry, those were typos:
> template:
> {% if subnet == 'subnet-ab1' %}
> subnet1
> {% elif subnet == 'subnetab2' %}
> subnet2
> {% elif subnet == 'subnetab3' %}
> subnet3
> {% elif subnet == 'subnetab4' %}
> subnet4
> {% else %}
> subnet5
> {% endif %}
>
> playbook:
> group_id: "{{ lookup('template', 'sec_grp.j2') }}"

Testet the code in v2.1.1 and it's working for me.

--
Kai Stian Olstad

a akin

unread,
Jul 30, 2016, 5:52:09 PM7/30/16
to Ansible Project, ansible-pr...@olstad.com
Thank you for the quick response. I think I got the error to go away but I m facing a different issue now; I am using debug/msg to print the value of the variable and for some reason a newline character is being appended, for example, instead of just 'subnet1', I get  'subnet1\n'. Adding '#jinja2:trim_blocks: True, lstrip_blocks: True' doesn't help.

Kai Stian Olstad

unread,
Jul 31, 2016, 3:23:36 AM7/31/16
to ansible...@googlegroups.com
On 30. juli 2016 23:52, a akin wrote:
> Thank you for the quick response. I think I got the error to go away but I
> m facing a different issue now; I am using debug/msg to print the value of
> the variable and for some reason a newline character is being appended, for
> example, instead of just 'subnet1', I get 'subnet1\n'. Adding
> '#jinja2:trim_blocks: True, lstrip_blocks: True' doesn't help.

Most likely a bug, I haven't seen this problem with the template module.

As a workaround you can use the trim filter in Ansible.
group_id: "{{ lookup('template', 'sec_grp.j2') | trim }}"

--
Kai Stian Olstad

a akin

unread,
Jul 31, 2016, 9:12:28 PM7/31/16
to Ansible Project, ansible-pr...@olstad.com
Thanks, the trim filter worked.
Reply all
Reply to author
Forward
0 new messages