ruby - 厨师在多行上拆分“执行”命令
问题描述
尝试创建说明书以将 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
然而,它并没有真正的区别。任何想法为什么?
解决方案
感谢上面/u/coderanger 的评论,找到了答案!
我instance_id
从 ec2metadata 得到如下信息:
instance_id = shell_out("ec2metadata --instance-id").stdout
添加.strip
修复它:
instance_id = shell_out("ec2metadata --instance-id").stdout.strip
推荐阅读
- reactjs - 当组件最初未呈现时,如何通过 React-Router 4 中的 URL 访问组件?
- fortran - Fortran 结果中的 FFTW 仅包含零
- google-cloud-platform - 谷歌云 pubsub 聚合接收器
- firebase - 使用angularfire2获取firebase中图像的url
- javascript - 使用 Ajax 的数据表
- speech-recognition - 为什么谷歌的语音命令数据集的采样率为 16kHz
- automapper - Automapper MapExpression - 未定义强制运算符
- wordpress - 如何在自定义 php 文件中使用 wordpress 的功能
- angular - NGXS:测试一个动作
- php - Apache mod_rewrite 删除 url 和 php 扩展中的文件夹