1

I have to access ip address of a node with role pg_client in a cookbook recipe named hc-pg. For testing purpose I am using a single node which has both role[pg_client] and recipe[hc-pg] in the runlist. hc-pg has a recipe which tries to search on a node with role pg_client. Following is the recipe code:

ruby_block "searchnode" do
  block do
   pg_client_nodes = search(:node , "chef_environment:#{node.chef_environment} AND role:pg_client")
  end
end

Following is the pg_client.json file

{
"name" : "pg_client",
"description" : "Client which connects to postgres server"
}

I am using test kitchen with vagrant driver and following is the .kitchen.yml

---
driver:
  name: vagrant

provisioner:
  name: chef_zero

platforms:
  - name: precise64
    driver:
     box: precise64-chef

suites:
  - name: default
    roles_path: ../../roles
    run_list:
      - role[pg_client]
      - recipe[hc-pg]
    attributes:
     postgresql:
      config:
       listen_addresses: "*"

I have mentioned the role before the recipe in run list. When I converge the node for the first time, search on pg_client returns 0 node. When the same node is converged the second time, I do get the node with pg_client role. So my assumption is that on a fresh run, the role is not applied till chef converges the node. Is this a valid assumption? How do I access the node with a role then?

Vaibhav
  • 569
  • 6
  • 31
  • Roles are not applied until *after* the first Chef run is successful – sethvargo Mar 05 '14 at 14:14
  • Is my approach valid then? I wish to use the role within the chef run. My use case is to get the ip address of a node with role `pg_client`. It can be the same node which is being provisioned. – Vaibhav Mar 06 '14 at 07:28
  • I don't know what you mean. You can't search for applied roles until the roles are applied. – sethvargo Mar 06 '14 at 16:53
  • Ideally there would be two nodes, one each for `pg_client` and `hc-pg`. And I will provision one node after the other. But for testing purpose I wish to assign them to a single node and test my recipes. – Vaibhav Mar 07 '14 at 08:55

1 Answers1

1

Generally nodes are not returned in search queries until after the first run so it's probably impossible.

Just to be 100% sure you can also try using keyword roles (plural instead of singular) in the search query:

search(:node , "chef_environment:#{node.chef_environment} AND roles:pg_client")
makhan
  • 3,809
  • 2
  • 18
  • 24