【问题标题】:How to tag multiple resources reading lazily them from an attribute?如何标记多个资源从属性中懒惰地读取它们?
【发布时间】:2021-03-04 19:28:06
【问题描述】:

我想标记多个 AWS 实例,它们的实例 ID 存储在一个属性中,例如-

aws
  |_ instances
            |____ 0: i-12sdfddf
            |____ 1: i-23fdgfdg
            |____ 2: i-34rtetio

这里,在读取节点数据的时候,我得懒加载。以下对我来说很好 -

    aws_resource_tag 'tag_instances' do
        resource_id lazy{node['aws']['instances'][0]}
        tags (tag)
    end

    aws_resource_tag 'tag_instances' do
        resource_id lazy{node['aws']['instances'][1]}
        tags (tag)
    end
.. and so on

但我想在一个循环中标记所有实例。像这样 -

aws_resource_tag 'tag_instances' do
  resource_id lazy {node['aws']['instances'].values}
  tags (tag)
end

我是新手,请帮忙。

更新:

根据@seshadri_c 的回答,这里存在一个错误-bug#243

有解决办法吗?这样的事情会有所帮助 -

instance_values = lazy {node['aws']['instances']}

instance_values.each do |index, instance_id|
  aws_resource_tag 'ec2_instances' do
    resource_id instance_id
    tags(tags)
  end
end

【问题讨论】:

  • 示例属性和单个resource_id 显示[aws][instance],但您想循环使用['ebs']['volumes']?您能用该属性的示例更新问题吗?
  • 谢谢,@seshadri_c .. 更新了问题。

标签: ruby chef-infra aws-sdk cookbook recipe


【解决方案1】:

aws_resource_tag 资源的resource_id 属性可以采用一组实例。所以我们可以从node['aws']['instances'] 哈希中取出values

aws_resource_tag 'tag_instances' do
  resource_id lazy { node['aws']['instances'].values }
  tags (tag)
end

更新:

我认为我们在上游代码中遇到了bug。它已经开放 3 年多了。

但循环遍历 Hash 或 Array 的典型方法是使用 .each 迭代器。如果你能得到node['aws']['instances'] 没有 lazy,那么你可以使用类似下面的东西:

node['aws']['instances'].each do |indx, instance_id|
  aws_resource_tag 'ec2_instances' do
    resource_id instance_id
    tags(tags)
  end
end

【讨论】:

  • 这给了我错误 - ArgumentError: expected params[:filters][0][:values][0] to be a String, got value ["i-12sdfddf", "i-23fdgfdg"] (class: Array) instead.
  • 我认为我们遇到了bug。我更新了答案。
  • 谢谢。我不能在没有lazy 的情况下使用node['aws']['instances'],因为该值稍后会在配方中设置。有解决方法吗?像 - values = lazy { node['aws']['instances'] } values.each do |indx, instance_id| aws_resource_tag 'ec2_instances' do resource_id instance_id tags(tags) end end
  • 我认为这是不可能的。 lazy 不适用于变量赋值。它在 Chef 资源中工作。
  • 你能用一个node['aws']['instances']在配方中设置的例子来更新这个问题吗?
猜你喜欢
  • 2011-10-16
  • 2019-09-11
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
相关资源
最近更新 更多