# checking list of files to see if one exists

**URL:** <https://forum.ansible.com/t/checking-list-of-files-to-see-if-one-exists/38192>\
**Category:** Ansible Project\
**Created:** [July 5, 2023, 10:59am UTC](https://forum.ansible.com/t/checking-list-of-files-to-see-if-one-exists/38192 "2023-07-05T10:59:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [July 5, 2023, 10:59am UTC](https://forum.ansible.com/t/checking-list-of-files-to-see-if-one-exists/38192/1 "2023-07-05T10:59:16Z")

</div>

I have a list of 6 files; if any of them exist already, I need to delete it. If none of them exist just keep going. What I have below is giving me errors that ‘tmp\_files.stat.exists is false’. Here is what I have:

- name: Check to see if one file exists already

stat:  
path: “/work/{{ item }}”  
loop: “{{ possible\_file\_names }}”  
register: tmp\_files

- debug:  
msg: “file exists”  
when: tmp-file.exists

How do I “stop” the loop when a file exists? And if it doesn’t, keep going through the loop?

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 5, 2023, 11:26am UTC](https://forum.ansible.com/t/checking-list-of-files-to-see-if-one-exists/38192/2 "2023-07-05T11:26:59Z")

</div>

hi,

you want to delete those files, if they exist, right?

- name: delete file, if it exists

ansible.builtin.file:  
state: absent  
path: “/work/{{ item }}”  
loop:

- file1

- file2

- file3

- file4

- file5

- file6

- all loop items will be set into the disired state (absent).

- If that state is reached (file is ‘absent’) Ansible is happy and won’t do anything.

- If that state is not reached (file is not ‘absent’) Ansible will make sure the state is there (aka remove the file)

If you want to define those files elsewehere then in the playbook itself you can also create a variable and then just put the variable in the path line (where now {{ item }} is set

path: “/work/{{ file\_to remove }}”

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 5, 2023, 11:33am UTC](https://forum.ansible.com/t/checking-list-of-files-to-see-if-one-exists/38192/3 "2023-07-05T11:33:13Z")

</div>

the below example requires that your local user (with whom you are running the playbook) has sufficient privilegdes to remove files in your path location.  
If not, you’ll have to add

become: true  
become\_user: user\_with\_sufficient\_privileges

at the same level as loop

---

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [July 5, 2023, 11:35am UTC](https://forum.ansible.com/t/checking-list-of-files-to-see-if-one-exists/38192/4 "2023-07-05T11:35:41Z")

</div>

Oh boy, do I feel dumb. I should know that - I was making the problem harder than it has to be. Thank you!

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 5, 2023, 12:46pm UTC](https://forum.ansible.com/t/checking-list-of-files-to-see-if-one-exists/38192/5 "2023-07-05T12:46:16Z")

</div>

normally I am at the ‘do I feel dumb’ side of the conversation.  
So it’s really great whether for once I can help someone else out.
