Sometimes you need to execute remote commands or copy files on/to remote hosts using ssh/scp commands. Here is how you can do it using Sparrowdo ssh/scp core-dsl functions.
Issuing ssh commands
The shortest form to do this – call `ssh‘ function with minimum of required parameters – command to execute and remote host address.
ssh 'uptime', %( host => '192.168.0.1' )
Usually people use ssh public-key authentication, so it is possible to set a path to ssh private key and provide user id:
ssh 'uptime', %( host => '192.168.0.1', user => 'old-dog', ssh-key => 'keys/id_rsa' );
Note, that ssh private key should be only stored at master host where sparrowdo runs, no other actions need to be taken, sparrowdo will care about coping(*) of ssh private key to the target host. It’s handy!
(*) By the way – sparrowdo will remove a private ssh key from target host when ssh command is done.
There are many options of `ssh’ function you may read about at sparrowdo docs, here are just more examples.
You may run multi-line bash commands btw:
ssh q:to/CMD/, %( host => '192.168.0.1', user => 'old_dog'); set -e apt-get update DEBIAN_FRONTEND=noninteractive apt-get install -y -qq curl CMD
Or don’t execute the same command twice relying on existence of file located at target server:
ssh "rm file", %( host => '192.168.0.1' , create => '/do/not/run/twice' );
And finally you may set alternative descriptions for your commands which will be shown at sparrowdo report to help you understand what your command does:
ssh "cat patch.sql | mysql", %( description => 'patching my database', host => '192.168.0.1' );
Issuing scp commands
`Scp’ command akin `ssh’ one. Except it deals with remote files coping. Nothing to say here but showing some examples.
Copy a number of files to remote hosts 192.168.0.1:
scp %( data => "/var/file1 /var/file2 /var/file3", host => "192.168.0.1:/var/", user => "Me", ssh-key => "keys/id_rsa", );
Note, that files to copy should exists on the target hosts. If they don’t you may copy them from master host first using `file‘ function:
file '/var/file1', %( content => ( slurp 'files/file1' ) ); file '/var/file2', %( content => ( slurp 'files/file2' ) ); file '/var/file3', %( content => ( slurp 'files/file3' ) );
The same way as you do for `ssh’ command you may prevent from coping the same file twice if some file exists at target host:
scp %( data => "/var/biiiiiiig-file", host => "192.168.0.1:/var/data", create => "/tmp/do/not/copy/me/twice" )
And finally one my copy files FROM master to target host, using `pull’ flag:
scp %( data => "/var/data/dir", host => "master-host:/var/file1", pull => 1, ssh-key => "keys/id_rsa", );
That is it. Stay tuned with Sparrowdo Automation. 🙂