# How to skip the task by checking the file exists or not using when

**URL:** <https://forum.ansible.com/t/how-to-skip-the-task-by-checking-the-file-exists-or-not-using-when/20688>\
**Category:** Ansible Project\
**Created:** [February 26, 2016, 2:58pm UTC](https://forum.ansible.com/t/how-to-skip-the-task-by-checking-the-file-exists-or-not-using-when/20688 "2016-02-26T14:58:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dinesh\_E](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@Dinesh\_E](https://forum.ansible.com/u/Dinesh_E)\
**Post date:** [February 26, 2016, 2:58pm UTC](https://forum.ansible.com/t/how-to-skip-the-task-by-checking-the-file-exists-or-not-using-when/20688/1 "2016-02-26T14:58:14Z")

</div>

I have to check whether a file exists or not in /etc/. If the file exists then I have to skip the task otherwise the task should be run. Below is the code

- name: check whether the file exists  
command: touch /etc/file.txt  
when: $(! -s /etc/file.txt)

If the file exists then task should be skipped other wise run the task

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 26, 2016, 8:53pm UTC](https://forum.ansible.com/t/how-to-skip-the-task-by-checking-the-file-exists-or-not-using-when/20688/2 "2016-02-26T20:53:25Z")

</div>

use stat module and make a condition on the registered result, when won’t execute a shell, a lookup could be used but they run on ‘master’ not on target.
