Re: Using onlyif for Windows exec

869 views
Skip to first unread message

jcbollinger

unread,
Apr 16, 2013, 10:54:19 AM4/16/13
to puppet...@googlegroups.com


On Monday, April 15, 2013 9:14:21 AM UTC-5, Gary Park wrote:
Hello,

I am in the process of "trying out" Puppet, and so far, it is going really well, and I can see a clear line of how we can use it internally.

I do have one question though with regard to the using the onlyif parameter of the exec command (as per here http://docs.puppetlabs.com/references/latest/type.html#exec).

Ideally, what I would like to do is to only run a PowerShell exec command, if a web page doesn't currently exist on the server (i.e. the PowerShell script is responsible for deploying the Web Pages (into SharePoint in this case) and I only want to run this step, if these pages don't already exist.  To that end, I have done something like this:

onlyif => '$webRequest = [System.Net.WebRequest]::Create("http://some-url.test.aspx"); $webRequest.UseDefaultCredentials = $true; try { if([int]$webRequest.GetResponse().StatusCode -eq "200") { exit 0; } else { exit 1; } } catch [System.Net.WebException] { exit 1; }'

Which, at the command line, has the correct result.  However, when I try to run this, I get an error saying that $webRequest is not recognised.
 

Puppet invokes the specified command directly, not via the [standard | Power] shell, so whether that works at the (some) command line is irrelevant.

 

Which leads me to think that using variables within the onlyif is not supported.  Is that correct?  If so, what is the best approach for doing this, or am I going up the wrong path?



No, that's not correct.  I suppose you expect '$webRequest' to be meaningful to (and the whole command sequence to be executed by) PowerShell, but you haven't told Puppet to run it via PowerShell.  Instead, you've told Puppet to execute a command named literally '$webRequest'.  Refer to the docs on the Exec type's "windows" provider for information and examples of how to make this sort of thing work: docs.puppetlabs.com/references/3.1.latest/type.html#exec .  What they say about the 'command' parameter applies equally to 'onlyif'.


John

Gary Park

unread,
Apr 16, 2013, 4:29:24 PM4/16/13
to puppet...@googlegroups.com
Hello,
 
Thanks for getting back to me.
 
I think I forgot to mention, I am using this:
 
 
Does this "change" how the onlyif parameter interprets what it is provided?
 
Thanks

Gary

Justin Stoller

unread,
Apr 16, 2013, 7:57:25 PM4/16/13
to puppet...@googlegroups.com
On Tue, Apr 16, 2013 at 1:29 PM, Gary Park <gep...@gmail.com> wrote:
Hello,
 
Thanks for getting back to me.
 
I think I forgot to mention, I am using this:
 
 
Does this "change" how the onlyif parameter interprets what it is provided?

yes it should. the onlyif and unless parameters should be interpreted the same was the command parameter.
 
Thanks

Gary
--
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 post to this group, send email to puppet...@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gary Park

unread,
Apr 17, 2013, 7:55:51 AM4/17/13
to puppet...@googlegroups.com
Hello,

Thanks for all the information so far.

Looks like I have got this working.  I think that there was a couple things at play, most notable the use of double quotes in the onlyif command.

I replaced these with \' and that seemed to help get things moving.

Thanks again!

Gary

Josh Cooper

unread,
May 21, 2013, 1:27:01 AM5/21/13
to puppet...@googlegroups.com
Hi Gary,

On Wed, Apr 17, 2013 at 4:55 AM, Gary Park <gep...@gmail.com> wrote:
Hello,

Thanks for all the information so far.

Looks like I have got this working.  I think that there was a couple things at play, most notable the use of double quotes in the onlyif command.

I replaced these with \' and that seemed to help get things moving.

Since powershell variables are referenced using `$varname`, when they are used in double-quoted strings, they need to be escaped, otherwise puppet will think it's one of its variables. So you'll need to do:

    onlyif => "\$webRequest ..."

If you are not doing any variable interpolation, then it's best to use a single quoted string:

    onlyif => '$webRequest ..."

Josh

--
Josh Cooper
Developer, Puppet Labs

Join us at PuppetConf 2013, August 22-23 in San Francisco - http://bit.ly/pupconf13
Register now and take advantage of the Early Bird discount - save 25%!

Reply all
Reply to author
Forward
0 new messages