首页 > 解决方案 > 厨师在多行上拆分“执行”命令

问题描述

尝试创建说明书以将 EBS 卷附加到实例。

操作系统是在 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

知道我在这里做错了什么吗?两者都很新,chef并且ruby还没有找到任何可以在文档中指出我正确方向的东西。

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

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

然而,它并没有真正的区别。任何想法为什么?

标签: rubyamazon-web-serviceschef-infraamazon-ebs

解决方案


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

instance_id从 ec2metadata 得到如下信息:

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

添加.strip修复它:

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

推荐阅读