Using the below script to call sudo command in redhat linux with the puppet version 3.7.
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.
Can someone help me on this.?
Thanks in advance.
This command will not do what you expect because all exec resource commands are executed in a spawned process. If you want to execute a command as another user, then the exec resource has a user parameter.
For example:
exec { 'Touch some file':
cwd => '/home/dev02',
command => 'touch some_file',
path => '/usr/bin/',
logoutput => 'on_failure',
user => 'dev01'
}
Related
I'm trying to run a script (let's call it test.sh) via ssh as follows
ssh ${user}#${ip} "python3 test.py"
and the test.py is as follows
import os
# Do sth
...
os.system("emulator xxx")
...
The android environemnt paths are exported in ~/.bashrc, but the above cmd failed due to missing ${ANDROID_SDK_ROOT}. I know it's because ssh ${user}#{ip} cmd will setup a non-login and non-interactive shell, but I wonder if there is any solution?
PS:
I have tried #!/bin/bash --login and it failed.
Try this:
ssh -t ${user}#${ip} "bash -i -c 'python3 test.py'"
I'm running this command from my local machine:
ssh -tt -i "pem.pem" ec2-user#ec2-IPADDRESS.compute-1.amazonaws.com "sudo su -c 'cd /dir/;npm install pm2'"
It connects, operates as a super users, cds to dir and attempts to run the command but returns that npm is not a command recognized by the system.
However, when I connect "manually" i.e.
ssh -i "pem.pem" ec2-user#ec2-IPADDRESS.compute-1.amazonaws.com
sudo su
cd /dir
npm install pm2
it works.
npm is installed under root and the system can see it.
ssh -tt -i "pem.pem" ec2-user#ec2-IPADDRESS.compute-1.amazonaws.com "sudo su -c 'cd /dir/;whoami'"
and
ssh -i "pem.pem" ec2-user#ec2-IPADDRESS.compute-1.amazonaws.com
sudo su
cd /dir
whoami
both return "root"
Why can't the npm command be found when running on top of an ssh?
When you login, you create an interactive shell, which typically will read a couple of files, including /etc/profile, $HOME/.profile, and $HOME/.bashrc in the case of bash.
Any of these files can add extra elements (paths) to the PATH variable, which affects which commands can be found.
When you run the command line directly, no such initialisation takes place, and the value of $PATH may be limited to just /bin:/usr/bin.
Next there is sudo, which may or may not import the value of PATH when looking for commands.
Solution
Best you can do is find out where npm is installed, and use its full PATH.
I am creating a group named jboss in puppet and then using exec to run a sed command to make some changes in /etc/group file afterwards.
The problem is that the exec command is running before the group command.
My Yaml file
group { 'jboss':
ensure => 'present',
gid => "501",
}
exec { "modify etc_group":
command => "/bin/sed -i -e '<regex>' /etc/group",
path => "/bin:/usr/bin",
unless => "<condition>",
}
Puppet run output
notice: /Stage[main]/App::Misc/Exec[modify etc_group]/returns: current_value notrun, should be 0 (noop)
notice: /Stage[main]/App::Misc/Group[jboss]/ensure: current_value absent, should be present (noop)
How to make sure that the exec runs after the group command?
Simply just define relationship between group and exec.
E.g:
exec { "modify etc_group":
command => "/bin/sed -i -e '<regex>' /etc/group",
path => "/bin:/usr/bin",
unless => "<condition>",
require => Group['jboss'],
}
More about relationships in puppet here.
Hi I am creating a Vagrant setup and I am needing to fetch a .zip file which will be put in /vagrant/www directory.
They way I am trying to do this is:
exec { 'setup octobercms':
command => "/bin/sh -c 'wget -P /vagrant/www https://github.com/octobercms/install/archive/master.zip'",
timeout => 900,
logoutput => true
}
When vagrant up has been triggered I can see that the file is downloading but it is not appearing in the /vagrant/www directory. The file is not really anything to do with vagrant but will be used to install October CMS.
When using puppet what would be the best way to fetch a zipped file and extract its contents into a directory and remove the zipped archive?
Any help would be much appreciated.
The exec command is run in a generic construct of the shell, and doesn't obey the constraints of the user account evoking it. Try:
exec { 'setup octobercms':
command => "cd /vagrant/www/; /bin/sh -c 'wget https://github.com/octobercms/install/archive/master.zip'",
timeout => 900,
logoutput => true
}
for changing the umask setting , i had created the puppet script which insert the line "umask 0027" in the file "/etc/profile" , but it doesn't show the umask value as 0027, when we echo the umask. but when we relogin then it show the value 0027. so it take effect only after relogin.
but we want the immediate effect of the change without relogin , so we added one more line to my puppet script as "source /etc/profile" but it doesnot work and gave the error as below
'source /etc/profile' is not qualified and no path was specified. Please qualify the command or specify a path.
Could somebody help me on this issue ?
my puppet file looks like below
exec {"modify-umask-entry":
command => "sed -i 's/umask [0-9]\{3,\}/umask 027/g' /etc/profile",
path => "/bin:/usr/bin/",
}
exec { "/bin/echo 'umask 027' >> '/etc/profile'":
unless => "/bin/grep -Fx 'umask[\t][0-9]{3}' '/etc/profile'",
# onlyif => "/bin/grep -i 'umask[ \t][0-9]{3}' /etc/profile | wc -w",
}
exec {"seeting_new_umask":
command => "source /etc/profile",
}