Alex Brickman Alex Brickman - 4 months ago 59
Linux Question

Chef knife ssh sed command

I run a sed command with knife ssh:

knife ssh 'name:mynode' 'sudo sed -i 's/https:\/\/test1.mydomain.com/https:\/\/test2.mydomain.com/g' /etc/chef/1.rb' -x myuser ./mypem.pem


I got an error:

sed: -e expression #1, char 11: unknown option to `s'


If I try

knife ssh 'name:mynode' "sudo sed -i 's/https:\/\/test1.mydomain.com/https:\/\/test2.mydomain.com/g' /etc/chef/1.rb" -x myuser ./mypem.pem

Error:

sed: can't read ./mypem.pem: No such file or directory


Also if I try

knife ssh 'name:mynode' "'sudo sed -i 's/https:\/\/test1.mydomain.com/https:\/\/test2.mydomain.com/g' /etc/chef/1.rb'" -x myuser ./mypem.pem
Error:
bash: sudo sed -i s/https://test1.mydomain.com/https://test2.mydomain.com/g /etc/chef/1.rb: No such file or directory


the command works from the machine:

sudo sed -i 's/https:\/\/test1.mydomain.com/https:\/\/test2.mydomain.com/g' /etc/chef/1.rb


Why is this happening?

Answer

create recipe and run it with knife ssh

bash 'sed' do
  code <<-EOH
    sudo sed -i 's,https://test1.mydomain.com,https://test2.mydomain.com,g' /etc/chef/1.rb
    EOH
end