# Better way to check if file exists?

**URL:** <https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [June 23, 2013, 12:51am UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009 "2013-06-23T00:51:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Mandel](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Mark\_Mandel](https://forum.ansible.com/u/Mark_Mandel)\
**Post date:** [June 23, 2013, 12:51am UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009/1 "2013-06-23T00:51:29Z")

</div>

Hi all,

I’m setting up my local machine provisioning on Ubuntu, and I find myself doing this pattern a lot for software that’s not managed by APT/PPA. E.g. Google Chrome.

#chrome

- name: See if chrome is installed  
command: find /usr/bin -name ‘google-chrome’  
register: chrome\_installed
- include: \_google\_chrome.yml  
when: (not chrome\_installed.stdout)

So I check to see if the file exists using a find, store that result in a variable, and then use it in my when to decide if ansible should do the chrome install process (which is an include).

Is there an easier way to check if a file exists? Maybe a simple lookup plugin that I’m missing?

Just figured I would ask the question, as I’m doing this a lot, and thought there may be a more efficient way.

Thanks!

Mark

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 23, 2013, 1:02am UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009/2 "2013-06-23T01:02:50Z")

</div>

lookup plugins are evaluated server side and are not a fit in this case.

I would probably key it off the return code of a find, using .rc vs .stdout

You could definitely write a custom facts module that would make setting all those variables a lot easier, and it could encapsulate all of those checks.

---

<div class="post-metadata">

**Author:** ![Mark\_Mandel](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Mark\_Mandel](https://forum.ansible.com/u/Mark_Mandel)\
**Post date:** [June 23, 2013, 1:15am UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009/3 "2013-06-23T01:15:58Z")

</div>

Thanks, I will look into doing some custom facts.

Another way to check if a file exists, that I’m liking a bit more than the ‘find’ syntax is using shell like so:

- name: See if zsh is installed  
shell: “[-d ~/.oh-my-zsh] && echo ‘Found’ || echo ‘’”  
register: zsh\_installed

Which I am using in my ZSH installation include here: [https://github.com/markmandel/dotfiles/blob/master/provision/roles/core/tasks/\_oh\_my\_zsh.yml](https://github.com/markmandel/dotfiles/blob/master/provision/roles/core/tasks/_oh_my_zsh.yml)

```auto

```

```auto
Works out very well, as find can sometimes be a bit of a pain.

```

```auto

```

```auto
Mark

```

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 23, 2013, 1:26am UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009/4 "2013-06-23T01:26:54Z")

</div>

Can’t you just do a regular “-f” test?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 23, 2013, 1:27am UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009/5 "2013-06-23T01:27:27Z")

</div>

Nevermind, read that as if you were running oh-my-zsh to check if something else was there.

Brain not working, sorry 🙂

---

<div class="post-metadata">

**Author:** ![Mark\_Mandel](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Mark\_Mandel](https://forum.ansible.com/u/Mark_Mandel)\
**Post date:** [June 23, 2013, 1:28am UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009/6 "2013-06-23T01:28:17Z")

</div>

No dramas 😉 only just realised I could do -f / -d test too.

Mark

---

<div class="post-metadata">

**Author:** ![GW1](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@GW1](https://forum.ansible.com/u/GW1)\
**Post date:** [June 23, 2013, 3:19pm UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009/7 "2013-06-23T15:19:20Z")

</div>

Hi,

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [June 23, 2013, 3:36pm UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009/8 "2013-06-23T15:36:54Z")

</div>

​Google Chrome actually has also an apt repo:

deb [http://dl.google.com/linux/chrome/deb/](http://dl.google.com/linux/chrome/deb/) stable main

Serge​

---

<div class="post-metadata">

**Author:** ![Mark\_Mandel](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Mark\_Mandel](https://forum.ansible.com/u/Mark_Mandel)\
**Post date:** [June 24, 2013, 3:09am UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009/9 "2013-06-24T03:09:23Z")

</div>

Chrome has more features - including an updated Flash.  
[https://code.google.com/p/chromium/wiki/ChromiumBrowserVsGoogleChrome](https://code.google.com/p/chromium/wiki/ChromiumBrowserVsGoogleChrome)

Can’t remember if Chromium has the bookmark / extension sync feature. I can’t live without that.

---

<div class="post-metadata">

**Author:** ![Mark\_Mandel](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Mark\_Mandel](https://forum.ansible.com/u/Mark_Mandel)\
**Post date:** [June 24, 2013, 3:10am UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009/10 "2013-06-24T03:10:43Z")

</div>

Yeah, that would be another way to go for sure.

Mark

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 24, 2013, 3:27am UTC](https://forum.ansible.com/t/better-way-to-check-if-file-exists/13009/11 "2013-06-24T03:27:56Z")

</div>

I use chromium, it does have bookmark/extension sync.
