On Tue, Jan 24, 2012 at 7:39 PM, Nick <oinksoc...@letterboxes.org> wrote:
> I want to assert a file exists - but in a way that doesn't have the potential 
> to
> create conflicting resource definitions.
>
> I know this has been asked many times before:
>
>  "How can I check whether a file exists, like 'File.exists?(file)'
>  or '-f $file'?"
>
> My very specific use case is with a user-account define.  I want something 
> like
> this:
>
>  define user_account($name, $shell = '/bin/bash', /* ... */) {
>    /* ... */
>
>    assert file_exists($shell) # make sure the shell is available
>
>    /* ... */
>  }
>
> I don't want to create the file $shell, nor do I care how it gets there, but I
> want it to be there (and ideally, executable).  Declaring a resource 
> user("foo":
> shell = $shell) doesn't appear to assert this by itself (trying in 2.7.9).
>
> Just to be clear, "require File[$shell]" and equivalents are no good, because
> they force me to define a File resource which "owns" the file $shell, and 
> since
> there can only be one of those I am in trouble if something else wants to 
> define
> the same condition.  In general I cannot know if this is being asserted
> elsewhere by the author of another module.
>
> However, the question has a more general aspect. Why doesn't core Puppet make
> assertions like this easy?  It seems a simple, obvious, and common thing to
> want.  And not just for files - I often want to express similar assertions 
> about
> users, groups, packages, and probably other resources.
>
> This question seems like it ought to be addressed in the puppet FAQ [1], but I
> can't find any discussion of it there, nor in the puppet cookbook [2], puppet
> language guide [3], nor the "Puppet types" reference page [4].
>
> I know there are workarounds of sorts:
>
>  - if !defined(File[$shell]) { file{ $shell: ensure => 'exists' } }
>
>  - Exec { "check ${shell} exists for ${user}":
>          command => "/usr/bin/test -f '${shell}'" }
>
>  - if inline_template("<% File.exists?(shell)? 'true':'false' %>") == "true"
>    {  /* fail somehow */ }
>
>  - define a custom function which simply does File.exists?(shell) and fails if
>   false
>
>
> They are all sledgehammers to crack a nut, the first is apparently dangerous 
> and
> another thread on this list is proposing to outlaw it.
>
> To sum up, it appears that this feature is missing for a reason, but why?  And
> what's the best workaround available?

I'm assuming you mean if a resources exist in the catalog and not the
question of whether a file exist on the agent since they are different
issues.

Either a separate class for common resources or virtual resources and
realizing them instead of declaring them in a define:
http://docs.puppetlabs.com/guides/virtual_resources.html

Thanks,

Nan

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To post to this group, send email to puppet-users@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en.

Reply via email to