Reputation: 1113
In a chef recipe I have the following code:
if (node['server1']['PT1'] == true)
setup('PT1')
elsif (node['server1']['PT2'] == true)
setup('PT2')
end
I am checking my attributes to see if the value equals true for either PT1 or PT2. This works correctly if i hardcode server1
into the code but I want to know do it dynamically depending on the server running this. How would I replace node['server1']
with something like node.name
to find different servers in the attribute file. An example of my attributes is:
default['server1'][...]...
default['server2'][...]...
default['server3'][...]...
default['server4'][...]...
If I can dynamically look at the different servers, that'd be the ideal result.
Upvotes: 0
Views: 3909
Reputation: 54181
You can make this totally dynamic even:
node['whatever'][node.name].each do |key, value|
setup(key) if value == true
end
Upvotes: 1
Reputation: 520
Depends on your naming convention. It doesn't look like ohai gathers the node name information automatically but it does gather up a lot of information.
If you have a standard around your node names, such as using their hostname or fqdn as the node name, then you can simply query for that.
node['hostname']...
node['fqdn']...
If you use a more esoteric method to name your nodes that have nothing to do with your host information you can still query the client.rb located on your node, which is how your node knows what to identify itself as to the Chef server. On windows it's located at C:/chef/client.rb
, on UNIX its at /etc/chef/client.rb
. I'll leave the parsing of the file up to you.
To view the full extent of what ohai (everything available under node
) log onto a bootstrapped machine and type ohai
into your shell. Its quite a bit so you might want to output to a text file and use an editor to scroll/search through it.
EDIT1:
In test kitchen the location changes. It becomes <your kitchen cache location>\client.rb>
EX, if you use vagrant with windows and its defaults it becomes c:\users\vagrant\appdata\local\temp\kitchen\client.rb
EDIT2: To bring it back to your original example, if the contents of your node['server'] can either be PT1 or PT2 then you can do the following
setup(node['server'])
and you can control the contents of what server is through any variety of mechanisms. If you are controlling it through hostname then you could do
attributes/default.rb
...
node['server']= node['hostname']
or more simply, if your standards are such that allow for it
recipes/default.rb
...
setup(node['hostname'])
Although normally you'd control what is being setup in separate recipes defined in your runlist.
Upvotes: 2