Running shell commands with Puppet

As you might be aware of, I am directly comparing all my previously 18 written Cfengine 3 code snippets with Puppet 2.x/3.x.
This time a Puppet manifest will run a very simple shell command, similar to the Cfengine 3 version.

Writing the Puppet manifest for running a shell command

exec { "start_mysql":
	command	=> "/etc/init.d/mysql start"

	# Here you could also use require
	# require	=> Package["mysql-server"]
	# Another important feature would be setting the path variable
	# path	=> "/usr/local/bin:/bin/"

Now let’s apply the manifest and verify the result:

/etc/init.d/mysql stop

ps aux |grep mysql
root     12366  0.0  0.0   4384   832 pts/0    S+   15:48   0:00 grep
--colour=auto mysql

puppet apply xenuser_org-006-running_shell_commands.pp
Warning: Could not retrieve fact fqdn
Notice: /Stage[main]//Exec[start_mysql]/returns: executed successfully
Notice: Finished catalog run in 2.25 seconds

ps aux |grep mysql
mysql    12611  2.7  0.9 318512 34604 ?        Ssl  15:48   0:00
root     12833  0.0  0.0   4384   836 pts/0    S+   15:48   0:00 grep
--colour=auto mysql

There you go – MySQL is up and running.

I think that executing shell commands directly via a Puppet manifest is not very “clean”, but sometimes you have no other option. I like that the function for doing so is called “exec” and that there are a lot of cool and useful parameters for it:

Compared to Cfengine 3, I am missing the “comment” which will appear in the syslog. However, the code in Puppet is much shorter.
As usual, you can download today’s Puppet manifest for executing a shell command here.