Search code examples
node.jspuppetredhat

Puppet "onlyif" attribute cannot find not(!) operator


I am trying to figure out if Node package PM2 is installed in the path and executable or not with the following puppet code.

 exec { "create symbolic link for pm2":
    cwd => "${pm2_link_dir}",
    path => ['/usr/bin','/bin','/usr/sbin','/sbin'],
    onlyif => "! which node &> /dev/null",
    command => "ln -s ../lib/node_modules/pm2/bin/pm2 pm2"
}

It is telling me cannot find command "!". Is this the right way to find out if some program is installed and executable?? And why puppet cannot understand The not operator?? I am working on Redhat master and slave.


Solution

  • And why puppet cannot understand The not operator?

    The ! operator is provided by the shell; it is not a command. You are using Exec's default provider (posix) which runs your commands directly rather than via a shell. (Or so it is documented to do. It has recently come to light that sometimes the posix provider runs commands via the shell, in apparent contradiction of its docs.)

    It is anyway a bit silly to use ! in an Exec's onlyif attribute, when you could instead drop the ! and switch to an unless attribute instead. And drop the redirection, which also relies on the shell.

    Is this the right way to find out if some program is installed and executable?

    It is usually better to know whether the program should be available on the specific target node, and where necessary to ensure that it is available. If you must inquire about node state, then it is usually better to do that via a custom fact.

    With that said, I don't think your approach is inherently wrong, though of course it will only look for the requested program in the path you specify in the Exec.