Amazon web services 如何获取附加到OpsWorks实例的EBS卷ID?

Amazon web services 如何获取附加到OpsWorks实例的EBS卷ID?,amazon-web-services,chef-infra,aws-opsworks,Amazon Web Services,Chef Infra,Aws Opsworks,我正在使用OpsWorks部署一组应用程序,我想标记实例及其所有相关资源。我使用opscode aws cookbook()来标记我的实例,使用以下方法可以很好地工作: include_recipe 'aws' custom_tags = node.fetch('aws_tag', {}).fetch('tags', nil) instance_id = node.fetch('ec2', {}).fetch('instance_id', nil) unless custom_tags.ni

我正在使用OpsWorks部署一组应用程序,我想标记实例及其所有相关资源。我使用opscode aws cookbook()来标记我的实例,使用以下方法可以很好地工作:

include_recipe 'aws'

custom_tags = node.fetch('aws_tag', {}).fetch('tags', nil)
instance_id = node.fetch('ec2', {}).fetch('instance_id', nil)

unless custom_tags.nil? || custom_tags.empty?
  aws_resource_tag 'tag instance' do
    tags custom_tags
    resource_id instance_id
    action :update
  end
end
我想扩展这个方法来标记连接到实例的EBS卷
aws\u resource\u tag()
可以标记实例、快照和卷,但我需要为其提供要标记的卷的列表


如何获取附加到实例的卷ID?

我在中没有看到任何内容,因此您可能只需要使用标准ohai数据。连接到计算机并运行
ohai ec2
,您将看到完整的元数据树。

首先,您需要知道相关的层或堆栈资源,但标记当前无法应用于实例的根或默认EBS卷

如果您使用OpsWorks for Windows Stack,我建议您从超市安装以下食谱:

文件元数据.rb

depends 'aws', '4.2.2'
depends 'ohai', '4.2.3'
depends 'compat_resource', '12.19.1'
接下来,向堆栈中添加一个IAM角色,该角色具有执行和删除操作所需的权限

最后,您可以使用此配方添加标签。rb:

Chef::Log.info("******TAGS VOLUME******")
#Chef::Log.level = :debug
instance = search("aws_opsworks_instance", "self:true").first
stack = search("aws_opsworks_stack").first
arnstack = "#{stack['arn']}"
cmd = "aws opsworks list-tags --resource-arn #{arnstack} --region eu-west-1"
Chef::Log.info("****** #{arnstack} ******")
batch  'find_tags' do
  cwd "C:\\Program Files\\Amazon\\AWSCLI"
  code <<-EOH
  #{cmd} > C:\\tmp\\res.json
  EOH
end
if ::File.exist?('C:\\tmp\\res.json')
  myjsonfile = ::File.read('C:\\tmp\\res.json').chomp
  data = JSON.parse("#{myjsonfile}")
  data['Tags'].each do |key, value|
    aws_resource_tag 'Boot Volume' do
        resource_id lazy {instance['root_device_volume_id']}
        tags(key => value)
    end
  end
end
Chef::Log.info(“*******标记卷*******”)
#Chef::Log.level=:调试
实例=搜索(“aws\u opsworks\u实例”,“self:true”)。首先
堆栈=搜索(“aws\u opsworks\u堆栈”)。首先
arnstack=“#{stack['arn']}”
cmd=“aws opsworks列表标签——资源arn{arnstack}——地区eu-west-1”
厨师:Log.info(“******{arnstack}*****”)
批处理“查找标签”操作
cwd“C:\\Program Files\\Amazon\\AWSCLI”
代码值)
结束
结束
结束

该配方将所有建立在堆栈上的标记仅添加到我的实例的根卷。

在我运行
sudo opsworks agent cli get_json
时生成的元数据中没有EBS信息。Ohai只挖掘Amazon元数据服务器上存在的任何数据。默认情况下,Ohai二进制文件没有随实例一起安装。Amazon的所有文档都说要使用
opsworks agent cli
来收集有关实例的信息。如果我们谈论的是同一件事,亚马逊似乎根本不包括这些数据。Ohai是Chef的内部组成部分,它与OpsWorks没有(直接)关系。好吧,我理解。但是,无论我做什么,我似乎都无法访问有关连接到实例的EBS卷的任何信息。我已经尝试将
节点的内容转储到日志中,以查看其中的内容,但没有关于EBS的内容。