0

No output from search in test kitchen

Throwing error check the recipe and suggest me some details

Node JSON file

{
    "id": "cldb",
    "chef_type": "node",
    "json_class": "Chef::Node",
    "run_list": [],
    "automatic": {
        "hostname": "cldb.net",
        "fqdn":"127.0.0.1",
        "name": "cldb.net",
        "ipaddress": "127.0.0.1",
        "roles": [],
        "cldb" : true
    }
}

Recipe


cldbNodes = search(:node, "cldb:true")

cldb = "#{cldbNodes["fqdn"]}"

file '/tmp/test.txt' do
    content "#{cldb}"
end
coderanger
  • 52,400
  • 4
  • 52
  • 75
Udhay
  • 7
  • 7
  • How do you have your kitchen.yml config set up and what path is the node JSON file in? – coderanger Mar 01 '17 at 22:56
  • --- driver: name: vagrant provisioner: name: chef_zero #nodes_path: test/integration/nodes environments_path: test/integration/default/environments client_rb: environment: test always_update_cookbooks: true verifier: name: inspec platforms: - name: centos-7.2 suites: - name: nodesearch run_list: - recipe[nodesearch-cookbook::nodesearch_mapr] – Udhay Mar 01 '17 at 23:01
  • `driver: name: vagrant provisioner: name: chef_zero environments_path: test/integration/default/environments client_rb: environment: test verifier: name: inspec platforms: - name: centos-7.2 suites: - name: nodesearch run_list: - recipe[nodesearch-cookbook::nodesearch_mapr]` – Udhay Mar 01 '17 at 23:06
  • nodes_path: test/integration/nodes – Udhay Mar 01 '17 at 23:06
  • Okay, and what is the full path to the JSON file? – coderanger Mar 01 '17 at 23:09
  • /cookbook/test/integration/nodes/cldb.json – Udhay Mar 01 '17 at 23:11
  • Does it work if you do `cldbNodes.first['fqdn']` instead? A search normally returns an array, not a single object. – coderanger Mar 01 '17 at 23:15
  • `cldbNodes = search(:node, "cldb:true") cldb = "cldbNodes['fqdn']" file '/tmp/test.txt' do content "#{cldb}" end` – Udhay Mar 01 '17 at 23:23
  • out put is cldbNodes['fqdn'] – Udhay Mar 01 '17 at 23:23
  • In file output is cldbNodes['fqdn'] – Udhay Mar 01 '17 at 23:24
  • Yes, that's a string not a Ruby code expression. If you are unfamiliar with Ruby syntax you may need to learn that before working with Chef. – coderanger Mar 01 '17 at 23:24
  • I need array only – Udhay Mar 01 '17 at 23:25
  • cldb = "#{cldbNodes['fqdn']}[0]" i learned ruby – Udhay Mar 01 '17 at 23:28
  • Again, `cldbNodes` is an array so you want `cldb = cldbNodes[0]['fqdn']` or `cldb = cldbNodes.first['fqdn']` – coderanger Mar 01 '17 at 23:31
  • Sorry `#cldbList = Array.new() #cldbNodes.each do |cldb| # cldbList += cldb # i +=1 #end` – Udhay Mar 01 '17 at 23:42
  • commented this part forgot – Udhay Mar 01 '17 at 23:43

1 Answers1

0

To summarize from the comments above, search(...) returns an array so you need to get a specific element, usually the first, before you can access node data.

Using the example above, it would be something like:

cldbNodes = search(:node, "cldb:true")

cldb = cldbNodes.first["fqdn"]

file '/tmp/test.txt' do
    content cldb
end
coderanger
  • 52,400
  • 4
  • 52
  • 75