Centos – Puppet: exec onlyif value is not equal

centosexecpuppet

I have a script that really only needs to be run one time on a server, i.e. at deployment time, but figured it would be best to have Puppet manage it. The script remaps several legacy user id's that conflict with local system users.

The way I check this is if the gopher user has the default uid of 13. If so, then I need to run my remapping script.

exec { "change_uid":
   command  => "/script/to/run.sh",
   provider => 'shell',
   path => [ "/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin", "/usr/local/sbin" ],
   onlyif   => "test `/usr/bin/id -u gopher` -eq 13; echo $?",
}

I've tried several different permutations of the above onlyif check, but the exec always gets fired on the agent.

When I run that command directly on the command line, it returns either 0 or 1. It could be related to the fact that bash returns 0 when true and 1 when false, but I think its more likely that I'm misunderstanding the way onlyif works.

How can I have this exec fire when the gopher's uid=13? I'm open to alternative ways to implement what I'm trying to accomplish here.

UPDATE

I dropped the echo part. The final working solution is:

exec { "change_uid":
   command  => "/script/to/run.sh",
   path     => [ "/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin", "/usr/local/sbin" ],
   onlyif   => "test `id -u gopher` -eq 13",
}

Making use of the ralsh commmand was helpful in testing, thanks @Daniel C. Sobral. I'm marking @Daniel's as the accepted answer since that was the main issue, besides my misunderstanding of a few concepts, plus he answered first. Thanks all.

Best Answer

Did you try removing the echo $? at the end?

That part will always make the whole thing return 0.

Related Topic