On Friday, December 4, 2015 at 10:27:52 AM UTC-6, Sean wrote:
>
> Greetings,
>
> I am working on streamlining some older puppet code, that uses a lot of 
> Exec resources to accomplish it's purposes.  It's not terribly elegant and 
> we're working on design to replace it with code that leverages puppet 
> features.  One of the pieces I'm struggling with is how to set complex 
> environment variables that are available to use in onlyif/unless statements 
> as well as the command itself.  I'm wondering if this just isn't possible?
>
> Here's an example with the aide.  NOTE that we're not in a spot where I 
> can make puppet manage the aide config file, and thus use an aide module 
> and parameters instead of shell variables.  All of these commands run 
> successfully in a bash shell for the various conditions that would apply. 
>  I have other similar scenarios, but aide is one of the more complex ones.
>
>   exec { 'init-aide-database':
>     path        => 
> '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin',
>     environment => [ 'DBDIR=$(egrep \'^@@define DBDIR \' 
> /etc/aide.conf|awk \'{print $NF}\')',
>       'DBFILE=$(egrep \'^database=file\' /etc/aide.conf|awk -F/ \'{print 
> $NF}\')',
>       'DBNEW=$(egrep \'^database_out=file\' /etc/aide.conf|awk -F/ 
> \'{print $NF}\')',
>       ],
>     command     => '/usr/sbin/aide --init >/dev/null 2>&1 && cp -p 
> ${DBDIR}/${DBNEW} ${DBDIR}/${DBFILE}',
>     unless      => 'test -f ${DBDIR}/${DBNEW} && test -f 
> ${DBDIR}/${DBFILE}',
>     require     => Package['aide'],
>     logoutput   => true,
>     timeout     => 0,
>   }
>
> When executing, puppet always runs the exec because the env var's are 
> empty, so the unless case always fails.  Then we get a scenario where we're 
> running aide --init when it's not needed and the cp command throws an error 
> due to empty variables.
>
>

Are you sure?  I'd be inclined to suppose that the problem was that the 
environment variables don't have the values you expect them to have.  As 
far as I am aware, Puppet inserts environment variables into child 
processes' environments via a mechanism that does not involve evaluating 
the value expressions via the shell (even when you use the 'shell' 
provider).

I had thought that the same environment is presented to the 'unless' and 
'onlyif' commands as is presented to the main command, but I can't 
immediately back that up, as the documentation is at best unclear on that 
point.  If you could establish that it is not, then that might justify 
filing a ticket.

 

> My thought at the moment is that I need to build two shell scripts as file 
> resources.  One to call in the unless test, and another to call in the 
> command if the unless script fails.  Perhaps that's more elegant, but we're 
> hoping to avoid delivering script files to the nodes wherever possible. 
>  I'm hoping the gurus out here might point me in the best direction!
>


If you must determine the needed values for your environment variables at 
run time, by executing shell code, then I'm pretty confident that one way 
or another you will need to build that in to each of the commands you use.  
The approach you describe is a reasonable way to do that.

You could also consider whether it would be worth the effort to build a 
full-fledged custom type.  You would still need to perform some environment 
mangling, but you could at least avoid managing separate command scripts.


John

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/4cb89384-3113-4b11-ae48-a09281abc0b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to