problems with onlyif/unless directives

2,202 views
Skip to first unread message

sHaggY_caT

unread,
Dec 13, 2009, 2:20:30 PM12/13/09
to Puppet Users
Hi all!

Please help me get understand my error:

I have problems with onlyif/unless directives. Simple test work fine,
for example:


exec { "Update ovz templates":

command => "wget -O /vz/template/cache/centos-5-i386-
default.tar.gz http://install.local/ovz/templates/centos-5-i386-default.tar.gz
&& rm /vz/firsh_install.lock",
path => "/usr/bin:/usr/sbin:/bin",
onlyif => "test -f /vz/firsh_install.lock"
}

It's exec run, and download teplate only if file /vz/
firsh_install.lock exist.
I make custom script for running inside bash shell on puppet node,
script return 0 or 1

When i try use it in puppet module, puppet run unless exec always, but
onlyif exec never run.

this exec does'nt work true:

exec { "vecfg-applyset_${name}":
command => "if [[ ( -d /vz/private/${veid} ) && ( -f /etc/
sysconfig/vz-scripts/${veid}.conf)]] ; then vzctl set ${veid} --
hostname ${vehostname} --ipadd ${veip} --nameserver ${vedns} --save;
vzctl set ${veid} --applyconfig ${vetarif} --save; fi",
path => ["/usr/bin", "/usr/sbin"],
unless => "/usr/sbin/rc.test-parametrs-changed.sh ${veid} ${veip} $
{vetarif} ${vedns}",
}


file /usr/sbin/rc.test-parametrs-changed.sh work, in shell, fine
(return 0 or 1):

============================
cat /usr/sbin/rc.test-parametrs-
changed.sh
#!/bin/
bash

#Check VE settings for Puppet. Returned "1", if settings changed, else
return 0

#Get variables
VEID=$2
#also, $VEIP variable is variable of on/off status of conainer
VEIP=$3
VETARIF=$4
VEDNS=$5

PATHVE="/vz/private/"$VEID
PATHVECONF="/etc/vz/conf/"$VEID".conf"

check_settings()
{

if [[ ( $(grep $VEIP $PATHVECONF 1>/dev/null 2>&1; echo $?) -eq 0) &&
( $(grep $VETARIF $PATHVECONF 1>/dev/null 2>&1; echo $?) -eq 0 ) && ( $
(grep $VEDNS $PATHVECONF 1>/dev/null 2>&1; echo $? ) -eq 0 ) ]]; then
exit 0; else exit 1; fi


}

check_status()
{

if [[ ( $(grep $VEIP $PATHVECONF 1>/dev/null 2>&1; echo $?) -eq
0 ) ]]; then exit 0; else exit 1; fi
}

case "$1" in
check_settings)
check_settings
;;
check_status)
check_status
;;

*)
echo $"Usage: $0 {check_settings|check_status}"
exit 0
esac

jcbollinger

unread,
Dec 14, 2009, 12:13:18 PM12/14/09
to Puppet Users


On Dec 13, 1:20 pm, sHaggY_caT <galia....@gmail.com> wrote:
> Please help me get understand my error:

The debug output from trying to run these would probably help with
that, but I'll see whether I can provide at least a little insight
anyway.

> I have problems with onlyif/unless directives. Simple test work fine,

[...]

> I make custom script for running inside bash shell on puppet node,
> script return 0 or 1
>
> When i try use it in puppet module, puppet run unless exec always, but
> onlyif exec never run.

> this exec does'nt work true:
>
> exec { "vecfg-applyset_${name}":
>     command => "if [[ ( -d /vz/private/${veid} ) && ( -f /etc/
> sysconfig/vz-scripts/${veid}.conf)]] ; then vzctl set ${veid}  --
> hostname ${vehostname} --ipadd ${veip} --nameserver ${vedns} --save;
> vzctl set ${veid} --applyconfig ${vetarif} --save;  fi",
>     path => ["/usr/bin", "/usr/sbin"],
>     unless => "/usr/sbin/rc.test-parametrs-changed.sh ${veid} ${veip} $
> {vetarif} ${vedns}",
>     }
>
> file /usr/sbin/rc.test-parametrs-changed.sh work, in shell, fine
> (return 0 or 1):

As far as I know, Exec executes its command(s) directly, not via a
shell, so I would be surprised if that did what you expect,
independent of the "unless". That is, "if" is a keyword in the sh
language, not an executable program (not on any system I'm familiar
with, anyway), so I would expect that Exec to fail every time it runs.

For debugging your manifests, you could try using

unless => "/bin/true"

or

unless => "/bin/false"

to take that test script out of the picture.

Speaking of the test script, are you sure your nodes are getting and
using the correct version? You can have the puppetmaster distribute
it, but you may need to set up a require for it to make sure its
downloaded first. (It's not clear from the docs whether Puppet will
autorequire it from the "unless" parameter.) You'll also want to make
sure its ownership and permissions are set appropriately.


John

sHaggY_caT

unread,
Dec 15, 2009, 6:12:50 AM12/15/09
to Puppet Users
Hi John,

Thank you for answer.

> For debugging your manifests, you could try using
>
> unless => "/bin/true"
>
> or
>
> unless => "/bin/false"
>
> to take that test script out of the picture.


