Ask Your Question

How to call the sudo command in linux using Puppet

asked 2015-09-08 12:16:00 -0500

sathyamus gravatar image

updated 2015-09-15 20:36:09 -0500

GregLarkin gravatar image

I am facing some difficulties while calling the below sudo command.

Exec {
  cwd => "/home/dev02",
  command => "sudo -su dev01",
  path => "/usr/bin/",
  logoutput => "on_failure",

I am not getting any errors, but after executing this script, when i checked to see my user with "whoami", still am seeing as dev02 instead of dev01.

exec { 'before-sudo':
 command => '/usr/bin/whoami',
 logoutput => true,

exec { 'sudo-dev01':
 command => "sudo -su dev01 whoami",
 path => "/usr/bin/",
 logoutput => true,
 require => Exec['before-sudo'] 

Exec { 'after-sudo':
 command => '/usr/bin/whoami',
 logoutput => true,
 require => Exec['sudo-dev01'] 


Notice: /Stage[main]/Main/Exec[before-sudo]/returns: dev02
Notice: /Stage[main]/Main/Exec[before-sudo]/returns: executed successfully
Notice: /Stage[main]/Main/Exec[sudo-dev01]/returns: dev01
Notice: /Stage[main]/Main/Exec[sudo-dev01]/returns: executed successfully
Notice: /Stage[main]/Main/Exec[after-sudo]/returns: dev02
Notice: /Stage[main]/Main/Exec[after-sudo]/returns: executed successfully

I have some calls after the Exec[after-sudo], which needs the sudo user. Now with the above code, it was giving the dev02 user.

Can you please help me on this.

I am using puppet 3.7 version and running the puppet using below command.

puppet apply ./test.pp -parser=future

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-09-08 17:44:39 -0500

GregLarkin gravatar image

updated 2015-09-15 20:40:40 -0500

Puppet does not spawn new interactive shell sessions, so after the puppet apply command, you will still be user "dev02".

If you wrote the command as "sudo -su dev01 whoami", then you should see the correct output during the puppet apply run, but again, you will return to user dev02 once puppet apply exits.

UPDATE: To be more clear about this - the sudo will only be in effect for the duration of the exec resource that spawns it. My answer above implies that the sudo to the alternate user will be in force during the rest of the Puppet run after the exec resource, but that's not the case.

If you have multiple commands that depend on a different user privilege, you will have to call each one with sudo in its exec resource, e.g.

exec { 'step1':
  command => 'sudo -su myuser command-step1',

exec { 'step2':
  command => 'sudo -su myuser command-step2',
  require => Exec['step1'],

exec { 'step3':
  command => 'sudo -su myuser command-step3',
  require => Exec['step2'],
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

1 follower


Asked: 2015-09-08 12:16:00 -0500

Seen: 530 times

Last updated: Sep 15 '15