Ask Your Question
0

only if not working in exec resource

asked 2015-04-18 11:24:22 -0500

Chutki gravatar image

Hi,

I am doing changes in sysctl.conf file, wanna change 6th column in kernel parameter.

kernel.sem = 33  44  55  66

below is the manifests

exec {'sysctl_test':
    path => ['/etc','/usr/bin','/usr/sbin'],
    cwd     => '/etc',
    command => "/usr/bin/awk '/kernel.sem/ { if(( \$6 +0) < 77) sub( \$6,$value) } 1' /etc/sysctl.conf >> /etc/sysctl.conf.mod && /bin/mv /etc/sysctl.conf.mod /etc/sysctl.conf",
    onlyif => "/usr/bin/awk '/kernel.sem/ { if(( \$6 +0) < 77) print $0; else exit;}' /etc/sysctl.conf"

puppet manifests working with out any issues, getting below o/p

 kernel.sem = 33  44  55  77

but "exec" resources running again and again, it toches the file but the content is not overwriting.

when I run

onlyif => "/usr/bin/awk '/kernel.sem/ { if(( \$6 +0) < 77) print $0; else exit;}' /etc/sysctl.conf"

in prompt its working, the value 77 print only it is higher than the existing else it will exit so that command attribute should not run, but I dont know why it is running.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2015-04-18 15:37:51 -0500

GregLarkin gravatar image

Read this piece of documentation very closely: https://docs.puppetlabs.com/references/latest/type.html#exec-attribute-onlyif, and make sure you understand the role of the command exit code when using the onlyif attribute with the exec resource. That should help you debug the problem.

edit flag offensive delete link more

Comments

The command referenced by the onlyif attribute should only exit with a zero or non-zero code, and does not need to print anything to stdout or stderr. The only thing I would change is "print $0" to "exit 0".

GregLarkin gravatar imageGregLarkin ( 2015-04-19 11:02:34 -0500 )edit
0

answered 2015-04-19 09:28:14 -0500

Chutki gravatar image

Thanks Greg for your reference URL.

I added exit 1 condition after else

onlyif => "/usr/bin/awk '/kernel.sem/ { if(( \$6 +0) < 77) print $0; else exit 1;}' /etc/sysctl.conf"

so the program stop if the condition is false. you can sugegest me if you know any better option.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-04-18 11:24:22 -0500

Seen: 698 times

Last updated: Apr 19 '15