Puppet – Run exec as a specific user

I would like to Puppet to install the node modules which is specified in the Node.js project package.json. But the Puppet agent is run under the root account while the project is owned by another Linux user. The Puppet exec type allows you the specific the running user but it would not include the environmental variables like the $HOME. Those variables have to be set when calling the exec type just like follow.

exec { "install node modules of a nodejs project":
  cwd         => "/home/ykyuen/nodejs-project",
  command     => "npm install",
  user        => "ykyuen",
  environment => ["HOME=/home/ykyuen"],
}

 

If you need to set more than one environmental variable, just add them in an array format.

  environment => ["HOME=/home/ykyuen", "FOO=foo"],

 

Done =)

Reference: Puppet Docs: Type Reference – Exec

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s