2
votes

I would like to do the following in an Ansible playbook:

- name: "Generate variant html report"
  shell: "<my command>"
  with_items: "{{ variants | default([]) }}"
  when: ({{ folderReports }}/{{ item }}).exists == False

Inside the when, I need a way to create the file path, and determine if it exists or not. I tried to use the "(" ")" to surround my first expression, but it doesn't seem to be working:

2016-10-13 16:22:48,130 p=94292 u=sautomation | fatal: [localhost]: FAILED! => {"failed": true, "msg": "The conditional check '({{ folderReports }}/{{ item }}).exists == True' failed. The error was: template error while templating string: unexpected '/'. String: {% if (/opt/diff-test1.diff).exists == True %} True {% else %} False {% endif %}\n\nThe error appears to have been in '/opt/ansible/roles/stats/tasks/main.yml': line 45, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: \"Generate report\"\n ^ here\n"}

1

1 Answers

4
votes

To prevent a task from executing if a specified file already exists, use creates parameter of the shell module:

- name: "Generate variant html report"
  shell: "<my command>"
  args:
    creates: "{{ folderReports }}/{{ item }}"
  with_items: "{{ variants | default([]) }}"

You get an error because .exists, in your conditional, checks if a variable (fact) exist, not a file on the target node.