Script, in "command" work fine.


> Speaking of the test script, are you sure your nodes are getting and
> using the correct version?

It's use one version, without differents of number:

[shaggycat@puppet ~]$ rpm -qa | grep puppet
puppet-server-0.24.8-1.el5.1
puppet-0.24.8-1.el5.1
[shaggycat@puppet ~]$

[root@vz-test conf]# rpm -qa | grep puppet
puppet-0.24.8-1.el5.1
[root@vz-test conf]#

> As far as I know, Exec executes its command(s) directly, not via a
> shell, so I would be surprised if that did what you expect,
> independent of the "unless". That is, "if" is a keyword in the sh
> language, not an executable program (not on any system I'm familiar
> with, anyway), so I would expect that Exec to fail every time it runs.


OK, i think you true. Do you have means, how use bash script inside
"onlyif"/"unless"?
I try use script in command line:

[root@vz-test conf]# test "/usr/sbin/rc.test-parametrs-changed.sh
check_settings 301 10.0.5.31 yes vps256M 10.0.5.52"
[root@vz-test conf]# echo $?
0
[root@vz-test conf]# /usr/sbin/rc.test-parametrs-changed.sh
check_settings 301 10.0.5.31 yes vps256M 10.0.5.52
[root@vz-test conf]# echo $?
1
[root@vz-test conf]#

but, bash exec(system command) return 0:

[root@vz-test conf]# bash /usr/sbin/rc.test-parametrs-changed.sh
check_settings 301 10.0.5.31 yes vps756M 10.0.5.52
[root@vz-test conf]# echo $?
0
[root@vz-test conf]#

but "unless" inside puppet exec run exec:

exec { "vecfg-applyset_${name}":
command => "if [[ ( -d /vz/private/${veid} ) && ( -f /etc/
sysconfig/vz-scripts/${veid}.conf) && \
( $(/usr/sbin/rc.test-parametrs-changed.sh check_settings ${veid}
${veip} ${vetarif} ${vedns} ; echo $?) -eq 1)]] ; \
then vzctl set ${veid} --hostname ${vehostname} --ipadd ${veip}
--nameserver ${vedns} --save; \
vzctl set ${veid} --applyconfig ${vetarif} --save; fi",
path => ["/usr/bin", "/usr/sbin"],
unless => "bash /usr/sbin/rc.test-parametrs-changed.sh
check_settings ${veid} ${veip} ${vetarif} ${vedns}",
}

If/else inside "command" works, as i test (i use if/else inside
command, becouse unless/onlyif does'nt work for me):

[root@vz-test conf]# cat ~/tmp_2 | grep -v _off
Dec 15 13:41:05 vz-test puppetd[23387]: Starting catalog run
Dec 15 13:41:06 vz-test puppetd[23387]: (//Node[vz-test.local]/
Mod_class_ovzconfigs::Mod_def_veadm[customer2.vps.local]/Exec[vecfg-
applyset_customer2.vps.local]/returns) executed successfully
Dec 15 13:41:16 vz-test puppetd[23387]: (//Node[vz-test.local]/
Mod_class_ovzconfigs::Mod_def_ovzbackup[ovzserver_backup]/Cron
[incremental_backup]/weekday) defined 'weekday' as '*'
Dec 15 13:43:07 vz-test puppetd[23387]: (//Node[vz-test.local]/
Mod_class_ovzconfigs::Mod_def_veadm[customer1.vps.local]/Exec[vecfg-
applyset_customer1.vps.local]/returns) executed successfully
Dec 15 13:43:14 vz-test puppetd[23387]: (//Node[vz-test.local]/
Mod_class_ovzconfigs::Mod_def_veadm[customer4.vps.local]/Exec[vecfg-
applyset_customer4.vps.local]/returns) executed successfully
Dec 15 13:43:29 vz-test puppetd[23387]: Finished catalog run in 143.67
seconds

sHaggY_caT

unread,
Dec 15, 2009, 11:17:34 AM12/15/09
to Puppet Users
I think, problems was resolved:

Exec with this "onlyif":

onlyif => "test `/root/test-new.sh ${veid} ${vetarif}` = 0 "

Run only if $veid=302:


Dec 15 19:12:19 vz-test puppetd[16426]: Starting catalog run
Dec 15 19:12:45 vz-test puppetd[16426]: (//Node[vz-test.local]/
Mod_class_ovzconfigs::Mod_def_ovzbackup[ovzserver_backup]/Cron
[incremental_backup]/weekday) defined 'weekday' as '*'
Dec 15 19:12:46 vz-test puppetd[16426]: (//Node[vz-test.local]/
Mod_class_ovzconfigs::Mod_def_veadm[customer2.vps.local]/Exec[vecfg-
applyset_customer2.vps.local]/returns) executed successfully
Dec 15 19:12:46 vz-test puppetd[16426]: Finished catalog run in 27.27
seconds


onlyif => "test `/root/test-new.sh ${veid} ${vetarif}` = 0 "


[root@vz-test conf]# cat /root/test-new.sh
#!/bin/bash
if [[ $1 = 302 ]]
then echo 0
else echo 1
fi

Thank you for answer.

For another puppet users, who find this topic: try to use binary file
test for test with your custom scripts :))
Reply all
Reply to author
Forward
0 new messages