# Jinja template error while using with copy module

**URL:** <https://forum.ansible.com/t/jinja-template-error-while-using-with-copy-module/32803>\
**Category:** Ansible Project\
**Created:** [April 28, 2020, 7:45pm UTC](https://forum.ansible.com/t/jinja-template-error-while-using-with-copy-module/32803 "2020-04-28T19:45:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [April 28, 2020, 7:45pm UTC](https://forum.ansible.com/t/jinja-template-error-while-using-with-copy-module/32803/1 "2020-04-28T19:45:14Z")

</div>

Hi All,

I would like to use jinja templates for conditions instead when. Is there anyways to fix the below errors with copy module?

- name: makeing file backup as vfstab.bkp in /tmp  
copy: |-  
{% if ansible\_os\_family == “Solaris” %}  
src=/etc/vfstab dest=/tmp/vfstab.bkp remote\_src=yes  
{%- else %}  
src=/etc/fstab dest=/tmp/fstab.bkp remote\_src=yes  
{%- endif %}  
when:
- item.changed == true
- item.stdout != “”  
with\_items: “{{ mounts.results }}”

Error Details:

fatal: [ngzerpandb01-dev]: FAILED! =\> {  
“msg”: “template error while templating string: Encountered unknown tag ‘else’… String: {%- else %}\n{%- endif %}”  
}  
fatal: [vmwopsapp04-stg]: FAILED! =\> {  
“msg”: “template error while templating string: Encountered unknown tag ‘else’… String: {%- else %}\n{%- endif %}”  
}  
fatal: [vmwopsapp12-prd]: FAILED! =\> {  
“msg”: “template error while templating string: Encountered unknown tag ‘else’… String: {%- else %}\n{%- endif %}”  
}

Regards,  
Suresh

---

<div class="post-metadata">

**Author:** ![Suresh\_Karpurapu](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Suresh\_Karpurapu](https://forum.ansible.com/u/Suresh_Karpurapu)\
**Post date:** [April 28, 2020, 7:58pm UTC](https://forum.ansible.com/t/jinja-template-error-while-using-with-copy-module/32803/2 "2020-04-28T19:58:40Z")

</div>

sorry guys, it worked for me in this way.

- name: makeing file backup as fstab.bkp in /tmp  
copy:  
src: |-  
{% if ansible\_os\_family == “Solaris” %}  
/etc/vfstab  
{%- else %}  
/etc/fstab  
{%- endif %}  
dest: /tmp/fstab.bkp  
remote\_src: yes  
when:
- item.changed == true
- item.stdout != “”  
with\_items: “{{ mounts.results }}”

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 28, 2020, 8:18pm UTC](https://forum.ansible.com/t/jinja-template-error-while-using-with-copy-module/32803/3 "2020-04-28T20:18:13Z")

</div>

Try this

- name: making file backup as vfstab.bkp in /tmp  
&nbsp;&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remote\_src: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ (ansible\_os\_family == 'Solaris')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ternary('/etc/vfstab', '/etc/fstab') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ (ansible\_os\_family == 'Solaris')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ternary('/tmp/vfstab.bkp', '/tmp/fstab.bkp') }}"  
&nbsp;&nbsp;&nbsp;loop: "{{ mounts.results }}"  
&nbsp;&nbsp;&nbsp;when:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- item.changed|bool  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- item.stdout|length \> 0

Why do you use the loop? The same copy shall be repeated when the  
conditions are met? Wouldn't probably the tests "any" or "all" fit the  
use-case better?  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_tests.html#test-if-a-list-contains-a-value](https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#test-if-a-list-contains-a-value)

For example, copy backup if any result has been changed

&nbsp;&nbsp;when: mounts.results|json\_query('.changed') is any

HTH,

&nbsp;&nbsp;-vlado
