Conditional Creates Attribute

281 views
Skip to first unread message

Mark McFate

unread,
May 29, 2014, 10:23:54 AM5/29/14
to puppet...@googlegroups.com
I'm probably going about this all wrong, but I have an instance where I've employed a Puppet module and need to sometimes add a "creates" attribute to one of the exec's defined there.  My code (below) is probably all wrong, but I think you'll see what I am trying to do...

  if $creates != nil {
    exec { "drush-${title}" :
      command => "drush ${command} ${root_option} ${uri_option} ${force_option} ${additional_options}",
      path    => [ '/bin', '/usr/bin' ],
      creates => $creates, 
    }
  } else {
    exec { "drush-${title}" :
      command => "drush ${command} ${root_option} ${uri_option} ${force_option} ${additional_options}",
      path    => [ '/bin', '/usr/bin' ],
    }
  }
 
This doesn't work and neither does specifying an empty or nil attribute, like "creates => ''" or "creates => nil".  

If nothing else, I will create two different exec's, one with and the other without a creates attribute.  Can anyone suggest a more elegant solution?

Thanks in advance.

Peter Bukowinski

unread,
May 29, 2014, 10:42:49 AM5/29/14
to puppet...@googlegroups.com
This can likely be solved with the defined() function. http://docs.puppetlabs.com/references/latest/function.html#defined

exec { "drush-${title}" :
    command => "drush ${command} ${root_option} ${uri_option} ${force_option} ${additional_options}",
    path    => [ '/bin', '/usr/bin' ],
    if defined('$creates') {
        creates => $creates,
    }
}

--
Peter

José Luis Ledesma

unread,
May 29, 2014, 10:47:55 AM5/29/14
to puppet...@googlegroups.com

I have not tried with the creates param, but if its undef ( I.e. no one have set it) you can use it directly

exec { "drush-${title}" :

      command => "drush ${command} ${root_option} ${uri_option} ${force_option} ${additional_options}",

      path    => [ '/bin', '/usr/bin' ],

      creates => $creates, 

    }

If it's undef, this should work like not specifying it.

Regards,

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/753daff9-b848-40b9-bca8-1328c313c870%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark McFate

unread,
May 29, 2014, 11:11:30 AM5/29/14
to puppet...@googlegroups.com
Thanks Peter, but I can't make that work either.  Puppet doesn't seem to like the defined( ) function inside the exec attributes list.  It's kicking back a syntax error of the form:
Error: Syntax error at 'defined'; expected '}' at /tmp/vagrant-puppet-1/modules-0/drush/manifests/exec.pp:65 

-Mark

Mark McFate

unread,
May 29, 2014, 11:13:55 AM5/29/14
to puppet...@googlegroups.com
And thank you Joes Luis, but I'm also unable to make this work.  It seems the $creates parameter must be declared in my module and when left with a blank or nil value Puppet kicks back this error:

Error: Parameter creates failed on Exec[drush-drush-download-modules]: creates must be a fully qualified path at /tmp/vagrant-puppet-1/modules-0/drush/manifests/exec.pp:68
  
-Mark

José Luis Ledesma

unread,
May 29, 2014, 11:21:41 AM5/29/14
to puppet...@googlegroups.com

Have you tried to set it to undef?

Mark McFate

unread,
May 29, 2014, 11:32:10 AM5/29/14
to puppet...@googlegroups.com
Ah, I misunderstood and didn't realize that Puppet's 'undef' bareword is effectively equivalent to a Ruby 'nil'.  Thanks for the follow-up!  I'm not all the way there yet, but yes, I believe this approach is working.  Thanks.

Just to be clear, I added a parameter definition to the module so that it now reads...

