# Syntax errors with Jinja2 control structures in tasks file

**URL:** <https://forum.ansible.com/t/syntax-errors-with-jinja2-control-structures-in-tasks-file/33096>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [June 4, 2020, 8:45pm UTC](https://forum.ansible.com/t/syntax-errors-with-jinja2-control-structures-in-tasks-file/33096 "2020-06-04T20:45:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Z](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Alexander\_Z](https://forum.ansible.com/u/Alexander_Z)\
**Post date:** [June 4, 2020, 8:45pm UTC](https://forum.ansible.com/t/syntax-errors-with-jinja2-control-structures-in-tasks-file/33096/1 "2020-06-04T20:45:26Z")

</div>

Hi Ansible community,

I am observing syntax error on attempt to use Jinja2 control structures in the tasks file:

**Code**  
/opt/ansible/roles/network/tasks/network.yml:

> {% if publicavailable|default(False) == False and public|default(“”) == “” and datacenter != “XXX” and datacenter != “YYY” %}  
> {% set private\_interface = ansible\_default\_ipv4.interface %}  
> {% else %}  
> {% set private\_interface = ansible\_default\_ipv4.interface|regex\_replace(‘\d+’,‘’)|string + (ansible\_default\_ipv4.interface|regex\_replace(‘\D+’,‘’)|int+1)|string %}  
> {% endif %}
> 
> - name: alias interface on private servers  
> template:  
> src: if\_internal\_eth.j2  
> dest: /etc/network/interfaces.d/if\_{{ private\_interface }}\_0.cfg  
> owner: root  
> group: root  
> mode: 0644  
> when: ‘datacenter != “XXX” and datacenter != “YYY” and vlan\_prefix2|default(“”) != “”’

**Error**

> fatal: [host.domain.tld]: FAILED! =\> {“reason”: "Syntax Error while loading YAML.  
> found character that cannot start any token  
> The error appears to have been in /opt/ansible/roles/network/tasks/network.yml: line 52, column 2, but may  
> be elsewhere in the file depending on the exact syntax problem.  
> The offending line appears to be:
> 
> {% if publicavailable|default(False) == False and public|default("") == "" and datacenter != "XXX" and datacenter != "YYY" %}  
> ^ here  
> "}

**Versions**

ansible 2.5.10  
python version = 2.7.12  
Ubuntu 16.04.3 LTS

Kindly advise!  
Thank you,  
Alex

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 4, 2020, 9:04pm UTC](https://forum.ansible.com/t/syntax-errors-with-jinja2-control-structures-in-tasks-file/33096/2 "2020-06-04T21:04:06Z")

</div>

That is because that is not a feature of Ansible.

Ansible parses the files using a YAML loader, then it does use jinja2  
internally for templating, but only on SOME of the values in the data  
structure returned by YAML.

---

<div class="post-metadata">

**Author:** ![Alexander\_Z](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Alexander\_Z](https://forum.ansible.com/u/Alexander_Z)\
**Post date:** [June 5, 2020, 2:53pm UTC](https://forum.ansible.com/t/syntax-errors-with-jinja2-control-structures-in-tasks-file/33096/3 "2020-06-05T14:53:15Z")

</div>

Hi Brian,

Thank you for the prompt reply.  
Is there is any documentation describing that limitation?  
For example it renders correctly some variables inside {{ … }} but does not allows to use control structures {% … %} to define those variables.  
Kindly advise!

Thank you,  
Alex
