Chef在多行上拆分“ execute”命令

时间:2018-07-12 03:45:16

标签: ruby amazon-web-services chef amazon-ebs

尝试创建食谱以将EBS卷附加到实例。

OS是在AWS EC2内运行的Ubuntu 18.04,其凭据通过IAM角色传递。

在某个时候,我有一个方块如下:

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

但是,当我实际运行它时,似乎chef会将命令分为两行?输出结果如下:

[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

知道我在做什么错吗?对于chefruby来说都是很新的东西,并且没有发现任何可以使我在文档中朝正确方向发展的东西。

我最初尝试将命令设置为变量,如下所示:

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

但是,它并没有真正改变。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

感谢上面/ u / coderanger的评论,找到了答案!

我从ec2metadata中获得了instance_id,如下所示:

instance_id = shell_out("ec2metadata --instance-id").stdout

添加.strip可以解决此问题:

instance_id = shell_out("ec2metadata --instance-id").stdout.strip