在厨师中使用数组时我遇到了问题

While using array in chef i am facing issue

我正在尝试在我的厨师食谱中使用数组。请找代码:-

node.default['aem_vm_cookbook']['publish_vm'] = %s('apvrd1234', 'apvrd2345')
      node.default['aem_vm_cookbook']['host_name'] = node['hostname']
      author = #{node['aem_vm_cookbook']['host_name']}
     node['aem_vm_cookbook']['publish_vm'].each do |publish|
        bash 'Creating_replication_agent' do
          code <<-EOH
            curl -u admin:admin -F "jcr:primaryType=cq:Page" -F "jcr:content/jcr:title=PublishAgent" -F "jcr:content/sling:resourceType=/libs/cq/replication/components/agent" -F "jcr:content/template=/libs/cq/replication/templates/agent" -F "jcr:content/transportUri=#{publish}/bin/receive?sling:authRequestLogin=1" -F "jcr:content/ssl=relaxed" -F "jcr:content/transportUser=admin" -F "jcr:content/transportPassword=admin" -F "jcr:content/enabled=true" -F "jcr:content/serializationType=durbo"  -F "jcr:content/retryDelay=60000" -F "jcr:content/logLevel=info" http://#{author}:8080/etc/replication/agents.author/#{publish} --insecure
              EOH
        end
    end

我收到这个错误

[2020-07-22T01:26:48-05:00] FATAL: NoMethodError: undefined method `each' for :"'apvrd1234', 'apvrd2345'":Symbol

您需要使用 %w(apvrd1234 apvrd2345) 而不是 %s('apvrd1234', 'apvrd2345')。你得到的是一个字符串,你想要一个字符串数组。

此外,由于您只是到达一个端点,bash 资源不是很有用:

node.default['aem_vm_cookbook']['publish_vm'] = %w(apvrd1234 apvrd2345)
node.default['aem_vm_cookbook']['host_name'] = node['hostname']
author = #{node['aem_vm_cookbook']['host_name']}

node['aem_vm_cookbook']['publish_vm'].each do |publish|
  cmd = <<~EOH
    curl -u admin:admin -F "jcr:primaryType=cq:Page" -F "jcr:content/jcr:title=PublishAgent" \
    -F "jcr:content/sling:resourceType=/libs/cq/replication/components/agent" -F "jcr:content/template=/libs/cq/replication/templates/agent" \
    -F "jcr:content/transportUri=#{publish}/bin/receive?sling:authRequestLogin=1" \
    -F "jcr:content/ssl=relaxed" -F "jcr:content/transportUser=admin" \
    -F "jcr:content/transportPassword=admin" -F "jcr:content/enabled=true" \
    -F "jcr:content/serializationType=durbo"  -F "jcr:content/retryDelay=60000" \
    -F "jcr:content/logLevel=info" http://#{author}:8080/etc/replication/agents.author/#{publish} \
   --insecure
  EOH

  shell_out!(cmd)
end