Ask Your Question

unable to use awk in exec resources

asked 2015-04-16 14:53:02 -0500

Chutki gravatar image

updated 2015-04-16 14:53:54 -0500


I am compiling below manifests

exec {'shmmax':
    cwd => '/etc',
    path => ['/etc'],
    #command => "/usr/bin/awk '{getline shmmax < "/proc/sys/kernel/shmmax"} /^kernel.shmmax/ { if (200 > shmmax) sub(\$3,shmmax)} 1' /etc/sysctl.conf >> /etc/sysctl.conf.mod && /bin/mv /etc/sysctl.conf.mod /etc/sysctl.conf",
    command => "usr/bin/awk 'BEGIN{getline shmmax < "/proc/sys/kernel/shmmax"; if((shmmax + 0) > 200) print shmmax}' >>/etc/sysctl.conf >> /etc/sysctl.conf.mod && /bin/mv /etc/sysctl.conf.mod /etc/sysctl.conf",
    onlyif  => "/bin/grep '^kernel.shmmax' /etc/sysctl.conf",

but getting error

Error 400 on SERVER: Syntax error at '; if((shmmax + 0) > 200) print shmmax}' >>/etc/sysctl.conf >> /etc/sysctl.conf.mod && /bin/mv /etc/sysctl.conf.mod /etc/sysctl.conf'; expected '}' at /etc/puppetlabs/puppet/modules/offshore/sysctl/manifests/kernel_param.pp:8 on node

I guess "/proc/sys/kernel/shmmax" value is not accepting by puppet. can someone explain why it is not working

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-04-17 06:49:22 -0500

JohnsonEarls gravatar image

updated 2015-04-24 08:10:58 -0500

You are surrounding the command value with " quotes, but then using " quotes within the command itself. Try escaping the inner " quotes: (You also lost an initial / in the path to awk)

    command => "/usr/bin/awk 'BEGIN{getline shmmax < \"/proc/sys/kernel/shmmax\"; if((shmmax + 0) > 200) print shmmax}' >>/etc/sysctl.conf >> /etc/sysctl.conf.mod && /bin/mv /etc/sysctl.conf.mod /etc/sysctl.conf",

However, note that your command has a number of problems with it besides the syntax error that causes the catalog not to generate:

  • You're doing two output redirections; >> /etc/sysctl.conf and >> /etc/sysctl.conf.mod.
    • If you keep the >> /etc/sysctl.conf, then you can get rid of the && /bin/mv part.
    • If you keep the >> /etc/sysctl.conf.mon, then you need to do an initial /bin/cp /etc/sysctl.conf /etc/sysctl.conf.mod, otherwise you will overwrite your /etc/sysctl.conf with a file containing only the awk output.
  • You're not redirecting the input of awk from anything, so it will be trying to read from whatever puppet passes to it for standard input. I'm not sure if that will cause a problem or not.
  • The awk script does not output a properly formatted sysctl.conf line. It outputs a bare variable, instead of parameter = variable format.

The best way to get around all of this is to not run awk directly, and instead use Puppet's augeas interface:

$current_shmmax = generate("cat /proc/sys/kernel/shmmax")
if $current_shmmax > 200 {
    augeas { "kernel.shmmax-in-sysctl.conf":
        context => "/files/etc/sysctl.conf" ,
        changes => "set kernel.shmmax ${current_shmmax}"

However, augeas has some prerequisites - namely, installing Augeas itself as well as the ruby-augeas bindings: (you might need to change the name of the "augeas" package if you're not running a RedHat family Linux)

package { "augeas":
    ensure => installed
package { "ruby-augeas":
    provider => "gem" ,
    ensure => installed
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



Asked: 2015-04-16 14:53:02 -0500

Seen: 549 times

Last updated: Apr 24 '15