Search code examples
chef-infratest-kitchenchef-zero

`search` not finding nodes by role on dev


I'm working on spinning up a cluster of nodes. In myrepo/cookbooks/mycookbook/.kitchen.yml I have

driver:
  name: vagrant

provisioner:
  name: chef_zero
  roles_path: '../../roles'
  environments_path: '../../environments'
  data_bags_path: '../../data_bags'

platforms:
  - name: ubuntu-12.04
    suites:
      - name: node01
        driver:
          vm_hostname: "node01.localhost"
        run_list:
          - role[genericnode]
        provisioner:
            client_rb:
                environment: development

In myrepo/cookbooks/mycookbook/recipes/default.rb I have

nodes = []
search(:node, 'role:genericnode').each do |node|
    nodes.push("http://#{node['ipaddress']}:8080")
end

node.default['mysetting'] = nodes.join(',')

In my development environment, though, search returns an empty list. Do I need more than the .kitchen.yml settings above to be able to access the ip addresses of the nodes in my network with the role genericnode?

Additional information:

Here's the role:

{
  "name": "genericnode",
  "description": "Generic Node",
  "json_class": "Chef::Role",
  "default_attributes": {

  },
  "override_attributes": {

  },
  "chef_type": "role",
  "run_list": [
    "recipe[apt::default]", "recipe[mycookbook::default]"
  ],
  "env_run_lists": {

  }
}

Solution

  • For search to work under chef_zero you'll need to provide a folder of node objects as JSON to be the data that gets searched. Search won't find the current node unless it is on the server (which it isn't unless you add it to that nodes/ folder.

    The usual fix for this is to use both search as well as checking local values.

    ips = []
    search(:node, 'roles:genericnode').each do |n|
        ips << n['ipaddress']
    end
    if node['roles'].include?'(genericnode')
      ips |= [node['ipaddress']]
    end
    
    node.default['mysetting'] = ips.map{|ip| "http://#{ip}:8080"}.join(',')