Better way to check if file exists?

3,437 views
Skip to first unread message

Mark Mandel

unread,
Jun 22, 2013, 8:51:29 PM6/22/13
to ansible...@googlegroups.com
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

--
E: mark....@gmail.com
T: http://www.twitter.com/neurotic
W: www.compoundtheory.com

2 Devs from Down Under Podcast

Michael DeHaan

unread,
Jun 22, 2013, 9:02:50 PM6/22/13
to ansible...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Mark Mandel

unread,
Jun 22, 2013, 9:15:58 PM6/22/13
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Jun 22, 2013, 9:26:54 PM6/22/13
to ansible...@googlegroups.com
Can't you just do a regular "-f" test?

Michael DeHaan

unread,
Jun 22, 2013, 9:27:27 PM6/22/13
to ansible...@googlegroups.com
Nevermind, read that as if you were running oh-my-zsh to check if something else was there.

Brain not working, sorry :)

Mark Mandel

unread,
Jun 22, 2013, 9:28:17 PM6/22/13
to ansible...@googlegroups.com
No dramas ;) only just realised I could do -f / -d test too.

Mark

GW

unread,
Jun 23, 2013, 11:19:20 AM6/23/13
to ansible...@googlegroups.com

Hi,

On Sun, 23 Jun 2013 10:51:29 +1000, Mark Mandel wrote:

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.
Just curious why are you using Google Chrome, instead of Chromium that *is* managed by APT/PPA and is even in the official repositories?
And in case you install this packages through a .deb archive, you may consider creating a local repository for you custom packages, eg. in `/root/debs`. This is pretty simple to do, you just need to regenerate Packages.gz each time and add it to your /etc/apt/sources.list. Than you use all packages through APT tools as usual.
Greetings,
   gw 

Serge van Ginderachter

unread,
Jun 23, 2013, 11:36:54 AM6/23/13
to ansible...@googlegroups.com

On 23 June 2013 17:19, GW <gw....@tnode.com> wrote:
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.
Just curious why are you using Google Chrome, instead of Chromium that *is* managed by APT/PPA and is even in the official repositories?

​Google Chrome actually has also an apt repo:


Serge​

Mark Mandel

unread,
Jun 23, 2013, 11:09:23 PM6/23/13
to ansible...@googlegroups.com
Chrome has more features - including an updated Flash.

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


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Mark Mandel

unread,
Jun 23, 2013, 11:10:43 PM6/23/13
to ansible...@googlegroups.com
Yeah, that would be another way to go for sure.

Mark


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Brian Coca

unread,
Jun 23, 2013, 11:27:56 PM6/23/13
to ansible...@googlegroups.com
I use chromium, it does have bookmark/extension sync.

--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno
Reply all
Reply to author
Forward
0 new messages