ruby-on-railsrubychef-infrachef-recipeapache-felix

While using array in chef i am facing issue


I am trying to use array in my chef recipe. Please find the code:-

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

I am getting this error

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

Solution

  • You need to use %w(apvrd1234 apvrd2345) instead of %s('apvrd1234', 'apvrd2345'). You are getting a single string, you want an array of strings.

    Also since you're just hitting an endpoint the bash resource isn't being very useful:

    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