我正在使用OpsWorks部署一系列应用程序,并希望标记这些实例及其所有相关资源。我正在使用opscode aws cookbook (https://github.com/opscode-cookbooks/aws)来标记我的实例,使用下面的食谱可以很好地工作:
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_resource_tag()可以标记实例、快照和卷,但我需要向它提供要标记的卷的列表。
如何获取实例附加的卷ids?
发布于 2014-09-25 00:35:51
我在http://docs.aws.amazon.com/opsworks/latest/userguide/attributes-json-opsworks-instance.html中看不到任何东西,所以你可能只能使用标准的ohai数据。连接到机器并运行ohai ec2,您将看到完整的元数据树。
发布于 2019-04-08 21:51:57
首先,您需要知道关联的EBS层或堆栈资源,但这些标记当前不能应用于实例的根或默认OpsWorks automatically tag卷。
如果你使用OpsWorks for Windows Stack,我建议你从超市安装以下食谱:
File metadata.rb
depends 'aws', '4.2.2'
depends 'ohai', '4.2.3'
depends 'compat_resource', '12.19.1'接下来,向您的堆栈中添加一个拥有执行list-tags for OpsWorks和create-tags in the EC2 service所需权限的IAM角色。
最后,您可以使用此食谱add-。At
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该方法仅将建立在堆栈上所有标记添加到我的实例的根卷中。
https://stackoverflow.com/questions/26020473
复制相似问题