a_akin
(a akin)
July 30, 2016, 3:09pm
1
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.
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.
a_akin
(a akin)
July 30, 2016, 3:41pm
3
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.
a_akin
(a akin)
July 30, 2016, 9:52pm
5
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 }}"
a_akin
(a akin)
August 1, 2016, 1:12am
7
Thanks, the trim filter worked.