Search code examples
chef-infrasudoknife

knife ssh contains sudo and prompts for password


I am using a execute resource which calls knife ssh. The only issue is, it works fine if sudo isn't used, the moment sudo is introduced password for root is requested and the cookbook doesn't converge. Need help:

Current State:

...    
execute 'command' do
           command "knife ssh 'name:mynode' -x user123 -i '/tmp/user123.pem' -a ipaddress 'sudo echo Hi > /tmp/output.txt;'"
            live_stream true
end
...

After chef-client is run (as a Jenkins Build) it prompts the following and doesn't converge because the password is not put-in: mynode> knife sudo password:

Desired State:

...    
execute 'command' do
               command "knife ssh 'name:mynode' -x user123 -i '/tmp/user123.pem' -a ipaddress 'sudo echo Hi > /tmp/output.txt;'"
                live_stream true
end
...

After chef-client is run, string Hi is stored in /tmp/output.txt

NOTE: If sudo is excluded, then the convergence takes place. Is it a way wherein we can include sudo and still attain convergence without having to input the password.


Solution

  • I would suggest you to create / use another user and to generate an SSH key pair for that user to use. Then if you require root privileges you could just give the user the privileges required.