Trying to create a cookbook to attach an EBS volume to an instance.
OS is Ubuntu 18.04 running inside AWS EC2 with credentials passed via an IAM role.
At some point I have a block as follows:
if do_attach
execute 'attach_ebs_vol' do
command "aws ec2 attach-volume --volume-id #{volume_id} --instance-id #{instance_id} --device #{ebs_device}"
action :run
end
end
However when I actually go to run it, it seems chef
is splitting the command into two separate lines? This is what the output looks like:
[2018-07-12T03:37:28+00:00] FATAL: Mixlib::ShellOut::ShellCommandFailed: execute[attach_ebs_vol] (aws_attach_ebs_vol::default line 69) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '127'
---- Begin output of aws ec2 attach-volume --volume-id vol-08a69721ee5ffe615 --instance-id i-0342cc9794decd206
--device /dev/sdf ----
STDOUT:
STDERR: usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: error: argument --device is required
sh: 2: --device: not found
---- End output of aws ec2 attach-volume --volume-id vol-08a69721ee5ffe615 --instance-id i-0342cc9794decd206
--device /dev/sdf ----
Ran aws ec2 attach-volume --volume-id vol-08a69721ee5ffe615 --instance-id i-0342cc9794decd206
--device /dev/sdf returned 127
Any idea what I'm doing wrong here? Pretty new to both chef
and ruby
and haven't found anything that would point me in the right direction in documentation.
I've initially tried setting the command as a variable like so:
if do_attach
attach_cmd = "aws ec2 attach-volume --volume-id #{volume_id} --instance-id #{instance_id} --device #{ebs_device}"
execute 'attach_ebs_vol' do
command attach_cmd
action :run
end
end
However it doesn't really make a difference. Any ideas why?
Found the answer thanks to /u/coderanger's comment above!
I was getting instance_id
from ec2metadata as follows:
instance_id = shell_out("ec2metadata --instance-id").stdout
Adding .strip
fixed it:
instance_id = shell_out("ec2metadata --instance-id").stdout.strip