title: Ansible include task only if file exists
body: <p>I'm trying to include a file only if it exists. This allows for custom "tasks/roles" between existing "tasks/roles" if needed by the user of my role. I found this:</p>
  
  <pre><code>- include: ...
    when: condition
  </code></pre>
  
  <p>But the Ansible docs state that:</p>
  
  <blockquote>
    <p>"All the tasks get evaluated, but the conditional is applied to each and every task" - <a href="http://docs.ansible.com/playbooks_conditionals.html#applying-when-to-roles-and-includes" rel="noreferrer">http://docs.ansible.com/playbooks_conditionals.html#applying-when-to-roles-and-includes</a></p>
  </blockquote>
  
  <p>So</p>
  
  <pre><code>- stat: path=/home/user/optional/file.yml
    register: optional_file
  - include: /home/user/optional/file.yml
    when: optional_file.stat.exists
  </code></pre>
  
  <p>Will fail if the file being included doesn't exist. I guess there might be another mechanism for allowing a user to add tasks to an existing recipe. I can't let the user to add a role after mine, because they wouldn't have control of the order: their role will be executed after mine.</p>