define drush::exec(
  $command        = $title,
  $creates           = undef,

-Mark

Mark McFate

unread,
May 29, 2014, 11:38:54 AM5/29/14
to puppet...@googlegroups.com
Confirmed... defaulting the parameter to the bareword 'undef' does the trick.  Thanks again for the assist.

-Mark

jcbollinger

unread,
May 29, 2014, 1:59:35 PM5/29/14
to puppet...@googlegroups.com


On Thursday, May 29, 2014 9:23:54 AM UTC-5, Mark McFate wrote:
I'm probably going about this all wrong, but I have an instance where I've employed a Puppet module and need to sometimes add a "creates" attribute to one of the exec's defined there.  My code (below) is probably all wrong, but I think you'll see what I am trying to do...

  if $creates != nil {


I think you can spell that as

if $creates { [...]

(See the docs on truthiness.)

Alternatively, you could take advantage of the fact that interpolating an unset variable gets you the empty string:

if "$creates" == '' { [...]

 
    exec { "drush-${title}" :
      command => "drush ${command} ${root_option} ${uri_option} ${force_option} ${additional_options}",
      path    => [ '/bin', '/usr/bin' ],
      creates => $creates, 
    }
  } else {
    exec { "drush-${title}" :
      command => "drush ${command} ${root_option} ${uri_option} ${force_option} ${additional_options}",
      path    => [ '/bin', '/usr/bin' ],
    }
  }
 
This doesn't work and neither does specifying an empty or nil attribute, like "creates => ''" or "creates => nil".  



You want

  creates => undef

for that approach (no quotes).  That's an affirmative declaration of not specifying any value, even an empty one, for the given parameter.


John

Henrik Lindberg

unread,
Jun 2, 2014, 10:05:55 AM6/2/14
to puppet...@googlegroups.com
On 2014-29-05 19:59, jcbollinger wrote:
>
>
> On Thursday, May 29, 2014 9:23:54 AM UTC-5, Mark McFate wrote:
>
> I'm probably going about this all wrong, but I have an instance
> where I've employed a Puppet module and need to _sometimes_ add a
> "creates" attribute to one of the exec's defined there. My code
> (below) is probably all wrong, but I think you'll see what I am
> trying to do...
>
> if $creates != nil {
>
>
>
> I think you can spell that as
>
> if $creates { [...]
>
> (See the docs on truthiness
> <http://docs.puppetlabs.com/puppet/3/reference/lang_datatypes.html#automatic-conversion-to-boolean>.)
>
> Alternatively, you could take advantage of the fact that interpolating
> an unset variable gets you the empty string:
>
> if "$creates" == '' { [...]
>
> exec { "drush-${title}" :
> command => "drush ${command} ${root_option} ${uri_option}
> ${force_option} ${additional_options}",
> path => [ '/bin', '/usr/bin' ],
> creates => $creates,
> }
> } else {
> exec { "drush-${title}" :
> command => "drush ${command} ${root_option} ${uri_option}
> ${force_option} ${additional_options}",
> path => [ '/bin', '/usr/bin' ],
> }
> }
>
> This doesn't work and neither does specifying an empty or nil
> attribute, like "creates => ''" or "creates => nil".
>
>
>
> You want
>
> creates => undef
>
> for that approach (no quotes). That's an affirmative declaration of not
> specifying any value, even an empty one, for the given parameter.
>
To be pedantic, it means "use the default value".

You do not need the conditional construct around the resource, simply
use

creates => $creates

Since, if $creates is undefined, so will creates parameter be.
(at least in theory, but may depend on the impl of the exec resource type).

- henrik


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

jcbollinger

unread,
Jun 3, 2014, 9:48:54 AM6/3/14
to puppet...@googlegroups.com


On Monday, June 2, 2014 9:05:55 AM UTC-5, Henrik Lindberg wrote:
On 2014-29-05 19:59, jcbollinger wrote:
>
> You want
>
>    creates => undef
>
> for that approach (no quotes).  That's an affirmative declaration of not
> specifying any value, even an empty one, for the given parameter.
>
To be pedantic, it means "use the default value".



I like my characterization better, but it amounts to much the same thing because the default value -- if there is one -- will be used if no value is declared for the parameter.

Also, one must be careful here to be clear about which default applies: it is whatever is built in to the resource type, as opposed to any DSL-declared resource default that may be in scope.  One of the uses of declaring a parameter undef is to override a resource default of the latter kind.

 
You do not need the conditional construct around the resource, simply
use

    creates => $creates

Since, if $creates is undefined, so will creates parameter be.
(at least in theory, but may depend on the impl of the exec resource type).



Really?  It is my perhaps imperfect recollection that that didn't work, on account of the reference to an undefined variable being evaluated as an empty string.  Perhaps indeed that varies by resource type, but if so, I find that disturbing.  Will this be consistent in Puppet 4, one way or the other?


John

Reply all
Reply to author
Forward
0 new messages