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
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 }}”
vbotka
(Vladimir Botka)
3
Try this
- name: making file backup as vfstab.bkp in /tmp
copy:
remote_src: true
src: "{{ (ansible_os_family == 'Solaris')|
ternary('/etc/vfstab', '/etc/fstab') }}"
dest: "{{ (ansible_os_family == 'Solaris')|
ternary('/tmp/vfstab.bkp', '/tmp/fstab.bkp') }}"
loop: "{{ mounts.results }}"
when:
- item.changed|bool
- 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
For example, copy backup if any result has been changed
when: mounts.results|json_query('.changed') is any
HTH,
-vlado