Puppet won’t execute command

puppet

Puppet 0.25.4 on ubuntu point blank refuses to execute the following command:

exec {"initiate replica set":
    command => "echo 'rs.initiate()' | mongo",
    path => ["/usr/bin","/usr/sbin","/bin"],
    user => "root",
    require => Class["mongodb"]
}

I can execute the command as root myself, so I'm guessing perhaps it's an issue with the shell. Unfortunately upgrading puppet isn't an option (and causes other issues anyway).

I've tried specifying explicit paths to the binaries instead of relying on the path parameter, and also changing the command to:

"bash -c \"echo 'rs.initiate()' | mongo\""

Still doesn't work.

Any ideas?

I get an error message saying something like "failed to change from notrun to 0"

Best Answer

See what the command actually returns when run as shell from root by running echo $? after running the actual command, probably it returns not 0. If so you need to change the statement to:

exec {"initiate replica set":
    command => "echo 'rs.initiate()' | mongo",
    path => ["/usr/bin","/usr/sbin","/bin"],
    user => "root",
    require => Class["mongodb"],
    returns => $ActualReturnCode
}

Where $ActualReturnCode is the code that you get from echo $? after running the command as root.

There is one more thing about mongodb I forgot to mention - when you install the package it will take some time for the main mongodb database to init, until then you won't be able to access it. Thus the workaround here would be to also add sleep and a number of retries making the code look like:

exec {"initiate replica set":
    command => "echo 'rs.initiate()' | mongo",
    path => ["/usr/bin","/usr/sbin","/bin"],
    user => "root",
    require => Class["mongodb"],
    retries => 5,
    sleep   => 60,
}