Better way to check if file exists?

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

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.

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


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

Mark

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

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

Brain not working, sorry :slight_smile:

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

Mark

Hi,

​Google Chrome actually has also an apt repo:

deb http://dl.google.com/linux/chrome/deb/ stable main

Serge​

Chrome has more features - including an updated Flash.
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.

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

Mark

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